+ All Categories
Home > Documents > Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases...

Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases...

Date post: 19-Jan-2016
Category:
Upload: posy-long
View: 220 times
Download: 0 times
Share this document with a friend
Popular Tags:
32
Tree Rotations & Splay Trees
Transcript
Page 1: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Tree Rotations & Splay Trees

Page 2: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

BST Structure

• BST's only perform well when balanced• But common cases lead

to unbalanced trees

Page 3: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Restoring Balance

• How could this unbalanced tree be balanced?

Page 4: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Restoring Balance

• How could this unbalanced tree be balanced?

Page 5: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Rotation

• Parent node become child, child becomes parent– Right rotation : Everyone moves right (clockwise)– Left rotation : Counter clockwise

Page 6: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Rotation Description

• Describe rotations by parent– Rotate 38 right

– Rotate 21 right

Page 7: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Child Issues

• Want to rotate 38 right (make 21 parent)– Where does 26 go?

Page 8: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Child Issues

• Right child of old child left child of old parent

Page 9: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Right Rotation

Page 10: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Left Rotation

Page 11: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Multi Step

• Want to balance this tree:

Page 12: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Multi Step

• Want to balance this tree:– Rotate root right is just as bad:

Page 13: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Multi Step

• Want to balance zig-zag pattern

Page 14: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Multi Step

• Want to balance zig-zag pattern– Rotate child of root left

Page 15: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Multi Step

• Want to balance zig-zag pattern– Rotate child of root left– Rotate root right

Page 16: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Deciding What to Rotate

• Figuring out where to rotate is tough– Need more info than available locally in BST• AVL, RB

Page 17: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Deciding What to Rotate

• Figuring out where to rotate is tough– Need more info than available locally in BST• AVL, RB

• Alternative:– Keep recent nodes at top of tree– Do a pretty good job balancing

Page 18: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Splay Tree

• Splaying : move node to root using rotates• Splay Tree : after any access, move node to

root– O(1) to access same element again– Faster to access nearby values– Amortized O(logn) average access

Page 19: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

How it works:

• Insert 1-6 in order BST:http://www.cs.usfca.edu/~galles/visualization/BST.html

• Insert 1-6 in order Splay Tree:http://www.cs.usfca.edu/~galles/visualization/SplayTree.html

Page 20: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Splay Algorithm

• Rotating upwards to root does not always balance tree:

Page 21: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Find 1

• Insert 1-6, Find 1:

Page 22: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Find 1

• Insert 1-6, Find 1:

Page 23: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Zig-Zig and Zig-Zag

• Rotations done in 3 ways– Node is right below root: Zig (Rotate left or right)

Page 24: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Zig-Zig and Zig-Zag

• Rotations done in 3 ways– Path to node is left-left or right-right:

Zig-Zig (Rotate grandparent, then rotate parent)

Page 25: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Zig-Zig and Zig-Zag

• Rotations done in 3 ways– Path to node is left-right or right-left:

Zig-Zag (Rotate parent one way, then grand parent other way)

Page 26: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Sample:

• Splay 42

Page 27: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Sample:

• Splay 42– It is left-left child : Zig-Zig

Page 28: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Sample:

• Splay 42– It is left-left child : Zig-Zig• Rotate 52 right

Page 29: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Sample:

• Splay 42– It is left-left child : Zig-Zig• Rotate 52 right, rotate 47 right

Page 30: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Sample:

• Splay 42– Now it is a left-right child : Zig-Zag

Page 31: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Sample:

• Splay 42– Now it is a left-right child : Zig-Zag• 35 Rotates left

Page 32: Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.

Sample:

• Splay 42– Now it is a left-right child : Zig-Zag• 35 Rotates left, 70 rotate right


Recommended