You are on page 1of 3

Splay Trees- self adjusting trees

Bottom-Up Splay Trees

Binary search trees.


No extra information is stored on the nodes (no height nor
color)
All operations search, insert, delete, and split have
amortized complexity O(log n) & actual complexity O(n).
 (split generating two binary trees, one contains all the
keys larger than a given value, the other tree contains the
rest).
Actual and amortized complexity of join is O(1).
 (Join gluing trees. Inverse of split.)

Search, insert, delete, and join are done as in an


unbalanced binary search tree.
Search, insert, and delete are followed by a
splay operation that begins at a splay node.
When the splay operation completes, the splay
node has become the tree root.
Join requires no splay.

Splay

Splay Step
Splaying pushing a node to the root.
If q = null or q is the root, do nothing (splay is
over). Else
If q is at level 2, do a one-level move and
terminate the splay operation.
q
p

Let q be a node in the tree, which we refer to as


the splay node.
During a splay operation, q is moved up the
tree using a series of splay steps.
In a splay step, the node q moves up the tree by
1or 2 levels.

a,b,c,d - subtrees

p
b

the case that q right child of p is symmetric.

2-Level Move (case 2)

Splay Step
If q is at a level > 2, do a two-level move and continue the
splay operation.

gp

gp
c

b
q is a left child of left child of gp,

or

q is a right child of right child of gp. Treaed


symmetrically
This is a zig-zig step.

gp

gp

d
a

q
b

q left child of right child of gp is symmetric.


Zig-zag step

Performing insert( x )

Performing search(k)
20

20

10
6

40
15

10

30

25

40

Definition: If there is a node containing key k, then this is the splay


node.
Otherwise, the node where the search terminates is the splay node.
On either case, perform splay on this node.

Idea: We splay the largest element y of T1,


We obtain a tree whose root does not have a right subtree.
We connect the root to the right subtree.

47

30

25

If there is already a node contains key x, then this node is the splay
node.
Otherwise, the newly inserted node is the splay node.
Example insert(47)

Performing delete(x) deleting x

Performing concat(T1,T2) merging 2 trees


Given two trees T1, T2, such that each key in T1, is smaller
than each key in T2.

15

Find x and splay it it moves to the root.


Remove it it leaves two subtrees.
Concatenate them.
Same result shorter description:
 Find x, splay it, and replace the root with the largest key
in the last subtree (note that it does not have right child)

y
x
T1

T1

T2

T2

split(k)
Split generate two subtrees, one with all its keys larger than k,
and one with all keys smaller than k.
Use the unbalanced binary search tree insert algorithm to insert a a
new key k.
Then splay on k.
Following the splay, the left and right subtrees are the output trees.

TS
L

T
BR

T1

kill x and
concat

Splay x
T2

T1

T2

Main Result
Theorem: Any sequence of m operations on an
initially empty splay tree that never contains
more than n nodes takes O(m log n)

Amortized Analysis of splay trees


Running time of each operation is proportional to time
for splaying.
Define rank(v ) as the logarithm (base 2) of the number
of nodes in subtree rooted at v.
Actual Costs: zig = $1, zig- zig= $2, zig- zag= $2.
Thus, cost for splaying a node at depth d = $d.
Imagine that we store rank(v) dollars at each node v of
the splay tree (just for the sake of analysis).
Invariant. Each node v always has at least rank(v)
dollars.
Lemma 1: Each splay operations requires us to invest at
most 3 log2n+1 new dollars, to maintain the invariant.

You might also like