Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Splay Trees: A Self-Balancing Binary Search Tree, Study notes of Computer Science

An overview of splay trees, a self-balancing binary search tree. It covers the motivation for using splay trees, their basic operations, and the splaying process. Splay trees are easy to implement and perform well in practice, making them a popular choice for data structures.

Typology: Study notes

Pre 2010

Uploaded on 08/17/2009

koofers-user-xsv-1
koofers-user-xsv-1 🇺🇸

10 documents

1 / 29

Toggle sidebar

Related documents


Partial preview of the text

Download Splay Trees: A Self-Balancing Binary Search Tree and more Study notes Computer Science in PDF only on Docsity!

CSE 2100

Splay Trees

Overview

Motivation

Basic Operations

Splaying

Implementation

Motivation

Objective...

A simple solution to unbalanced tree

Avoid pathological cases

Get balanced over time

What we will do

Understand the data structure

Understand the implementation

What we will not do

The analysis

Balanced Trees

Why we care....

Balanced implies cheap to search

Height=4! h

Nodes=31! 2

h+

-

Unbalanced Tree

What is the problem?

Height =

Nodes =

Question

Can we

Have Another binary search tree with

The same content

and

Better balanced?

Answer...

Sure!

Idea

Choose a better root!

Answer...

Sure!

Idea

Choose a better root!

“Re-wire” the tree

Can we do any better in this case?

The big Picture

Splay Trees are

Binary search tree

Self-rebalancing

They are not guaranteed to be balanced

They tend towards a balance state

Why do we study them?

Splay trees are easy to implement

Splay trees do extremely well in practice

Splay tree carry a very low overhead

Compared to other balanced trees

How It All Works

One simple key idea

Good news

No additional information to store or update

What must be defined

Exactly how an element floats up the tree!

Each time the tree is accessed (insertion, lookup

or deletion), the deepest node reached is splayed

(it floats) to the root of the tree.

The binary search tree property is maintained

during the splaying.

Splaying

A splaying considers

Up to 3 nodes

X! The node being splayed

Y! The parent of X

Z! The grand-parent of X (if it exist)

Up to 4 sub-trees

The two subtrees of X

The sibling of X

The aunt/uncle of X

Up to 3 cases

Based on how X,Y,Z are hooked up together

Case 1: Zig-Zig

Actually, this covers two cases

X

Y=parent(X)

Z=grand-parent(X)

The node we splay

Objectives:

X moves closer to the root

The partition is maintained

Case 1: Zig-Zig

Let's do it!

X

Y

Z

Z

Y

X

Case 1: Zig-Zig

Let's do it!

X

Y

Z

Z

Y

X

Case 1: Zig-Zig

Let's do it!

X

Y

Z

Z

Y

X

Case 1: Zig-Zig

Let's do it!

X

Y

Z

Z

Y

X

Case 2: Zig-Zag

Actually, this also covers two cases

X

Y=parent(X)

Z=grand-parent(X)

The node we splay

Objectives:

X moves closer to the root

The partition is maintained

The height gets reduced!

Case 2: Zig-Zag

Let's do it!

X

Y

Z

X

Z

Y

Case 2: Zig-Zag

Let's do it!

X

Y

Z

X

Z

Y

Case 2: Zig-Zag

Let's do it!

X

Y

Z

X

Z

Y

Case 2: Zig-Zag

Let's do It!

X

Y

Z

X

Z

Y

Case 2: Zig-Zag

Let's do it!

X

Y

Z

X

Z

Y

We squished the tree!

Case 3: plain Zig

Again, this also covers two cases

W

X

Y=parent(X)

The node we splay

Objectives:

X moves to the root

The partition is maintained

The height gets reduced!

Case 3: plain Zig

Let's do it!

W

X

Y

X

Y

W

Case 3: plain Zig

Let's do it!

W

X

Y

X

Y

W