0% found this document useful (0 votes)
57 views

Splay Tree

The document discusses the splay tree algorithm, including what a splay tree is, the different types of splaying operations (zig-zig, zig-zag, zig), and the rules for splaying after search, insertion, and deletion operations.

Uploaded by

Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Splay Tree

The document discusses the splay tree algorithm, including what a splay tree is, the different types of splaying operations (zig-zig, zig-zag, zig), and the rules for splaying after search, insertion, and deletion operations.

Uploaded by

Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Splay Tree Algorithm

Contents
 What is Splay Tree?
 Splaying: zig-zig; zig-zag; zig.
 Rules: search; insertion; deletion.
What is Splay Tree?
 A balanced search tree data structure
 NIST Definition: A binary search tree in
which operations that access nodes
restructure the tree.
 Goodrich: A splay tree is a binary search
tree T. The only tool used to maintain
balance in T is the splaying step done after
every search, insertion, and deletion in T.
 Kingston: A binary tee with splaying.
Splaying
 Left and right rotation
 Move-to-root operation
 Zig-zig
 Zig-zag
 Zig
 Comparing move-to-root and splaying
 Example of splaying a node
Left and right rotation
Adel’son-Vel’skii and Landis (1962), Kingston

Left rotation:

Y X

X T1
T3 Y

T1 T2 T2 T3
Right Rotation:

X Y

Y T1
T3 X

T1 T2 T2 T3
Move-to-root operation (x=3)
Allen and Munro (1978), Bitner(1979), Kingston

2 6

1 3 5 7
Move-to-root operation (x=3)

3
6

5 7
1
Move-to-root operation (x=3)
3

2
6

1
5 7
Zig-zig splaying
 The node x and its parent y are both left
children or both right children.
 We replace z by x, making y a child of x
and z a child of y, while maintaining the
inorder relationships of the nodes in tree T.
 Example: x is 30, y is 20, z is 10
Before splaying:

10

20

T1
30

T2

T3 T4
After splaying: 30

20
T4

10
T3

T1 T2
Zig-zag splaying
 One of x and y is a left child and the other is
a right child.
 We replace z by x and make x have nodes y
and z as its children, while maintaining the
inorder relationships of the nodes in tree T.
 Example: z is 10, y is 30, x is 20
Before splaying:

10

30

T1 20

T4

T2 T3
After splaying:

20

10 30

T1 T2 T4
T3
Zig splaying
 X doesn’t have a grand parent(or the
grandparent is not considered)
 We rotate x over y, making x’s children be
the node y and one of x’s former children u,
so as to maintain the relative inorder
relationships of the nodes in tree T.
 Example: x is 20, y is 10, u is 30
Before splaying:

10

20

T1
30

T2

T3 T4
After splaying:

20

10 30

T1 T2 T4
T3
Move-to-root vs. splaying

 Move-to-root should improve the performance of


the BST when there is locality of references in the
operation sequence, but it is not ideal. The
example we use before, the result is not balanced
even the original tree was.
 Splaying also moves the subtrees of node x(which
is being splayed) up 1 level, but move-to-root
usually leaves one subtree at its original level.
 Example:
Example1:original tree

10

20

T1
30

T2

T3 T4
Example1:move-to-root
30

10
T4

20
T1

T2 T3
Example1:splaying 30

20
T4

10
T3

T1 T2
Example2:original tree
50

40

30

20

10
Example2:
move-to-root 10

50

40

30

20
Example2:splaying

10

40

20 50

30
Splaying a node: original tree

20
25
40
10

30 50
15

35
Splaying a node: cont.

20
25
35
10

30 40
15

50
Splaying a node: cont.
35

25 40

20
30
50
10

15
Rules of splaying: Search

 When search for key I, if I is found at node


x, we splay x.
 If not successful, we splay the parent of the
external node at which the search terminates
unsuccessfully.(null node)
 Example: see above slides, it can be for
successfully searched I=35 or
unsuccessfully searched say I=38.
Rules of splaying: insertion

 When inserting a key I, we splay the newly


created internal node where I was inserted.
 Example:
Original tree

10
After insert key 15

10

15
After splaying

15

10
After insert key 12

15

10

12
After splaying

12

15
10
Rules of splaying: deletion

 When deleting a key I, we splay the parent


of the node x that gets removed. x is either
the node storing I or one of its
descendents(root, etc.).
 If deleting from root, we move the key of
the right-most node in the left subtree of
root and delete that node and splay the
parent. Etc.
 Example:
Original tree
30

10

40

15

50

20
After delete key 30(root)
20

10

40

15

50
We are going to splay 15
20

10

40

15

50
After splaying 15

10 20

40

50

You might also like