Docsity

Top-Down Splay Trees - Data Structures - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Top-Down Splay Trees - Data Structures - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Advanced Data Structures and

Implementation

  • Top-Down Splay Trees
  • Red-Black Trees
  • Top-Down Red Black Trees
  • Top-Down Deletion
  • Deterministic Skip Lists
  • AA-Trees
  • Treaps
  • k-d Trees
  • Pairing Heaps

Top-Down Splay Tree

• Direct strategy requires traversal from the root down

the tree, and then bottom-up traversal to implement

the splaying tree.

• Can implement by storing parent links, or by storing

the access path on a stack.

• Both methods require large amount of overhead and

must handle many special cases.

• Initial rotations on the initial access path uses only

O(1) extra space, but retains the O(log N) amortized

time bound.

X

Y

YL Yr

XR

If Y should become root, then X and

its right sub tree are made left children

of the smallest value in R, and Y is

made root of “center” tree. Y does not

have to be a leaf for the Zig case to

apply.

L R

L R

X

XR

Y

YL Yr

Case 1: Zig

Case 3: Zig-Zag (Simplified)

X

Y

ZL Zr

XR

L

R

Z

YL

The value to be splayed is in the tree rooted at Z. To make code simpler, the

Zig-Zag rotation is reduced to a single Zig. This results in more iterations in

the splay process.

L

R

X

XR

Y

ZL Zr

Z

YL

Reassembling the Splay Tree

X

XL XR

L

R

X

L

R

XL XR

When the value to be splayed to the root is at the root of the “center”

tree, we have reached the point where we are ready to reassemble the

tree. This is accomplished by a) making XL the right child of the

maximum element in L, b) making XR the left child of the minimum

element in R, and then making L and R the left and right children of X

Operation 2: Zig-Zag

L R
C D E F G H X

Cr

Dl

Er

Fl

Gl

Hl

Xl Xr

Just perform

Zig

(simplified

Zig-Zag)

D

Dl

L R
C

Cr

A
B

Br Ar

L was previously

empty and it now

consists of node D and

D’s left subtree

A
B

Br Ar

E
F
G
H
X

Er

Fl

Gl

Hl

Xl Xr

L
R

After X reaches root:

X

Xl Xr

C

Cr

A
B

Br Ar

E

Er

G

Gl

F

Fl

H

Hl

D

Dl

This

configuration

was achieved

by doing Zig

Zig (of F, G)

followed by a

Zig (node H)

Reassemble – XL becomes right sub

tree of H, XR becomes left sub tree of

E, and then L, R reattached to X

X

Xl

G

Gl

F

Fl

H

Hl

D

Dl

C

Cr

A
B

Br Ar

E

Er

Note that this is not the

same tree as was obtained

by doing BU splaying.

Red-Black Tree

• Popular alternative to the AVL tree.

• Operations take O(log N) time in worst case.

• Height is at most 2log(N+1).

• A red-black tree is a binary search tree with one extra

attribute for each node: the color , which is either red or black.

• The root is black.

• If node is red, its children must be black.

• Every path from a node to a null reference must contain the

same number of black nodes.

• Basic operations to conform with rules are color changes and

tree rotations.

Theorem 1 – In a red-black tree, at least

half the nodes on any path from the root

to a leaf must be black.

Proof – If there is a red node on the path,

there must be a corresponding black node.

Theorem 3 –

A red-black tree with n internal nodes has

height h <= 2 lg(n + 1).

Proof: Let h be the height of the red-black tree

with root x. By Theorem 1,

bh(x) >= h/

From Theorem 1, n >= 2bh(x) - 1

Therefore n >= 2 h/2 – 1

n + 1 >= 2h/

lg(n + 1) >= h/

2lg(n + 1) >= h

Bottom-Up Insertion

• Cases:

• 0: X is the root – color it black

• 1: Both parent and uncle are red – color parent and uncle

black, color grandparent red, point X to grandparent, check

new situation

• 2 (zig-zag): Parent is red, but uncle is black. X and its parent

are opposite type children – color grandparent red, color X

black, rotate left on parent, rotate right on grandparent

• 3 (zig-zig): Parent is red, but uncle is black. X and its parent

are both left or both right children – color parent black, color

grandparent red, rotate right on grandparent