0% found this document useful (0 votes)
43 views5 pages

Heap Leftist Trees

Uploaded by

amvsbroken
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views5 pages

Heap Leftist Trees

Uploaded by

amvsbroken
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Heap Data Structure:

I. Heaps: A Heap is a complete binary tree data structure that satisfies the heap property: for
every node, the value of its children is greater than or equal to its own value. Heaps are usually
used to implement priority queues, where the smallest (or largest) element is always at the root
of the tree.
Heap sort is used to sort the elements of a tree.

II. Min_MaX Heaps: A min-max heap is a complete binary tree such that if it is not empty,
each element has a field called key. Alternating levels of this tree are min levels and max levels,
respectively. The root is on a min level. Let x be any node in a min-max heap. If x is on a min
level then the element in x has the minimum key from among all elements in the subtree with
root x. We call this node a min node. Similarly, if x is on a max level then the element in x has
the maximum key from among all elements in the subtree with root X. We call this node a max
node.
A min-max heap is a complete binary tree containing alternating min (or even) and max (or
odd) levels. Even levels are for example 0, 2, 4, etc, and odd levels are respectively 1, 3, 5, etc.
We assume in the next points that the root element is at the first level, i.e., 0.
• Each node in a min-max heap has a data member (usually called key) whose value is
used to determine the order of the node in the min-max heap.
• The root element is the smallest element in the min-max heap.
• One of the two elements in the second level, which is a max (or odd) level, is the greatest
element in the min-max heap
• Let x be any node in a min-max heap.
• If x is on a min (or even) level, then x.key is the minimum key among all keys
in the subtree with root x.
• If x is on a max (or odd) level, then x.key is the maximum key among all keys
in the subtree with root x.
• A node on a min (max) level is called a min (max) node.
A max-min heap is defined analogously; in such a heap, the maximum value is stored at
the root, and the smallest value is stored at one of the root's children.[4]
II a. Insertion into Min-Max
To add an element to a min-max heap perform following operations:
1. Append the required key to (the end of) the array representing the min-max heap. This
will likely break the min-max heap properties; therefore, we need to adjust the heap.
2. Compare the new key to its parent:
1. If it is found to be less (greater) than the parent, then it is surely less (greater)
than all other nodes on max (min) levels that are on the path to the root of heap.
Now, just check for nodes on min (max) levels.
2. The path from the new node to the root (considering only min (max) levels)
should be in a descending (ascending) order as it was before the insertion. So,
we need to make a binary insertion of the new node into this sequence.
Technically it is simpler to swap the new node with its parent while the parent
is greater (less).
Example
Here is one example for inserting an element to a Min-Max Heap.

Say we have the following min-max heap and want to insert a new node with value 6.
Initially, node 6 is inserted as a right child of the node 11. 6 is less than 11, therefore it is
less than all the nodes on the max levels (41), and we need to check only the min levels (8
and 11). We should swap the nodes 6 and 11 and then swap 6 and 8. So, 6 gets moved to
the root position of the heap, the former root 8 gets moved down to replace 11, and 11
becomes a right child of 8.

Consider adding the new node 81 instead of 6. Initially, the node is inserted as a right child
of the node 11. 81 is greater than 11, therefore it is greater than any node on any of the min
levels (8 and 11). Now, we only need to check the nodes on the max levels (41) and make
one swap.

Find Minimum

The minimum node (or a minimum node in the case of duplicate keys) of a Min-Max Heap
is always located at the root. Find Minimum is thus a trivial constant time operation which
simply returns the roots.

Find Maximum

The maximum node (or a maximum node in the case of duplicate keys) of a Min-Max Heap
that contains more than one node is always located on the first max level--i.e., as one of
the immediate children of the root. Find Maximum thus requires at most one comparison,
to determine which of the two children of the root is larger, and as such is also a constant
time operation. If the Min-Max heap contains one node then that node is the maximum
node.

III. Liftlist Tree:


To understand leftist tree, we should know S value(Rank value) for each node. S value
for any node in Binary tree is shortest path from a given node to any external node.

After adding external nodes it is called extended trees.

It follows heap ordering property


S(left(x) >= s(right(x)), S value of left subtree >= s value of right subtree.
There are two types of Leftist trees
A. Height Biased B. Weight biased
A. Height Biased Leftist Tree:
An Extended binary tree is a binary tree in which all empty binary subtrees have to be
replaced by a square node (NULL node) called as external node.
Ex:
Binary tree: Extended Binary tree

Let X be a node in an extended binary tree and let leftchild(x) & rightchild(x) be the left and
right subtree parent nodes, then Shortest (x) is:

A leftist tree is a binary tree such that if it is not empty then,


shortest(leftchild(x) >=shortest(rightchild(x)) for every internal nodes,
Let r be the root of a leftist tree that has n (internal) nodes, then:
1) n ≥ 2shortest(r) – 1
2) The rightmost root to external node path is the shortest root to externals node
path. It’s length is shortest(r) ≤ 𝑙𝑜𝑔2 (𝑛 + 1).

B. Weight Biased Leftist Tree:


A binary tree is a weight biased leftist tree, if and only if, at every internal nodes, the
‘w’ value of the left child is greater than or equal the w value of the right child.
0 𝑖𝑓 𝑒𝑥𝑡𝑒𝑟𝑛𝑎𝑙 𝑛𝑜𝑑𝑒
𝑤(𝑥) = {
1 + 𝑤(𝑥 → 𝑙𝑒𝑓𝑡) + 𝑤(𝑥 → 𝑟𝑖𝑔ℎ𝑡) 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
Example:
And is extended tree with weights are

Here, we can see, tree (a) does not satisfy the weight biased Leftist tree, where as tree
(b) satisfy

Applications:

A Leftist Tree is a type of binary tree primarily used in the implementation of priority
queues, particularly in a leftist heap. Leftist trees are designed to maintain a specific
property that helps optimize the merge operation, making them efficient for scenarios
that involve merging heaps or priority queues.

Conclusion:
Leftist trees are primarily used for efficiently handling merge operations in priority
queues. Their primary advantage lies in the optimized time complexity for merging
heaps, which makes them ideal for scenarios where heaps need to be frequently
combined. They strike a balance between tree balance and operational efficiency,
making them a useful data structure in certain contexts like mergeable heaps and
priority queue implementations.

You might also like