You are on page 1of 29

Splaying operation:

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

Simple splaying operation

ZIG -ZAG ROTATIONS

Case 1: zig or zag


If the parent of the x is the root of the tree then If x is the left child then we perform a single AVL rotation zig Else (i.e. x is the right child) we perform a single AVL rotation zag.

Case1: contd

p
x T1 T2 T3 Single AVL rotation (left-to-right) T1 p

T2

T3

(a) Zig rotation

Case1: contd

Single AVL rotation


T1 T2 x T3

(right-to-left)

T3

T1

T2

(b) Zag Rotation

Case 2: zig-zig or zag-zag


If x has both a parent (p) and grandparent (g) If x is the left child of p and p is the left child of the g we perform a double AVL rotation zig-zag. Else (i.e. x is the right child of p and p is the right child of g) we perform a double AVL-rotation zag-zag.

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

(c) zig-zig rotation

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

(c) zag-zag rotation

Case 3: zig-zag or zag-zig


If x has both a parent (p) and a grandparent (g) If x is the left child of p and p is the right child of g then we perform a double AVLrotation zig-zag Else (i.e. x is the right child of p and p is the left child of g) we perform double AVLrotation zag-zig

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

(c) zig-zag rotation

Case3: zag-zig
g T1 2 p 1 x

x T2

T4

Double AVL rotation (left-right and right left) T1

p T2 T1

g T2

T3

(c) zag-zig rotation

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

2nd single AVL rotation

x T2

T2

T3

Algorithm splay TreeRebuild


Input: ROOT is the pointer to the root node ; K is the key to be accessed. Output: Restructure splay tree such that K appears at the root node. Remark: Assume that K is present in the given tree and hence ROOT ! = NULL

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

Current=zig(current,grandchild) Current=zag(current,child) EndCase6 EndWhile Root=current Stop

Application of splay tree


Splay tree do not consider maintaining balance
(height balance in AVL & colour in RBT) information

so it is used in memory constraint applications.


Experiments reveal the fact that splay tree operations run faster than their height-balanced counterparts. Splay trees are used in applications where sometime certain records are frequently accessed .

THANK YOU

You might also like