Professional Documents
Culture Documents
Since a splay tree is a binary search tree, the usual operations, namely searching, insertion and deletion remain same. The operations unique in a splay tree, is called splaying. It Involves rotations. It is performed after every BST operation within the tree.
Splaying operation:
Basic idea after a node is accessed, it is pushed to the root. It is not a simple swap operation between the node and the root. If the node is deep, there are many nodes on the path which are also deep and by restructuring we can make future accesses simpler on these nodes. Restructuring, in fact, has side effect of balancing (to some extent).
One way of restructuring is done by a series of AVL rotations A simple idea may be to perform single rotations, bottom up rotate every node on the access path (a path from root to the node) to its parent
Example
Suppose we want to access the node k2 in the diagram. The access path is shown as the dashed line.
k2 k4 k3 k1 D k2 C A E After rotation R1 k1 B k2 F k4
k5 F E D After rotation R2
k3
R2
R1
Contd.
perform AVL rotation (right-to-left (R1)) between the keys k2 and its parent k1 and will get the resultant tree after the rotation Next we rotate (left-to-right (R2)) between k2 and its parent k3. The tree after such rotation is shown in figure. Again we rotate (left-to-right (R3)) between the k2 and its parent k4 and the resultant tree is shown in the figure.
k5 k4 k2 k1 A B C
R3
k5 F
k2
After rotation R3 k1
R4
F After rotation R4 E
k4
k3 D
B
C
k3 D
Finally, we rotate another left-to-right (R4) single rotation between k2 and its parent k5 and we obtain the splay tree as shown in the figure. As k2 is moved up to the root position, the splaying operation is completed.
k2 k1 A B k3 C D k4 E k5
Case1: contd
p
x T1 T2 T3 Single AVL rotation (left-to-right) T1 p
T2
T3
Case1: contd
(right-to-left)
T3
T1
T2
Case 2:contd
x g T1 Double AVL rotation T2 p g
p
x T1 2
1 T3
T4
T2
(Double left-to-left)
T3
T4
Case 2:contd..
g
1 T1 T2 T3 p 2 x T4 Double AVL rotation (Double right-to-right) T1 T2 g
p
T3
T4
Case 3: zig-zag
g p T1 1 2 T4 Double AVL rotation T3 (right left and left-right) T1 T2 T1 T2 p x
T2
Case3: zag-zig
g T1 2 p 1 x
x T2
T4
p T2 T1
g T2
T3
Case 3: contd
g T4 T1 2 p 1 T4
x p g T1 T2 T3 T4
1st single AVL rotation
g p T1 1 2
x T3
x T2
T2
T3
Steps ::: Current = Root While (current DATA !=k) do If (current DATA < k) then Child =current LCHILD firstMove=zig If (childDATA=k) then secondMove=zero Else If (childDATA< k) then secondMove = zig
grandchild = childLCHILD Else secondMove= zag grandchild= childRCHILD EndIf EndIf Else Child= currentRCHILD firstMove= zag If (childDATA =k) then
secondMove= zero Else If (childDATA< k) then secondMove = zig Grandchild =childLCHILD Else secondMove = zag Grandchild = childRCHILD EndIf EndIf
EndIf Case1 : firstMove=zig AND secondMove= zero Current =zig (current, child) EndCase1 Case2 : firstMove=zag AND secondMove= zero Current =zag(current,child) EndCase2 Case3 :firstMove=zig AND secondMove=zig Current =zig(current,child) Current=zig(current,grandchild)
EndCase3 Case 4: firstMove= zag AND secondMove=zag Current =zag(current,child) Current = zag(current,grandchild) EndCase4 Case5 :firstMove=zig AND secondMove=zag Current=zag(current,grandchild) Current=zig(current,child) EndCase5 Case 6: firstMove=zag AND secondMove=zig
THANK YOU