1 / 27

Foundation of Computing Systems

Foundation of Computing Systems. Lecture 8 Trees: Part V. Height Balanced Binary Trees. Average search time in a binary search tree A binary search tree should be with a minimum value of average search time. t i = Number of comparisons for the i- th element

Télécharger la présentation

Foundation of Computing Systems

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. Foundation of Computing Systems Lecture 8 Trees: Part V

  2. Height Balanced Binary Trees • Average search time in a binary search tree • A binary search tree should be with a minimum value of average search time ti = Number of comparisons for the i-th element n = Total number of elements in the binary search tree IT 60101: Lecture #8

  3. Some Sample Binary Search Trees G = 6.50   IT 60101: Lecture #8

  4. Some Sample Binary Search Trees G = 4.00   G = 3.50   IT 60101: Lecture #8

  5. Some Sample Binary Search Trees G = 3.08   G = 3.08   IT 60101: Lecture #8

  6. Some Sample Binary Search Trees G = 3.16   IT 60101: Lecture #8

  7. Height Balanced Binary Trees • How to find a binary search tree with a minimum value of average search time ? • Solution • Height balanced binary search tree • Also called AVL tree • Concept of balance factor of a node bf = Height of the left sub-tree (hL) – Height of the right sub-tree (hR) IT 60101: Lecture #8

  8. Height Balanced Binary Trees • Definition A binary search tree is said to be height balanced binary search tree if all its nodes have a balance factor of 1, 0 or –1. That is, for all nodes |bf| = |hL – hR| ≤ 1 IT 60101: Lecture #8

  9. Height Balanced Binary Trees: Example IT 60101: Lecture #8

  10. Managing Unbalanced BST • Steps • Insert node into a binary search tree • Compute the balance factors • Decide the pivot node • Balance the unbalance tree IT 60101: Lecture #8

  11. Unbalanced to Balanced BST • AVL Rotations • G.M. Adelson-Velskii and E.M. Lendis (1962) • Case 1: Left-to-Left rotation • pivotleft-sub treeleft child • Case 2: Right-to-Right rotation • pivotright-sub treeright child • Case 3: Left-to-Right rotation • pivotleft-sub treeright child • Case 4: Right-to-Left rotation • pivotright-sub treeleft child IT 60101: Lecture #8

  12. Left-to-Left Rotation IT 60101: Lecture #8

  13. Left-to-Left Rotation IT 60101: Lecture #8

  14. Left-to-Left Rotation IT 60101: Lecture #8

  15. Left-to-Left Rotation IT 60101: Lecture #8

  16. Right-to-Right Rotation IT 60101: Lecture #8

  17. Right-to-Right Rotation IT 60101: Lecture #8

  18. Left-to-Right Rotation • Also called double rotation • Rotation 1 (Right-to-Left) • Left sub-tree (BL) of the right child (B) of the left child of the pivot node (P) becomes the right sub-tree of the left child (A). • Left child (A) of the pivot node (P) becomes the left child of B. • Rotation 2 (Left-to-Right) • Right sub-tree (BR) of the right child (B) of the left child (A) of the PIVOT node (P) becomes the left sub-tree of P. • P becomes the right child of B. IT 60101: Lecture #8

  19. Left-to-Right Rotation IT 60101: Lecture #8

  20. Left-to-Right Rotation IT 60101: Lecture #8

  21. Left-to-Right Rotation IT 60101: Lecture #8

  22. Right-to-Left Rotation • It is also another double rotation • Rotation 1(Left-to-Right) • Right sub-tree (BR) of the left child (B) of the right child (A) of the pivot node (P) becomes the left sub-tree of A. • Right child (A) of the pivot node (P) becomes the right child of B. • Rotation 2 (Right-to-Left) • Left sub-tree (BL) of the right child (B) of the right child (A) of the pivot rode (P) becomes the right sub-tree of P. • P becomes the left child of B. IT 60101: Lecture #8

  23. Right-to-Left Rotation IT 60101: Lecture #8

  24. Right-to-Left Rotation IT 60101: Lecture #8

  25. Right-to-Left Rotation IT 60101: Lecture #8

  26. Implementation • For tricky ways to manage the pointers see the book • Classic Data Structures • Chapter 7 • PHI, 2nd Edn., 17th Reprint IT 60101: Lecture #8

  27. Property of a HBT • Maximum height possible in an AVL tree with n number of nodes is given by • For the proof of this property see the book • Classic Data Structures • Chapter 7 • PHI, 2nd Edn., 17th Reprint IT 60101: Lecture #8

More Related