NORTH SOUTH UNIVERSITY
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.
Every node in a Leftist Heap has a rank which is the
distance to the nearest leaf.
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.
In contrast to a binary heap, a leftist tree attempts to be very
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
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
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
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.
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
The ranks for each of the nodes are updated and the sub trees are
swapped if needed.
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
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.