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. Every node in a Leftist Heap has a rank which is the
distance to the nearest leaf.
The Rank
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. 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. 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. 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.
11. 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.
18. 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
19. 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
23. 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
29. 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.