Splay Tree

Post on 23-Nov-2014

235 views 0 download

Tags:

description

Splay Tree

transcript

SPLAY TREECastaneda, Jonathan S.

Piscos, Shiela Mae

INTRODUCTION

Was invented by Daniel Dominic Sleator and Robert Endre Tarjan in1985.

All normal operations on a binary search tree are combined with one basic operation called splaying.

It performs basic operations such as insertion, look-up and removal in O(log n) amortized time.

ADVANTAGES

Simple implementation Comparable Performance Small memory footprint Possibility of creating a persistent data

structure Working well with nodes containing identical

keys

DISADVANTAGES

Perhaps the most significant disadvantage of splay trees is that the height of a splay tree can be linear

OPERATIONSSplayingInsertionDeletion

SPLAYING

When a node x is accessed, a splay operation is performed on x to move it to the root.

To perform a splay operation we carry out a sequence of splay steps, each of which moves x closer to the root.

The three types of splay steps are: Zig Step Zig-zig Step Zig-zag Step

ZIG STEP

This step is done when p is the root. The tree is rotated on the edge between x and p. Zig steps exist to deal with the parity issue and will be done only as the last step in a splay operation and only when x has odd depth at the beginning of the operation.

ZIG STEP>1P

X

A B

C

ZIG STEP>1-2

P

X

A B

C

Right Child will be transferred

ZIG STEP>1-2-3

P

X

A

B C

ZIG-ZIG STEP

This step is done when p is not the root and x and p are either both right children or are both left children. The picture below shows the case where x and p are both left children. The tree is rotated on the edge joining p with its parent g, then rotated on the edge joining x with p. Note that zig-zig steps are the only thing that differentiate splay trees from the rotate to root method introduced by Allen and Munro prior to the introduction of splay trees.

ZIG-ZIG STEP>1

X

P

G

A B

C

D

ZIG-ZIG STEP>1-2

X

P

G

A B

CD

ZIG-ZIG STEP>1-2-3

X

P

G

A B

C D

ZIG-ZIG STEP>1-2-3-4X

P

G

A B C D

ZIG-ZIG STEP>1-2-3-4-5X

P

G

A

B

C D

ZIG-ZAG STEP

This step is done when p is not the root and x is a right child and p is a left child or vice versa. The tree is rotated on the edge between x and p, then rotated on the edge between x and its new parent g.

ZIG-ZAG STEP>1G

P

X

D

CB

A

ZIG-ZAG STEP>1-2G

P

X

D

CB

A

ZIG-ZAG STEP>1-2-3G

P

X

D

B

C

A

ZIG-ZAG STEP>1-2-3-4

G

X

D

C

BA

P

ZIG-ZAG STEP>1-2-3-4-5X

DCBA

GP

Menu

INSERTION

Start with an empty splay tree and insert pairs with keys 1, 2, 3, …, in this order.

1 1

2 1

2

INSERTION

To insert a node x into a splay tree, we first insert it as with a normal binary search tree. Then we splay the new node x to the top of the tree

INSERTION

Start with an empty splay tree and insert pairs with keys 1, 2, 3, …, in this order.

1

2

3

1

2

3

Menu

DELETION

To delete a node x, we use the same method as with a binary search tree. If x has two children, we replace its value with either the rightmost node of its left sub tree (its in-order predecessor) or the leftmost node of its right subtree (its in-order successor). Then we remove that node. After deletion, we splay the parent of the removed node to the top of the tree.

DELETION

Delete 10

6 11

10

8

7

12

13

17

18

Parent

8

DELETION

Replace 10 with the leftmost node of its right sub tree

6 11

10

7

12

13

8

17

18

Move 7 up

DELETION

Splay the parent

6 11

8

7

12

13

17

18

DELETION

11

6

7

8

13

12

17

18

Transfer 13

DELETION

11

6

7

8

13

12

17

18

DELETION

116

7

8

13

12

17

18

END OF REPORT

Thank you for listeningand

Hope we can all graduate this SY-2011..ahaha