Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Leftist heap

5,322 views

Published on

Leftist tree or leftist heap is a priority queue implemented with a variant of a binary heap.

Published in: Engineering
  • Be the first to comment

Leftist heap

  1. 1. Data structure presentation Leftist heap(min)
  2. 2. CSE 225 Leftist Heap By Shuvro Roy EECS DEPARTMENT NORTH SOUTH UNIVERSITY
  3. 3. A leftist tree or leftist heap is a priority queue implemented with a variant of a binary heap. The parent node is always less than or equals to the child node. Hence , the minimum element is at the top.
  4. 4.  Every node in a Leftist Heap has a rank which is the distance to the nearest leaf. The Rank
  5. 5. The Leftist Heap Property: If the rank, (i.e.- the distance between the nearest null leaf node and the node) of the right sub tree is greater than the rank of the left sub tree, the right sub tree is made the left sub tree i.e.-they are swapped.
  6. 6. In contrast to a binary heap, a leftist tree attempts to be very unbalanced. Due to the Leftist Heap Property the Leftist heaps are maintained so the right descendant of each node has lower rank. Hence, the left sub tree is heavier than the right sub tree.
  7. 7. Insertion The rank of the right child <= the rank of the left child. So we always go right. If the element to be inserted is greater than the existing element in a node, then we insert that element at the right node of the existing node. If the element to be inserted is less than the existing element in a node, then we make the existing node the right node of that element.
  8. 8. Insertion After each insertion the ranks of all the parent nodes are updated back to the root. While updating the rank of a parent, we first compare the rank of the left child and the rank of right child. If the rank of the left child is less than that of the right, then we swap the left child and the right child and update the rank of the parent.
  9. 9. O(log n)
  10. 10. Merge While merging two trees, we first have to compare the two roots of the two trees. The node with the smaller element becomes the new root. The root of the other tree replaces the right node of the root. Then the replaced node is compared with the sub nodes for further placement. The ranks for each of the nodes are updated and the sub trees are swapped if needed.
  11. 11. 4 25 12 15 819 2027 43 6 78 14 Consider two leftist heaps …Consider two leftist heaps … Task: merge them into a single leftist heapTask: merge them into a single leftist heap Merging leftist heaps
  12. 12. 4 25 12 15 819 2027 43 6 78 14 First, instantiate a StackFirst, instantiate a Stack Merging leftist heaps
  13. 13. 4 25 12 15 819 2027 43 6 78 14 First, instantiate a StackFirst, instantiate a Stack 44 Merging leftist heaps x yy
  14. 14. 4 25 12 15 819 2027 43 6 78 14 Remember smaller valueRemember smaller value 66 44 xx yy Merging leftist heaps
  15. 15. 4 25 12 15 819 2027 43 6 78 14 Repeat the process with the right child ofRepeat the process with the right child of the smaller valuethe smaller value 66 44 x yy Merging leftist heaps
  16. 16. 4 25 12 15 819 2027 43 6 78 14 Remember smaller valueRemember smaller value 77 66 44 xx yy Merging leftist heaps
  17. 17. 4 25 12 15 819 2027 43 6 78 14 Repeat the process with the right child ofRepeat the process with the right child of the smaller valuethe smaller value 77 66 44 xx yy nullnull Merging leftist heaps
  18. 18. 4 25 12 15 819 2027 43 6 78 14 Because one of the arguments is null,Because one of the arguments is null, return the other argumentreturn the other argument 77 66 44 xx 88 Merging leftist heaps
  19. 19. 4 25 12 15 19 2027 43 6 8 14 Make 8 the right child of 7Make 8 the right child of 7 77 66 44 x 88 8 7Refers to nodeRefers to node 88 Merging leftist heaps
  20. 20. 4 25 12 15 19 2027 43 6 8 14 Make 7 leftist (by swapping children)Make 7 leftist (by swapping children) 77 66 44 88 8 7Refers to nodeRefers to node 88 Merging leftist heaps
  21. 21. 4 25 12 15 19 2027 43 6 8 14 Return node 7Return node 7 66 44 77 8 7Refers to nodeRefers to node 88 Merging leftist heaps
  22. 22. 4 25 12 15 19 2027 43 6 8 14 Make 7 the right child of 6 (whichMake 7 the right child of 6 (which it already is)it already is) 66 44 77 8 7Refers to nodeRefers to node 88 Merging leftist heaps
  23. 23. 4 25 12 15 19 2027 43 6 8 14 Make 6 leftist (it already is)Make 6 leftist (it already is) 66 44 77 8 7Refers to nodeRefers to node 88 Merging leftist heaps
  24. 24. 4 25 12 15 19 2027 43 6 8 14 Return node 6Return node 6 44 6 8 7Refers to nodeRefers to node 88 Merging leftist heaps
  25. 25. 4 25 12 15 19 2027 43 6 8 14 Make 6 the right child of 4Make 6 the right child of 4 44 66 8 7 Merging leftist heaps
  26. 26. 4 25 12 15 19 2027 43 6 8 14 Make 4 leftist (it already is)Make 4 leftist (it already is) 44 66 8 7 Merging leftist heaps
  27. 27. 4 25 12 15 19 2027 43 6 8 14 Return node 4Return node 4 44 8 7 O(log n)O(log n) Final leftist heap
  28. 28. Deletion To delete the minimum item we disconnect the root form its left and right sub tree. The left and right sub trees now has its own roots. The roots of the two trees are compared and merged into one tree.
  29. 29. Deletion
  30. 30. Deletion Right sub treeLeft sub tree
  31. 31. Deletion
  32. 32. Deletion O(log n)
  33. 33. Time Complexity Get min O(1) Insert O(log n) Delete O(log n) Merge O(log n)
  34. 34. THANK YOU 

×