Docsity

Splay Trees - Advanced Data Structures - Lecture Slides, Slides of Computer Science

Toggle sidebar
bg1
Splay Trees
•Binary search trees.
•Search, insert, delete, and split have amortized
complexity O(log n) & actual complexity O(n).
•Actual and amortized complexity of join is O(1).
•Priority queue and double-ended priority queue
versions outperform heaps, deaps, etc. over a
sequence of operations.
•Two varieties.
Bottom up.
Top down.
Bottom-Up Splay Trees
•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 (or, a null splay is done).
•For the split operation, the splay is done in the
middle (rather than end) of the operation.
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Splay Trees - Advanced Data Structures - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Splay Trees

  • Binary search trees.
  • Search, insert, delete, and split have amortized

complexity O(log n) & actual complexity O(n).

  • Actual and amortized complexity of join is O(1).
  • Priority queue and double-ended priority queue

versions outperform heaps, deaps, etc. over a

sequence of operations.

  • Two varieties. ƒ Bottom up. ƒ Top down.

Bottom-Up Splay Trees

  • 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 (or, a null splay is done).
  • For the split operation, the splay is done in the

middle (rather than end) of the operation.

Splay Node – search(k)

  • If there is a pair whose key is k, the node

containing this pair is the splay node.

  • Otherwise, the parent of the external node where

the search terminates is the splay node.

20

10 6

2 8

15

40 30

25

Splay Node – insert(newPair)

  • If there is already a pair whose key is

newPair.key, the node containing this pair is the

splay node.

  • Otherwise, the newly inserted node is the splay

node.

20

10 6

2 8

15

40 30

25

Splay

  • Let q be the splay node.
  • q is moved up the tree using a series of splay

steps.

  • In a splay step, the node q moves up the tree by

0 , 1 , or 2 levels.

  • Every splay step, except possibly the last one,

moves q two levels up.

Splay Step

  • If q = null or q is the root, do nothing (splay

is over).

  • If q is at level 2 , do a one-level move and

terminate the splay operation.

p q a b

c b c

a

q p

  • q right child of p is symmetric.

Splay Step

  • If q is at a level > 2, do a two-level move

and continue the splay operation.

  • q right child of right child of gp is symmetric.

p q a b

c

gp d

c d

b

p gp

q a

2-Level Move (case 2)

  • q left child of right child of gp is symmetric.

p q b c

a

gp d a b c

p gp

q

d

Per Operation Actual Complexity

  • Worst-case height = n.
  • Actual complexity of search, insert, delete,

and split is O(n).

Top-Down Splay Trees

  • On the way down the tree, split the tree into

the binary search trees T(small elements)

and 7(big elements).

ƒ Similar to split operation in an unbalanced binary search tree. ƒ However, a rotation is done whenever an LL or RR move is made. ƒ Move down 2 levels at a time, except (possibly) in the end when a one level move is made.

  • When the splay node is reached, T, 7 , and the subtree rooted at the splay node are combined into a single binary search tree.

Split A Binary Search Tree

e

d

b

A

a B

c

C

D

f

m g

E

T

Split A Binary Search Tree

A

a

d

b

B

c

C

D

f

m g

E

e

T

Split A Binary Search Tree

b

A

a

B

c

C

d

D

f

m g

E

e

T

Split A Binary Search Tree

b

A

a

B

c

C

d

D

f

m g

E

e

T

Split A Binary Search Tree

b

A

a

B

c

C

d

D

f m

g

E

e

T

Two-Level Moves

e

d

b

A

a B

c

C

D

f

m g

E

  • Let m be the splay node.
  • RL move from A to C.
  • RR move from C to E.
  • L move from E to m.

RR Move

b

A

a

B

d

c

C

D

f

m g

E

e

T

RR Move

b

A

a

B

c d

C

D

f

m g

E

e

T

L Move

b

A

a

B

c d

C

D

f

m g

E

e

T

L Move

b

A

a

B

c d

C

D

f

m g

E

e

T

Wrap Up

b

A

a

B

c d

C

D

f

m

g

E

e

Bottom Up vs Top Down

  • Top down splay trees are faster than bottom

up splay trees.