single rotation
play

Single Rotation k2 k1 k1 k2 h Z X X Y Y Z New item - PowerPoint PPT Presentation

Single Rotation k2 k1 k1 k2 h Z X X Y Y Z New item Suppose an item is added at the bottom of subtree X, thus causing an imbalance at k2. Then pull k1 up. Note that after the rotation, the height of the tree is the same as it was


  1. Single Rotation k2 k1 k1 k2 h Z X X Y Y Z New item Suppose an item is added at the bottom of subtree X, thus causing an imbalance at k2. Then pull k1 up. Note that after the rotation, the height of the tree is the same as it was before the insertion.

  2. Example 4 2 4 2 6 1 3 6 1 3 0 0 Imbalance at node 4 solved with single rotation .

  3. Another Single Rotation k1 k2 k1 k2 h Z X X Y Z Y Suppose an item is added at the bottom of subtree X, thus causing an imbalance at k2. Then pull k1 up. Note that after the rotation, the height of the tree is the same as it was before the insertion.

  4. Another Example 6 4 8 2 4 6 2 10 5 5 8 10 Imbalance at node 4 solved with single rotation.

  5. Single Rotations  After single rotations, the new height of the entire subtree is exactly the same as the height of the original subtree prior to the insertion of the new data item that caused X to grow.  Thus no further updating of heights on the path to the root is needed, and consequently no further rotations are needed.

  6. Double Rotation k k 2 3 k k k 1 1 3 h k D C 2 B A A D B C or or Suppose an item is added below k2. This causes an imbalance at k3. Then pull k2 up. Note that after the rotation, the height of the tree is the same as it was before the insertion.

  7. Another Double Rotation k k 2 3 k k k h 3 1 1 A k 2 B C D A D B C or or Suppose an item is added below k2. This causes an imbalance at k3. Then pull k2 up. Note that after the rotation, the height of the tree is the same as it was before the insertion.

  8. An Example 4 4 2 2 6 6 1 5 1 5 8 9 10 10 8 9 Imbalance at node 8 solved with double rotation.

  9. Which Rotation Do I Use?  Recognizing which rotation you have to use is the hardest part.  Find the imbalanced node.  Walk down two steps.  If the path is straight, use single rotation.  If the path zig-zags, use double rotation.

  10. Double Rotation= 2 Single Rotations k 3 k 1 D k 2 A C B First do a single rotation of k2 and k1.

  11. Double Rotation= 2 Single Rotation s k 3 k 2 D k C 1 A B But k3 still imbalanced, so do a single rotation of k2 and k3 .

  12. Double Rotation= 2 Single Rotations k 2 k k 1 3 C A B D Now we are done.

  13. Double Rotations  As with the single rotations, double rotations restore the height of the subtree to what it was before the insertion.  This guarantees that all rebalancing and height updating is complete.

  14. Programming Tip  You need to maintain a value for height of subtrees  Say variables LHeight and RHeight.  Store height = LHeight - RHeight  Whenever a node is inserted, walk up from that node and update the parent Lheight (or Rheight) and height variables.  In doing so, if a node becomes imbalanced, stop and do rotations  If some ancestor’s height does not change, work is done.

  15. Caution  Maintain height in each node to find which node has imbalance => Extra storage  Deletion is more complicated. May require a series of rotations that propagate upwards.  Difficult to program and debug  Nicer in theory but have to take care of corner cases in deletion and insertion.

  16. Conclusions: AVL Tree  AVL trees maintain balance of binary search trees while they are being created via insertions of data.  An alternative approach is to have trees that readjust themselves when data is accessed, making often accessed data items move to the top of the tree. We won ’ t be covering these (splay trees).

  17. Thank You

  18. EE717: Advanced Computing foR ELECTRICAL Engineers Lecture 7 Priority Queues 21 Aug 2014 Shankar Balachandran, IIT Bombay (shankarb@ee.iitb.ac.in)

  19. Priority Queues  T wo kinds of priority queues:  Min priority queue.  Max priority queue.

  20. Min Priority Queue • Collection of elements. • Each element has a priority or key. • Supports following operations:  isEmpty  size  add/put an element into the priority queue  get element with min priority  remove element with min priority

  21. Max Priority Queue • Collection of elements. • Each element has a priority or key. • Supports following operations:  isEmpty  size  add/put an element into the priority queue  get element with max priority  remove element with max priority  

  22. Complexity Of Operations  T wo good implementations are heaps and leftist trees.  isEmpty, size, and get => O(1) time  put and remove => O(log n) time where n is the size of the priority queue

  23. Applications  Sorting • use element key as priority • put elements to be sorted into a priority queue • extract elements in priority order  if a min priority queue is used, elements are extracted in ascending order of priority (or key)  if a max priority queue is used, elements are extracted in descending order of priority (or key)

  24. Sorting Example  Sort five elements whose keys are 6, 8, 2, 4, 1 using a max priority queue.  Put the five elements into a max priority queue.  Do five remove max operations placing removed elements into the sorted array from right to left.

  25. After Putting Into Max Priority Queue  Sorted Array 8 4 6 Max 1 2 Priority Queue

  26. After First Remove Max Operation  Sorted Array 4 6 Max 1 2 Priority Queue 8

  27. After Second Remove Max Operation  Sorted Array 4 Max 1 2 Priority Queue 6 8

  28. After Third Remove Max Operation  Sorted Array Max 1 2 Priority Queue 4 6 8

  29. After Fourth Remove Max Operation  Sorted Array Max 1 Priority Queue 2 4 6 8

  30. After Fifth Remove Max Operation  Sorted Array Max Priority Queue 1 2 4 6 8

  31. Complexity Of Sorting  Sort n elements.  n put operations => O(n log n) time.  n remove max operations => O(n log n) time.  total time is O(n log n).  compare with O(n2) for other sort methods

  32. Heap Sort  Uses a max priority queue that is implemented as a heap.  Initial put operations are replaced by a heap initialization step that takes O(n) time.

  33. Min Tree Definition  Each tree node has a value.  Value in any node is the minimum value in the subtree for which that node is the root.  Equivalently, no descendent has a smaller value.

  34. Min Tree Example  Root has minimum element. 2 4 9 3 4 8 7 9 9

  35. Max Tree Example  Root has maximum element. 9 4 9 8 4 2 7 3 1

  36. Min Heap Definition • Nearly-complete binary tree and a • Min tree

  37. Min Heap With 9 Nodes  Nearly-Complete binary tree with 9 nodes.

  38. Min Heap With 9 Nodes  Nearly-Complete binary tree with 9 nodes that is also a min tree. 2 4 3 6 7 9 3 8 6

  39. Max Heap With 9 Nodes  Nearly-Complete binary tree with 9 nodes that is also a max tree. 9 8 7 6 7 2 6 5 1

  40. Heap Height  Since a heap is a complete binary tree, the height of an n node heap is log2 (n+1).

  41. A Heap Is Efficiently Represented As An Array 9 8 7 6 7 2 6 5 1 9 8 7 6 7 2 6 5 1 0 1 2 3 4 5 6 7 8 9 10

  42. Moving Up And Down A Heap 1 9 2 3 8 7 4 7 5 6 6 7 2 6 5 1 8 9

  43. Putting An Element Into A Max Heap  Complete binary tree with 10 nodes. 9 8 7 6 7 2 6 5 1 7

  44. Putting An Element Into A Max Heap  New element is 5. 9 8 7 6 7 2 6 5 1 7 5

  45. Putting An Element Into A Max Heap  New element is 20. 9 8 7 6 2 6 7 5 1 7 7

  46. Putting An Element Into A Max Heap  New element is 20. 9 8 7 6 2 6 5 1 7 7 7

  47. Putting An Element Into A Max Heap  New element is 20. 9 7 6 8 2 6 5 1 7 7 7

  48. Putting An Element Into A Max Heap  New element is 20. 20 9 7 6 8 2 6 5 1 7 7 7

  49. Putting An Element Into A Max Heap  Complete binary tree with 11 nodes. 20 9 7 6 8 2 6 5 1 7 7 7

  50. Putting An Element Into A Max Heap  New element is 15. 20 9 7 6 8 2 6 5 1 7 7 7

  51. Putting An Element Into A Max Heap  New element is 15. 20 9 7 6 2 6 8 5 1 7 7 8 7

  52. Putting An Element Into A Max Heap  New element is 15. 20 7 15 6 9 2 6 8 5 1 7 7 8 7

  53. Complexity Of Put  Complexity is O(log n), where n is heap size. 20 7 15 6 9 2 6 8 5 1 7 7 8 7

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend