Vous êtes sur la page 1sur 34

SPLAY TREE

PRESENTED BY Barga Deori(12-25-102) Bishwaranjan Roy(12-25-104) Dept of CSE,NIT Silchar

Why splay tree is needed???


A binary search tree is one of the most important non-linear data structures and a variety of methods are available to keep the tree balanced and hence to guarantee that operations such as insertions, searching and deletion can be done in O(log2n) time in the worst case.

Cont.
We have already discussed two methods to achieve this, namely height-balancing by AVL-Rotations and node colouring (Red Black Tree). There are some situations when the ways of balancing may not guarantee the optimal performance.

Cont.
For example consider the binary search tree T1 and T2 Both the trees contains the same set of key elements k1, k2,k3, k4, k5 in their five nodes such that k1< K2 <k3 < k4<k5

Cont.

k2

k3

k1

k4

k2

k5

k3

k5

k1

k4

Fig1(a)

fig1(b)

Cont.
Consider that in an operation the following accesses are made as shown in the second column in Table 1 . First let us consider the performance of the binary search tree T1. In case of the tree T1, the total no of comparisons is 46.

Cont.
On the other hand the total number of comparisons in case of tree T2 is 27. Note that both the trees T1 and T2 are height balanced. Thus we can infer that if the operation as mentioned in Table 1 occurs frequently then we can save a huge computation times with the tree T2 compared to the tree T1.

Cont.

Table1: Comparisons of the performance of T1 and T2

Cont.
So we need an alternative balancing mechanism to tackle similar situation for the need of reducing the computational overheads.

Splay Tree
To address this requirement, an alternative approach of balancing a binary tree was proposed by D. D. Sleator and R. E. Tarjan in JACM. According to them the tree is called splay tree (splay means to spread wide apart).

Splay Tree
Splay tree is alternatively called self adjusting data structure. It is called self-adjusting because depending on frequency access of nodes the tree structure will be adjusted while those accesses are executed.

Splay Tree
In other words, splay tree is a self adjusting data structure that automatically changes its shape to bring keys closer to the root as they are more frequently accessed and allowing keys those are least accessed to drift slowly out towards the leaves.

Splay Tree
The splay tree is also alternatively termed dynamic binary search tree because its structure keeps changing unlike other type of binary search tree, whose structure remains unchanged once it has been built.

Splay Tree
Further, the splay tree maintains balance without any explicit balance condition such as height or balance factor (in AVL tree), colours (in red black tree). Hence, the splay tree data structure is more efficient compared to its other counterparts.

Splay Tree
Hence, the splay tree data structure is more efficient compared to its other counterparts. In the splay tree, the splay operation (which involves rotation) is performed within the tree every time an access is made.

Definition of splay tree


A splay tree is a binary search tree and it adjusts its balance dynamically providing lower access time of higher occurrence nodes.

Operations on splay tree


Since a play 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. In the splay tree, the splay operation (which involves rotation) is performed within the tree every time an access is made.

Splaying operation:
The basic idea of the splaying operation in a tree is that after a node is accessed, it is pushed to the root. But note that it is not a simple swap operation between the node and the root (this possibly violates the binary search tree property).

Cont.
Rather, 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).

Cont.
One way of performing such a restructuring is by a series of AVL rotations. A simple idea may be to perform single rotations, bottom up. This means that we rotate every node on the access path (a path from root to the node) to its parent.

Cont.

Cont.

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

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

k2 k1 A B k3 C D k4 E k5

Case1:
x

p
x T1 T2 T3

Single AVL rotation


(left-to-right)

T1

T2

T3

(a) Zig rotation

Case2:
p T1 T2 x

x T3

Single AVL rotation

T3

(right-to-left)
T1 T2

(b) Zag Rotation

Case3:
g

x T1 Double AVL rotation (Double left-to-left) T2 p g

p
x T1 2

1 T3

T4

T2

T3

T4

(c) zig-zig rotation

Case4: g
1 T1 T2 T3 p 2 x Double AVL rotation (Double right-to-right) T1 T4 (c) zag-zag rotation T2 g

p
T3

T4

Csae5:
g p T1 1 2 T4 Double AVL rotation T3 (right left and left-right) T1 p T2 T1 x

g T2

T2

(c) zig-zag rotation

Csae6:
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

x p g T1 T2 T3 T4 T1
1st single AVL rotation

g p 1 2 T4 T1

g 2 p 1 T4

x
2nd single AVL rotation

x T3 T2

T2

T3

Vous aimerez peut-être aussi