1 / 44

Lecture 16: Zen & the Art of O ( log n ) Search

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

inigo
Download Presentation

Lecture 16: Zen & the Art of O ( log n ) Search

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 213 – Large Scale Programming Lecture 16:Zen & the Art ofO(logn) Search

  2. 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

  3. Map & Dictionary ADT

  4. Map & Dictionary ADT

  5. 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

  6. AVL Tree Definition • Fancy type of BST • O(logn) time provided • For this, needs more info 6 2 9 1 4 8 5

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. Trinode Restructuring • Case 1: Single rotation (e.g., 3 in a row) a b T0 c T1 T2 T3

  14. 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

  15. 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

  16. Trinode Restructuring • Case 2: Double rotation (e.g., zig-zag) a b T0 c T3 T1 T2

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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

  38. 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

  39. 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

  40. 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

  41. 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

  42. 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

  43. 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

  44. 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

More Related