0% found this document useful (0 votes)
41 views57 pages

Initializing A Max Heap: Input Array

The document describes how to initialize and maintain a max heap data structure using a leftist tree implementation. It provides details on: 1) How to initialize a leftist tree from an input array in O(n) time by creating single node trees, repeatedly melding pairs of trees, and adding the result to a queue. 2) Properties of leftist trees that allow them to represent a max heap, including balancing the tree to keep the rightmost path short. 3) Common heap operations like insertion, deletion, and melding two heaps can all be done in logarithmic time on leftist trees using similar approaches as regular heaps.

Uploaded by

Hololololololo
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views57 pages

Initializing A Max Heap: Input Array

The document describes how to initialize and maintain a max heap data structure using a leftist tree implementation. It provides details on: 1) How to initialize a leftist tree from an input array in O(n) time by creating single node trees, repeatedly melding pairs of trees, and adding the result to a queue. 2) Properties of leftist trees that allow them to represent a max heap, including balancing the tree to keep the rightmost path short. 3) Common heap operations like insertion, deletion, and melding two heaps can all be done in logarithmic time on leftist trees using similar approaches as regular heaps.

Uploaded by

Hololololololo
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 57

Initializing A Max Heap

input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]


8
4
7
6 7
8 9
3
7
10
1
11
5
2
Initializing A Max Heap
Start at rightmost array position that has a child.
8
4
7
6 7
8 9
3
7
10
1
11
5
2
Index is n/2.
Initializing A Max Heap
Move to next lower array position.
8
4
7
6 7
8 9
3
7
10
1
5
11
2
Initializing A Max Heap
8
4
7
6 7
8 9
3
7
10
1
5
11
2
Initializing A Max Heap
8
9
7
6 7
8 4
3
7
10
1
5
11
2
Initializing A Max Heap
8
9
7
6 7
8 4
3
7
10
1
5
11
2
Initializing A Max Heap
8
9
7
6 3
8 4
7
7
10
1
5
11
2
Initializing A Max Heap
8
9
7
6 3
8 4
7
7
10
1
5
11
2
Initializing A Max Heap
8
9
7
6 3
8 4
7
7
10
1
5
11
Find a home for 2.
Initializing A Max Heap
8
9
7
6 3
8 4
7
7
5
1
11
Find a home for 2.
10
Initializing A Max Heap
8
9
7
6 3
8 4
7
7
2
1
11
Done, move to next lower array position.
10
5
Initializing A Max Heap
8
9
7
6 3
8 4
7
7
2
1
11
10
5
Find home for 1.
11
Initializing A Max Heap
8
9
7
6 3
8 4
7
7
2
10
5
Find home for 1.
Initializing A Max Heap
8
9
7
6 3
8 4
7
7
2
11
10
5
Find home for 1.
Initializing A Max Heap
8
9
7
6 3
8 4
7
7
2
11
10
5
Find home for 1.
Initializing A Max Heap
8
9
7
6 3
8 4
7
7
2
11
10
5
Done.
1
public void initialize(Comparable [] theHeap, int theSize)
{
heap = theHeap;
size = theSize;

// heapify
for (int root = size / 2; root >= 1; root--)
{
Comparable rootElement = heap[root];

// find place to put rootElement
int child = 2 * root; // parent of child is target
// location for rootElement
while (child <= size)
{
// heap[child] should be larger sibling
if (child < size &&
heap[child].compareTo(heap[child + 1]) < 0) child++;

// can we put rootElement in heap[child/2]?
if (rootElement.compareTo(heap[child]) >= 0)
break; // yes


while (child <= size)
{
// heap[child] should be larger sibling
if (child < size &&
heap[child].compareTo(heap[child + 1]) < 0) child++;

// can we put rootElement in heap[child/2]?
if (rootElement.compareTo(heap[child]) >= 0)
break; // yes

// no
heap[child / 2] = heap[child]; // move child up
child *= 2; // move down a level
}
heap[child / 2] = rootElement;
}
}
Time Complexity
8 7
6 3
4
7
7
10
11
5
2
9
8
1
Height of heap = h.
Number of subtrees with root at level j is <= 2
j-1
.
Complexity
Thus at most 2
j-1
nodes will have height h-(j-1) or h-j+1
The time for each subtree is O(h-j+1).
Time for level j subtrees is <= 2
j-1
(h-j+1) = t(j).
Total time is:



Since:

) ( ) 2 ( 2 / 2
2 ) 1 ( 2
2
2
1
1
1
n O O k O
k O j h O
h
h
k
k h
h
k
k h
h
j
j
= = |
.
|

\
|
=
|
.
|

\
|
=
|
|
.
|

\
|
+


=
=

m
m
k
k
m k
2
2
2
2
1
+
=

=
Leftist Trees
Linked binary tree.
Can do everything a heap can do and in the
same asymptotic complexity.
Can meld two leftist tree priority queues in
O(log n) time.
Extended Binary Trees
Start with any binary tree and add an
external node wherever there is an
empty subtree.
Result is an extended binary tree.
A Binary Tree
An Extended Binary Tree
number of external nodes is n+1
The Function s()
For any node x in an extended binary tree,
let s(x) be the length of a shortest path
from x to an external node in the subtree
rooted at x.

s() Values Example
s() Values Example
0 0 0 0
0 0
0 0
0
0
1 1
1
2 1 1
2 1
2
Properties Of s()
If x is an external node, then s(x) = 0.

Otherwise,
s(x) = min {s(leftChild(x)),
s(rightChild(x))} + 1
Height Biased Leftist Trees
A binary tree is a (height biased) leftist tree
iff for every internal node x,
s(leftChild(x)) >= s(rightChild(x))
A Height Biased Leftist Tree
0 0 0 0
0 0
0 0
0
0
1 1
1
2 1 1
2 1
2
Leftist Trees--Property 1
In a leftist tree, the rightmost path is a
shortest route to external node path and
the length of this path is s(root).
A Leftist Tree
0 0 0 0
0 0
0 0
0
0
1 1
1
2 1 1
2 1
2
Length of rightmost path is 2.
Leftist TreesProperty 2
The number of internal nodes is at least
2
s(root)
- 1
Because levels 1 through s(root) have no
external nodes.
A Leftist Tree
0 0 0 0
0 0
0 0
0
0
1 1
1
2 1 1
2 1
2
Levels 1 and 2 have no external nodes.
Leftist TreesProperty 3
Length of rightmost path is O(log n), where
n is the number of nodes in a leftist tree.

Follows from Property 2.
Leftist Trees As Priority Queues
Min leftist tree leftist tree that is a min tree.
Used as a min priority queue.
Max leftist tree leftist tree that is a max tree.
Used as a max priority queue.
A Min Leftist Tree
8 6
9
6 8 5
4 3
2
Some Min Leftist Tree Operations
put
remove()
meld()
initialize()
put() and remove() use meld().
Put Operation
put(7)
8 6
9
6 8 5
4 3
2
Put Operation
put(7)
8 6
9
6 8 5
4 3
2
Create a single node min leftist tree.
7
Put Operation
put(7)
8 6
9
6 8 5
4 3
2
Create a single node min leftist tree.
Meld the two min leftist trees.
7
Remove Min
8 6
9
6 8 5
4 3
2
Remove Min
8 6
9
6 8 5
4 3
2
Remove the root.
Remove Min
8 6
9
6 8 5
4 3
2
Remove the root.
Meld the two subtrees.
Meld Two Min Leftist Trees
8 6
9
6 8 5
4 3
6
Traverse only the rightmost paths so as to get
logarithmic performance.
Meld Two Min Leftist Trees
8 6
9
6 8 5
4 3
6
Meld right subtree of tree with smaller root and
all of other tree.
Meld Two Min Leftist Trees
8 6
9
6 8 5
4 3
6
Meld right subtree of tree with smaller root and all of
other tree.
Meld Two Min Leftist Trees
8 6
6 8
4
6
Meld right subtree of tree with smaller root and all of
other tree.
Meld Two Min Leftist Trees
8
6
Meld right subtree of tree with smaller root and all of
other tree.
Right subtree of 6 is empty. So, result of melding right
subtree of tree with smaller root and other tree is the
other tree.
Meld Two Min Leftist Trees
Swap left and right subtree if s(left) < s(right).
Make melded subtree right subtree of smaller root.
8
6
6
8
6
8
Meld Two Min Leftist Trees
8 6
6 6
4
8
8 6
6
4
6
8
Make melded subtree right subtree of smaller root.
Swap left and right subtree if s(left) < s(right).
Meld Two Min Leftist Trees
9
5
3
Swap left and right subtree if s(left) < s(right).
Make melded subtree right subtree of smaller root.
8 6
6 6
4
8
1
2
Meld Two Min Leftist Trees
9
5
3
8 6
6 6
4
8
Complexity Analysis of Meld
Meld only moves to the right of the two
trees x and y that are being melded.
Thus complexity is O(s(x) + s(y))
s(x) and s(y) are at most log
2
(m+1) and
log
2
(n+1) where m and n are the number of
elements in the max HBLTs with roots x
and y.
Result: O(log
2
m + log
2
n) = O(log
2
mn)
Initializing In O(n) Time
create n single node min leftist trees
and place them in a FIFO queue
repeatedly remove two min leftist trees
from the FIFO queue, meld them, and
put the resulting min leftist tree into the
FIFO queue
the process terminates when only 1 min
leftist tree remains in the FIFO queue
Initializing In O(n) Time
analysis is the same as for heap
initialization
Assume that the number of nodes, n is a
power of 2.
The first n/2 melds involve max HBLTs
with one element each.
The next n/4 melds involve max
HBLTs with two elements each
The next n/8 melds involve max
HBLTs with four elements each
Initializing In O(n) Time
The time needed to meld two trees with
2
i
elements each is O(i + 1)
Since log
2
2
i
is just i.
Total time taken by initialize is:
O(n/2 + 2*(n/4) + 3*(n/8) +



Since we know:

) (
2
n O
i
n O
i
=
|
.
|

\
|
=

m
m
k
k
m k
2
2
2
2
1
+
=

You might also like