1 / 23

Lab#9

Lab#9. AVL + Heap + Big O. AVL Tree. Height-balanced binary search tree where the heights of sub-trees differ by no more than 1: | H L – H R | <= 1 Each node has a balance factor Balance factors may be: Left High (LH) = +1 (left sub-tree higher than right sub-tree)

lovie
Télécharger la présentation

Lab#9

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. Lab#9 AVL + Heap + Big O

  2. AVL Tree • Height-balanced binary search tree where the heights of sub-trees differ by no more than 1: | HL – HR | <= 1 • Each node has a balance factor • Balance factors may be: • Left High (LH) = +1 (left sub-tree higher than right sub-tree) • Even High (EH) = 0 (left and right sub-trees same height) • Right High (RH) = -1 (right sub-tree higher than left sub-tree)

  3. HR=2 HR=3 HL=1 HL=1 AVL Tree HL-HR=1 Unbalanced BST HL-HR = 2

  4. Balancing Trees • Insertions and deletions potentially cause a tree to be imbalanced • When a tree is detected as unbalanced, nodes are balanced by rotating nodes to the left or right • Four imbalance cases: • Left of left: the sub-tree of a left high tree has become left high • Right of right: the sub-tree of a right high tree has become right high • Right of left: the sub-tree of a left high tree has become right high • Left of right: the sub-tree of a right high tree has become left high • Each imbalance case has simple and complex rotation cases

  5. Case1: Left of left Simple Right rotation Single Right rotation Rotate the out of balance node (the root) to the right

  6. Case1: Left of left Insert 4 18 12 12 20 8 18 14 8 14 4 20 Complex Right rotation 4 • Rotate root to the right, so the old root is the right sub-tree of the new root; the new root's right sub-tree is connected to the old root's left sub-tree

  7. Case2 : Right of right Simple left rotation Rotate the out of balance node (the root) to the left

  8. Case2 : Right of right Complex left rotation Rotate root to the left, so the old root is the left sub-tree of the new root; the new root's left sub-tree is connected to the old root's right sub-tree

  9. Case3 : Right of left Insert 7 11 11 7 5 7 left rotation Right rotation 11 5 7 5 • Simple double rotation right: Rotate left sub-tree to the left; rotate root to the right, so the old root's left node is the new root

  10. Case4 : Left of right Rotate right

  11. Case4 : Left of right Rotate Left Rotate right sub-tree to the right; rotate root to the left, so the old root's right node is the new root

  12. Case4 : Left of right Rotate Right

  13. Case4 : Left of right Rotate Left

  14. HW: • The following keys are inserted • (in the order given) into an initially empty AVL tree. • Show the AVL tree after each insertion. 24,39,31,46,48,34,19,5,29

  15. Heap • A particular kind of binary tree, called a heap • It has two properties: • The value of each node is greater(max heap)/less (min heap) than or equal to the value stored in each of its children. • The tree is perfectly balanced, and the leaves in the last level are all in the leftmost positions. • Heaps can be implemented by arrays. • Elements in a heap are not perfectly ordered. There is no relation between siblings .

  16. Heap or Not ? Min Heap 10 10 20 80 20 20 80 40 60 85 99 30 15 50 700 not a heap heap

  17. Heaps as Priority Queues • A heap is a way to implement priority queues. • Two procedures has to be implemented : • Enqueue an element : the element is added at the end of the heap as the last leaf . • 2. Dequeue an element : remove the root element from the heap.

  18. Enqueue 60 40 50 20 25 30 35 10 12 15 45 60 45 50 20 40 30 35 10 12 15 25

  19. Dequeue 60 40 50 20 25 30 35 10 12 15 19 19 40 50 20 25 30 35 10 12 15

  20. 50 40 19 20 25 30 35 10 12 15 50 40 35 20 25 30 19 10 12 15

  21. Algorithm efficiency • A function of the number of elements to be processed • f(n)=efficiency • If a function is linear – that is, if it contains no loop – then its efficiency is a functions of the number of instructions it contains. • On the other hand, functions that loop vary widely in their efficiency. Algorithm Efficiency

  22. What is the Big-O complexity of each of the following methods? O(n) O(n^2) O(n)

  23. O(n^2)

More Related