440 likes | 552 Views
CSC 213 – Large Scale Programming. Lecture 16: Zen & the Art of O ( log n ) Search. Today’s Goals. Review Map & Dictionary implementations What do they do well? When would they be used? Why do they suck so much another is needed? Discuss how BSTs can achieve Zen-like balance
E N D
CSC 213 – Large Scale Programming Lecture 16:Zen & the Art ofO(logn) Search
Today’s Goals • Review Map & Dictionaryimplementations • What do they do well? When would they be used? • Why do they suck so much another is needed? • Discuss how BSTs can achieve Zen-like balance • Nodes will need more data, what fields do we add? • How this will modify approach to add & remove data? • What nodes get restructured and how to know? • Are there any neat hacks to coding restructures? • How traces help us write this tricky code easily
Binary Search Trees • Maintain specific order • Lower keys - left subtree • Right subtree for higher • Equalkeys not specified, just be consistent • Fastest when complete • Normally we see worst case • How to force this to balance? 6 2 9 1 4 8 10
AVL Tree Definition • Fancy type of BST • O(logn) time provided • For this, needs more info 6 2 9 1 4 8 5
AVL Tree Definition • Fancy type of BST • O(logn) timeprovided • For this, needs more info 4 6 3 2 2 9 1 2 1 1 4 8 1 5 Node heights are shown in blue
AVL Tree Definition • Fancy type of BST • O(log n) time provided • For this, needs more info • Keep tree balanced by… • Checking heights of kids • Only let differ by 0 or 1 4 6 3 2 2 9 1 2 1 1 4 8 1 5 Node heights are shown in blue
AVL Tree Definition • Fancy type of BST • O(log n) time provided • For this, needs more info • Keep tree balanced by… • Checking heights of kids • Only let differ by 0 or 1 4 6 3 2 2 9 1 2 1 1 4 8 1 5 Node heights are shown in blue
AVL Tree Definition • Fancy type of BST • O(log n) time provided • For this, needs more info • Keep tree balanced by… • Checking heights of kids • Only let differ by 0 or 1 • Fix larger differences by • Shifting nodes in the BST • Help it maintain balance 4 6 3 2 2 9 1 2 1 1 4 8 1 5 Node heights are shown in blue
AVL Tree Definition • Fancy type of BST • O(log n) time provided • For this, needs more info • Keep tree balanced by… • Checking heights of kids • Only let differ by 0 or 1 • Fix larger differences by • Shifting nodes in the BST • Help it maintain balance Trinode Restructuring 4 6 3 2 2 9 1 2 1 1 4 8 1 5 Node heights are shown in blue
Trinode Restructuring • Insertion & removal can unbalance tree • Restore tree’s taousing trinode restructuring • When node’s children’s height differ more than 1 • Node, taller child, & tallest grandchild used for this • Grandchild used must betaller child of taller child • Median node subtree's root after restructure • Restructure makes the other 2 nodes its children • Also redistributes other child & grandkids
Trinode Restructuring • Case 1: Single rotation (e.g., 3 in a row) a b T0 c T1 T2 T3
Trinode Restructuring • Case 1: Single rotation (e.g., 3 in a row) • Move the taller child to subtree root • Parent & grandchild become kids of the taller child a b b T0 a c c T1 T2 T3
Trinode Restructuring • Case 1: Single rotation (e.g., 3 in a row) • Move the taller child to subtree root • Parent & grandchild become kids of the taller child • Does not (CANNOT)change order of nodes in tree a b b T0 a c c T1 T2 T3 T1 T0 T2 T3
Trinode Restructuring • Case 2: Double rotation (e.g., zig-zag) a b T0 c T3 T1 T2
Trinode Restructuring • Case 2: Double rotation (e.g., zig-zag) • Start when taller child & grandchild in opposition • Grandchild becomes root once this completes a c b T0 a b c T3 T1 T2
Trinode Restructuring • Case 2: Double rotation (e.g., zig-zag) • Start when taller child & grandchild in opposition • Grandchild becomes root once this completes • Does not (CANNOT) change order of nodes in tree a c b T0 a b c T3 T3 T0 T2 T1 T1 T2
AVL Tree Insertion • Normal BST insertion starts the process • Once added must then check tree for balance • insert(5) 4 7 3 2 2 9 2 1 1 1 4 8 1 6
AVL Tree Insertion • Normal BST insertion starts the process • Once added must then check tree for balance • insert(5) 4 7 3 2 2 9 2 1 1 1 4 8 1 6
AVL Tree Insertion • Normal BST insertion starts the process • Once added must then check tree for balance • insert(5) ? 7 ? 2 2 9 ? 1 1 1 4 8 ? 6 ? 5
Insertion in an AVL Tree • From inserted node, walk up to root • At worst, stop after root has been processed • With each node, check if children are balanced • Restructure to restore balance among the children • Each insertion performs at most 1 restructuring • Unbalanced node on path from root to inserted node • Most cases do not require a trinode restructuring
AVL Tree Insertion • Normal BST insertion starts the process • Once added must then check tree for balance • insert(5) ? 7 ? 2 2 9 ? 1 1 1 4 8 ? 6 1 5
AVL Tree Insertion • Normal BST insertion starts the process • Once added must then check tree for balance • insert(5) ? 7 ? 2 2 9 ? 1 1 1 4 8 ? 6 1 5
AVL Tree Insertion • Normal BST insertion starts the process • Once added must then check tree for balance • insert(5) ? 7 ? 2 2 9 ? 1 1 1 4 8 2 6 1 5
AVL Tree Insertion • Normal BST insertion starts the process • Once added must then check tree for balance • insert(5) ? 7 ? 2 2 9 ? 1 1 1 4 8 2 6 1 5
AVL Tree Insertion • Normal BST insertion starts the process • Once added must then check tree for balance • insert(5) Oops ? 7 ? 2 2 9 ? 1 1 1 4 8 2 6 1 5
Trinode Restructuring • Uses the node, taller child, tallest grandchild • Must be on insertion path,so used for restructuring • insert(5) ? 7 ? 2 2 9 ? 1 1 1 4 8 2 6 1 5
Trinode Restructuring • Uses the node, taller child, tallest grandchild • Must be on insertion path,so used for restructuring • insert(5) ? 7 ? 2 2 9 ? 1 1 1 5 8 ? ? 4 6
After The Restructuring • Balance restored to tree; tao returns to normal • Will need to complete walk updating heights • insert(5) 4 7 3 2 2 9 2 1 1 1 5 8 1 1 4 6
Removing From AVL Tree • Normal BST removal starts AVL tree removal • Removal of node may also cause tao to drop • remove(9) 4 7 3 2 2 9 2 1 1 1 5 8 1 1 4 6
Removing From AVL Tree • Normal BST removal starts AVL tree removal • Removal of node may also cause tao to drop • remove(9) 4 7 3 2 2 9 2 1 1 1 5 8 1 1 4 6
Removing From AVL Tree • Normal BST removal starts AVL tree removal • Removal of node may also cause tao to drop • remove(9) ? 7 3 ? 2 8 2 1 1 5 1 1 4 6
Removing From AVL Tree • Again walk up tree checking for balance • Examine nodes along path only • There will be no change for nodes not on path • Multiple restructuring operations may be needed • However, may not need any restructuring • Only 1 restructuring could be needed • Use the node, taller child & tallest grandchild • May not be on the path from start to root • Works exactly like restructuring during insert
Removing From AVL Tree • Normal BST removal starts AVL tree removal • Removal of node may also cause tao to drop • remove(9) ? 7 3 ? 2 8 2 1 1 5 1 1 4 6
Removing From AVL Tree • Normal BST removal starts AVL tree removal • Removal of node may also cause tao to drop • remove(9) ? 7 3 1 2 8 2 1 1 5 1 1 4 6
Trinode Restructuring • Uses the node, taller child, tallest grandchild • Nodes may not be on path used during the removal • remove(9) ? 7 3 1 2 8 2 1 1 5 1 1 4 6
After the Restructure • Cannot stop till we reach the top of the tree • May need to perform 0, 1, or multiple restructures • remove(9) 3 5 2 2 2 7 1 1 1 1 1 4 6 8
After the Restructure • Cannot stop till we reach the top of the tree • May need to perform 0, 1, or multiple restructures • remove(9) 3 5 2 2 2 7 1 1 1 1 1 4 6 8
After the Restructure • Cannot stop till we reach the top of the tree • May need to perform 0, 1, or multiple restructures • remove(9) 3 5 2 2 2 7 1 1 1 1 1 4 6 8
Restructuring for Dummies • Store the 7+1 nodes in local variables • Record subtree’s parent in the “+1” variable • All of left, right, & parent set using pattern • Median node is root; subtrees maintain order
Restructuring for Dummies • Store the 7+1 nodes in local variables • Record subtree’s parent in the “+1” variable • All of left, right, & parent set using pattern • Median node is root; subtrees maintain order a b b T0 a c c T1 T2 T3 T1 T0 T2 T3
Restructuring for Dummies • Store the 7+1 nodes in local variables • Record subtree’s parent in the “+1” variable • All of left, right, & parent set using pattern • Median node is root; subtrees maintain order a c b T0 a b c T3 T2 T3 T1 T0 T1 T2
For Next Lecture • Weekly assignment to demonstrate knowledge • Due at regular time Tuesday • Program#1 tests due Mondayat midnight • Remember to submit updated design & test cases • Quiz on Monday on trees, BSTs, & AVL trees • Please study up on all the material covered • Use your templates: they can greatly simplify process • Do not forget that pictures worth 1000 words