1 / 17

CSC 213 – Large Scale Programming

CSC 213 – Large Scale Programming. Lecture 18: Zen & the Art of O (log n ) Search. Today’s Goal. Discuss AVL Trees Relationship to BinaryTree and BST What an AVL Tree looks like and how they work Effects on big-Oh notation AVL Tree Limitations. Dictionary ADT.

Download Presentation

CSC 213 – Large Scale Programming

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 18: Zen & the Art of O(log n) Search

  2. Today’s Goal • Discuss AVL Trees • Relationship to BinaryTree and BST • What an AVL Tree looks like and how they work • Effects on big-Oh notation • AVL Tree Limitations

  3. Dictionary ADT • Dictionary ADT maps key to 1 or more values • Used wherever search is (e.g., everywhere) • Implementation using Sequence takes O(n) time • With good batch of hash, could get O(1) time • But could still end up with O(n) time • If data are random, BST takes O(log n) time • But for ordered data, BST still takes O(n) time • There must be a better way!

  4. Binary Search Trees • May not be complete • But faster when they are • Use different ordering • Lower keys in left subtree • Higher keys in right subtree • Equal keys not specified, but must be consistent 6 9 2 10 1 4

  5. AVL Tree Definition • Another type of BST • Algorithm guarantees O(log n) time • Does not allow tree to become linked list • Keeps tree in balance • AVL Tree balanced when each Node’s children differ in height by at most 1 • Maintains balance through trinode restructuring 4 6 3 2 2 9 2 1 1 8 1 4 1 5 Node heights are shown in red

  6. Trinode Restructuring • Insertion & removal can unbalance tree • Trinode restructuring restores tree’s tao • Used when node’s children’s height differ • Takes node, taller child, & taller grandchild • Grandchild must be taller of taller child’s children • Move median node to root of subtree • Other 2 nodes in restructuring become its children

  7. Trinode Restructuring • Case 1: Single rotation (e.g., 3 in a row) • Move child of 3 to root of subtree • Subtrees become grandchildren of node • No change in the order of subtrees b a T0 b a c T2 T3 T1 T0 c T1 T2 T3

  8. Trinode Restructuring • Case 2: Double rotation • Taller child & grandchild go opposite ways • Move grandchild up to the root • Subtrees become grandchildren of node, but maintain order a b T0 c a c T3 T0 T2 T1 b T3 T1 T2

  9. Insertion in an AVL Tree • Begins like normal BST insertion • Example: insert(5) 4 7 3 2 2 9 2 1 1 8 1 4 2 1 6 1 5

  10. Insertion in an AVL Tree • Travel up tree checking nodes for balance • Must increase tao of unbalanced nodes 7 2 9 8 1 4 6 5

  11. Insertion in an AVL Tree • Must walk up tree starting at inserted Node • Stop once we hit the root node • Update height for each Node along path • Check if Node’s children are balanced • Perform rotations when balance needs restoration • Not all insertions require rebalancing • Will need at most 1 insertion per Node • But some insertions need multiple rebalancings

  12. Removal in an AVL Tree • Start with normal BST removal • But removal may cause drop in the TAO • Example: remove(7) 4 7 8 3 2 1 2 9 2 1 1 8 1 5 1 1 4 6

  13. Removal in an AVL Tree • Start with normal BST removal • But removal may cause drop in the TAO • Example: remove(7) 4 7 8 3 1 2 9 2 1 1 5 1 1 4 6

  14. Removal in an AVL Tree • Start with normal BST removal • But removal may cause drop in the TAO • Example: remove(7) 7 5 2 8 6 1 4 9

  15. Removal in an AVL Tree • Again walk up tree checking for balance • Update heights as we go • Only examines Nodes along path • Height of nodes not on path cannot be changed • May need multiple restructuring operations • Uses same restructuring as insert • Use unbalanced node’s taller child & taller grandchild • Does not matter if child & grandchild on path

  16. Restructuring for Dummies • Store the 7+1 Nodes in local variables • Plus one records parent Node of this subtree • Set all left, right, & parent from pattern • Median node is root; subtrees maintain order a a b b T0 c T0 b a c a c c T3 T3 T0 T2 T0 T2 T1 T1 b T3 T1 T1 T2 T2 T3

  17. In the Next Lecture • Discuss even faster ways of searching • No difference in big-Oh notation, however • But provide significant speedup in real-life • Learn new ways of amortizing costs • Discover proper use of the term “splay” • When and why we splay a tree • What it means to splay • Any other excuses I can think of to say splay

More Related