Self-Organizing Trees
CSCE 2211
Applied Data Structures
Dr. Dina G. Mahmoud
Based on Slides from Prof. Amr Goneid
AUC
Self-Organizing Trees
▪ AVL Trees: Self-Balancing
▪ Red-Black Trees: Self-Balancing
▪ Splay Trees: Self-Organizing
60
Splay Trees: Self-Organizing Tree
▪ First introduced by American scientists Daniel Dominic
Sleator and Robert Endre Tarjan in 1985
▪ Every splay tree must be a binary search tree but does need not to
be a balanced tree
Amortized Worst case
Space O(n) O(n)
Search O(log n) O(n)
Insert O(log n) O(n)
Delete O(log n) O(n)
61
Splay Trees
▪ “splay” means to spread out, expand, or extend
▪ Automatically reorganizes itself so that frequently accessed
or inserted elements become closer to the root node
▪ Any operation on those elements is performed quickly
▪ Based on the fact that the O(N) worst-case time per
operation for binary search trees is not bad, as long as it
occurs relatively infrequently
▪ A guarantee of at most O(M logN) for any M consecutive operations,
is certainly satisfactory
▪ The basic idea is that after a node is accessed, it is pushed to
the root by a series of AVL tree rotations
62
Splay Trees Rotations
1. Zig Rotation
2. Zag Rotation
3. Zig - Zig Rotation
4. Zag - Zag Rotation
5. Zig - Zag Rotation
6. Zag - Zig Rotation
63
Zig Rotation
Splay (3)
64
Zig Rotation
Splay (3)
Zig Rotation
= Single right rotation
65
Zag Rotation
Splay (5)
66
Zag Rotation
Splay (5)
Zag Rotation
= Single left rotation
67
Zig-Zig Rotation
Splay (2)
68
Zig-Zig Rotation
Splay (2)
Zig-Zig Rotation
= Double right rotation
69
Zag-Zag Rotation
Splay (2)
70
Zag-Zag Rotation
Splay (2)
Zag-Zag Rotation
= Double left rotation
71
Zig-Zag Rotation
Splay (4)
72
Zig-Zag Rotation
Splay (4) Splay (4)
Zig Rotation
at 5
73
Zig-Zag Rotation
Splay (4) Splay (4)
Zig Rotation Zag Rotation
at 5 at 3
74
Zag-Zig Rotation
Splay (4) Splay (4)
Zag Rotation Zig Rotation
at 3 at 5
75
Insertion Operation in Splay Tree
▪ The insertion operation in Splay tree is performed using following
steps...
1. Check whether tree is empty
2. If tree is empty, then insert the newNode as root node and exit from
the operation
3. If tree is not empty, then insert the newNode as leaf node using BST
insertion algorithm
4. After insertion, Splay the newNode
76
Deletion Operation in Splay Tree
▪ The deletion operation in Splay tree is performed using following steps
1. Splay the element to be deleted to the root position
2. Delete the root using BST algorithm
77
Insertion Implementation
▪ See code for splay tree insertion on Canvas
78
Splay Trees: Visualization
▪ See Splay Tree visualization at link
79