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.
Example 4 2 4 2 6 1 3 6 1 3 0 0 Imbalance at node 4 solved with single rotation .
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.
Another Example 6 4 8 2 4 6 2 10 5 5 8 10 Imbalance at node 4 solved with single rotation.
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.
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.
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.
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.
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.
Double Rotation= 2 Single Rotations k 3 k 1 D k 2 A C B First do a single rotation of k2 and k1.
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 .
Double Rotation= 2 Single Rotations k 2 k k 1 3 C A B D Now we are done.
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.
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.
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.
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).
Thank You
EE717: Advanced Computing foR ELECTRICAL Engineers Lecture 7 Priority Queues 21 Aug 2014 Shankar Balachandran, IIT Bombay (shankarb@ee.iitb.ac.in)
Priority Queues T wo kinds of priority queues: Min priority queue. Max priority queue.
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
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
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
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)
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.
After Putting Into Max Priority Queue Sorted Array 8 4 6 Max 1 2 Priority Queue
After First Remove Max Operation Sorted Array 4 6 Max 1 2 Priority Queue 8
After Second Remove Max Operation Sorted Array 4 Max 1 2 Priority Queue 6 8
After Third Remove Max Operation Sorted Array Max 1 2 Priority Queue 4 6 8
After Fourth Remove Max Operation Sorted Array Max 1 Priority Queue 2 4 6 8
After Fifth Remove Max Operation Sorted Array Max Priority Queue 1 2 4 6 8
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
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.
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.
Min Tree Example Root has minimum element. 2 4 9 3 4 8 7 9 9
Max Tree Example Root has maximum element. 9 4 9 8 4 2 7 3 1
Min Heap Definition • Nearly-complete binary tree and a • Min tree
Min Heap With 9 Nodes Nearly-Complete binary tree with 9 nodes.
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
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
Heap Height Since a heap is a complete binary tree, the height of an n node heap is log2 (n+1).
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
Moving Up And Down A Heap 1 9 2 3 8 7 4 7 5 6 6 7 2 6 5 1 8 9
Putting An Element Into A Max Heap Complete binary tree with 10 nodes. 9 8 7 6 7 2 6 5 1 7
Putting An Element Into A Max Heap New element is 5. 9 8 7 6 7 2 6 5 1 7 5
Putting An Element Into A Max Heap New element is 20. 9 8 7 6 2 6 7 5 1 7 7
Putting An Element Into A Max Heap New element is 20. 9 8 7 6 2 6 5 1 7 7 7
Putting An Element Into A Max Heap New element is 20. 9 7 6 8 2 6 5 1 7 7 7
Putting An Element Into A Max Heap New element is 20. 20 9 7 6 8 2 6 5 1 7 7 7
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
Putting An Element Into A Max Heap New element is 15. 20 9 7 6 8 2 6 5 1 7 7 7
Putting An Element Into A Max Heap New element is 15. 20 9 7 6 2 6 8 5 1 7 7 8 7
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
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
Recommend
More recommend