Docsity

Splay Trees: A Balanced Binary Search Tree Technique, Slides of Java Programming

Toggle sidebar
bg1
Splay-trees
CalinC urescu, some material from Goodrich & Tamas sia
Splay-trees
Calin Curescu, some material from Goodrich & Tamassia
12 pages
TDDC32 lecture 4, 2006
Splay-trees
CalinC urescu, some material from Goodrich & Tamas sia
2of 12
TDDC32 lecture 4, 2006
Splay Tree
•Splay tree is a binary search tree
– Uses splaying for balancing
– Splay is performed during
• Search, insert, delete
• Moves a node at the root
– Im proved behaviour during repeated access of data
– Guarantees amortised running time
– No additional data needed
• No height information as in AVL needed
Splay-trees
CalinC urescu, some material from Goodrich & Tamas sia
3of 12
TDDC32 lecture 4, 2006
Definitions
•Splay
– Move a node to the root by a sequence of restructuring
•Let x be the node to be splayed
– Let y be its parent node
– If it exists, let z be its grandparent
•Restructurings
– Zig-zig
• Z exists, both x and y are left or both are right children
–Zig-zag
• Z exists, one of x and y is left child the other is right child
–Zig
• Z does not exist or not considered
Splay-trees
CalinC urescu, some material from Goodrich & Tamas sia
4of 12
TDDC32 lecture 4, 2006
Zig
T1T2
T4
T3
x
w
y
T1T2T4
T3
x
wy
Splay-trees
CalinC urescu, some material from Goodrich & Tamas sia
5of 12
TDDC32 lecture 4, 2006
Zig-zig
T1T2
T4
T3
y
x
z
T1
T2
T4
T3
y
x
z
Splay-trees
CalinC urescu, some material from Goodrich & Tamas sia
6of 12
TDDC32 lecture 4, 2006
Zig-zag
T1T2T4
T3
x
zy
T2T3
T1
T4
y
x
z
pf2

Partial preview of the text

Download Splay Trees: A Balanced Binary Search Tree Technique and more Slides Java Programming in PDF only on Docsity!

Splay-trees Calin Curescu, some material from Goodrich & Tamassia

Splay-trees

Calin Curescu, some material from Goodrich & Tamassia

12 pages TDDC32 lecture 4, 2006

Splay-trees Calin Curescu, some material from Goodrich & Tamassia

2 of 12 TDDC32 lecture 4, 2006

Splay Tree

• Splay tree is a binary search tree

– Uses splaying for balancing

– Splay is performed during

• Search, insert, delete

• Moves a node at the root

– Improved behaviour during repeated access of data

– Guarantees amortised running time

– No additional data needed

• No height information as in AVL needed

Splay-trees Calin Curescu, some material from Goodrich & Tamassia

3 of 12 TDDC32 lecture 4, 2006

Definitions

• Splay

– Move a node to the root by a sequence of restructuring

• Let x be the node to be splayed

– Let y be its parent node

– If it exists, let z be its grandparent

• Restructurings

– Zig-zig

• Z exists, both x and y are left or both are right children

– Zig-zag

• Z exists, one of x and y is left child the other is right child

– Zig

• Z does not exist or not considered

Splay-trees Calin Curescu, some material from Goodrich & Tamassia

4 of 12 TDDC32 lecture 4, 2006

Zig

T 1 T 2

T 4

T

x

w

y

T

T

2 T^4

T

x

w y

Splay-trees Calin Curescu, some material from Goodrich & Tamassia

5 of 12 TDDC32 lecture 4, 2006

Zig-zig

T

T

T

T 3

y

x

z

T

T

T

T

y

x

z

Splay-trees Calin Curescu, some material from Goodrich & Tamassia

6 of 12 TDDC32 lecture 4, 2006

Zig-zag

T 1 T^2 T 3 T 4

x

z y

T

T

T

T 4

y

x

z

Splay-trees Calin Curescu, some material from Goodrich & Tamassia

7 of 12 TDDC32 lecture 4, 2006

When to splay?

• Whenever we search, insert, delete

• Which nodes are splayed after each operation?

• Search

– If node found – use that node

– If node not found use last node in the search path

• Insert

– Use the new node

• Remove

– Use the parent of the node that was actually deleted

– I.e. the deepest node

Splay-trees Calin Curescu, some material from Goodrich & Tamassia

8 of 12 TDDC32 lecture 4, 2006

Splay example

• let x = (8,N)

– x is the right child of its parent, which is the left child of the grandparent

– left-rotate around p , then right-rotate around g

(10,A)

(20,Z)

(21,O) (37,P)

(35,R)

(7,T) (36,L) (40,X)

(1,C)

(1,Q)

(2,R) (5,G)

(5,H)

(5,I) (6,Y)

(8,N) (14,J)

(7,P)

(10,U)

x

g

p

(20,Z)

(21,O) (37,P)

(14,J)

(7,T)

(10,A) (35,R)

(1,C)

(1,Q)

(2,R) (5,G)

(5,H)

(5,I) (6,Y)

(8,N)

(7,P)

(36,L)

(10,U)

(40,X)

Splay-trees Calin Curescu, some material from Goodrich & Tamassia

9 of 12 TDDC32 lecture 4, 2006

Splay example cont’d

(10,A)

(20,Z)

(21,O) (37,P)

(35,R)

(36,L) (40,X)

(7,T)

(1,C)

(1,Q)

(2,R) (5,G)

(5,H)

(5,I) (6,Y)

(14,J)

(8,N)

(7,P)

(10,U)

x

g

p

(10,A)

(20,Z)

(21,O) (37,P)

(35,R)

(36,L) (40,X)

(7,T)

(1,C)

(1,Q)

(2,R) (5,G)

(5,H)

(5,I) (6,Y)

(14,J)

(8,N)

(7,P)

(10,U)

x

(10,A)

(20,Z)

(21,O) (37,P)

(35,R)

(36,L) (40,X)

(7,T)

(1,C)

(1,Q)

(2,R) (5,G)

(5,H)

(5,I) (6,Y)

(14,J)

(8,N)

(7,P)

(10,U)

x

Splay-trees Calin Curescu, some material from Goodrich & Tamassia

10 of 12 TDDC32 lecture 4, 2006

Complexity

• What is the worst case cost of a splay operation?

– Why?

• However amortized analysis shows that the cost of M splay

operation is O(M log n)

– In average a splay operation has a complexity of O(log n)

• Repetitive access

– Is even faster than for normal binary search trees since the

latest accessed value is splayed to the root node.

O(n)

Splay-trees Calin Curescu, some material from Goodrich & Tamassia

11 of 12 TDDC32 lecture 4, 2006

Bottom up vs. top-down splay

• Bottom-up

– First we find the node by going down in the tree

• For any of the search, insert, delete operations

– Then we splay the node up to the root

– Example:

• Top-down

– … just mentioning it

– While we are searching for the node we already start building

the final tree by manipulating and rotating subtrees of the

encountered nodes.

Splay-trees Calin Curescu, some material from Goodrich & Tamassia

12 of 12 TDDC32 lecture 4, 2006

Reading

• “Data Structures and Algorithms in Java”

– chapter 10.

• Tommy Olssons compendium

– For an alternative model of splaying

– If you are interested in top-down splaying