tree rotations & splay trees
DESCRIPTION
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?. Restoring Balance. How could this unbalanced tree be balanced?. Rotation. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/1.jpg)
Tree Rotations & Splay Trees
![Page 2: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/2.jpg)
BST Structure
• BST's only perform well when balanced• But common cases lead
to unbalanced trees
![Page 3: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/3.jpg)
Restoring Balance
• How could this unbalanced tree be balanced?
![Page 4: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/4.jpg)
Restoring Balance
• How could this unbalanced tree be balanced?
![Page 5: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/5.jpg)
Rotation
• Parent node become child, child becomes parent– Right rotation : Everyone moves right (clockwise)– Left rotation : Counter clockwise
![Page 6: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/6.jpg)
Rotation Description
• Describe rotations by parent– Rotate 38 right
– Rotate 21 right
![Page 7: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/7.jpg)
Child Issues
• Want to rotate 38 right (make 21 parent)– Where does 26 go?
![Page 8: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/8.jpg)
Child Issues
• Right child of old child left child of old parent
![Page 9: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/9.jpg)
Right Rotation
![Page 10: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/10.jpg)
Left Rotation
![Page 11: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/11.jpg)
Multi Step
• Want to balance this tree:
![Page 12: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/12.jpg)
Multi Step
• Want to balance this tree:– Rotate root right is just as bad:
![Page 13: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/13.jpg)
Multi Step
• Want to balance zig-zag pattern
![Page 14: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/14.jpg)
Multi Step
• Want to balance zig-zag pattern– Rotate child of root left
![Page 15: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/15.jpg)
Multi Step
• Want to balance zig-zag pattern– Rotate child of root left– Rotate root right
![Page 16: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/17.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/20.jpg)
Splay Algorithm
• Rotating upwards to root does not always balance tree:
![Page 21: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/21.jpg)
Find 1
• Insert 1-6, Find 1:
![Page 22: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/22.jpg)
Find 1
• Insert 1-6, Find 1:
![Page 23: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/23.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/24.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/25.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/26.jpg)
Sample:
• Splay 42
![Page 27: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/27.jpg)
Sample:
• Splay 42– It is left-left child : Zig-Zig
![Page 28: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/28.jpg)
Sample:
• Splay 42– It is left-left child : Zig-Zig• Rotate 52 right
![Page 29: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/29.jpg)
Sample:
• Splay 42– It is left-left child : Zig-Zig• Rotate 52 right, rotate 47 right
![Page 30: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/30.jpg)
Sample:
• Splay 42– Now it is a left-right child : Zig-Zag
![Page 31: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/31.jpg)
Sample:
• Splay 42– Now it is a left-right child : Zig-Zag• 35 Rotates left
![Page 32: Tree Rotations & Splay Trees](https://reader035.vdocuments.site/reader035/viewer/2022062410/56816353550346895dd3f5a1/html5/thumbnails/32.jpg)
Sample:
• Splay 42– Now it is a left-right child : Zig-Zag• 35 Rotates left, 70 rotate right