0% found this document useful (0 votes)
44 views6 pages

Heap Ordered Trees

A heap ordered tree is a tree where each node's key is not greater than its children's keys. Heap operations like insert, delete, and finding the minimum can be done efficiently in O(log n) time when implemented using a complete binary tree stored in an array. Binomial queues allow melding two priority queues in O(1) time and support other heap operations in O(log n) time through forests of binomial trees.
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)
44 views6 pages

Heap Ordered Trees

A heap ordered tree is a tree where each node's key is not greater than its children's keys. Heap operations like insert, delete, and finding the minimum can be done efficiently in O(log n) time when implemented using a complete binary tree stored in an array. Binomial queues allow melding two priority queues in O(1) time and support other heap operations in O(log n) time through forests of binomial trees.
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/ 6

1.

6 Heap ordered trees

A heap ordered tree is a tree satisfying the following condition.

“The key of a node is not greater than that of each child if any”

In a heap ordered tree, we can not implement “find” efficiently, taking O(n) time when
the size of the tree is n. We implement the following operations efficiently on a heap
ordered tree.

min
update_key (decrease_key, increase_key)
delete
insert
meld

1.6.1 The heap

The heap is a heap prdered tree of the complete binary tree shape. In general, a heap
ordered tree needs a linked structure which requires some overhead time to traverse,
whereas a heap can be implemented in an array on which heap opearations efficiently
work.

Example

2 3

4 5 6 7

8 9 10

The labels attached to nodes represent the array indices when implemented on an array

1 2 3 4 5 6 7 8 9 10
A complete binary tree occupies the first n elements in an array. If a node i (indicated by
index i) has one or two children the left child is at 2i and the right child (if any) is at
2I+1. The parent of node j (j>1) is at j div 2. These opearations can be implemented by
shift operations in machine code and thus fast.
Suppose the given heap is contained in a[1 .. n]. The procedure siftup(p, q) adjusts
the subtree rooted at p so that the whole tree can be recovered to be a heap when only the
key at p may be greater than those of its children and thus violates the heap condition.
The parameter q indicates the end of the subtree.

1. procedure siftup(p, q);


2. begin
3. y:=a[p]; j:=p; k:=2*p;
4. while k <= q do begin
5. z:=a[k];
6. if k <= q then if z > a[k+1] then begin k:=k+1; z:=a[k] end;
7. if y <= z then goto 10;
8. a[j]:=z; j:=k; k:=2*j
9. end;
10. 10: a[j]:=y
11. end;

The item with a smaller key is repeatedly compared with y and go up the tree each one
level until y finds its position and settles at line 10. O(log n) time.

Example. Pick up the key 41 at node 10 and put it at node 1. The key 41 comes down
and we have the following.

siftup(1, 9)
The procedure insert(x) is described below. O(log n) time.

1. procedure insert(x);
2. begin
3. n:=n+1;
4. i:=n;
5. while i >= 2 do begin
6. j := i div 2;
7. y := a[j];
8. if x >= y then go to 10;
9. a[i] := y; i:=j
10. end;
11. 10: a[i] := x
12. end;

The key x is put at the end of heap and if the key value is small, we go up the path
toward the root to find a suitable place for x.
The delete operation is a combination of insert and siftup operations. O(log n) time.

1. {x=a[p] is to be deleted}
2. begin
3. n:=n-1;
4. if p <= n then
5. if a[p] <= a[n+1] then begin
6. a[p] := a[n+1];
7. siftup(p, n)
8. end else begin
9. m:=n; n:=p-1;
10. insert(a[m+1]); n:=m
11. end
12. end.

The “min” operation is easy. The minimum is at the root. O(1) time. The operation
decrease_key(p) is similar to insert and increase_key(p) is similar to siftup, where p is
the node index where the key value is changed. These take O(log n) time.
The “meld” opearation which melds two priority queues is not easy for the heap. This
is easy for the binomial heap which will be described in the next section.
The opearation build_heap is to create a heap given in the following.

1. procedure build_heap;
2. begin
3. for i:=n div 2 downto 2 do siftup(i, n)
4. end.

This procedure create a heap in a bottom-up manner.


The computing time can be shown to be O(n).
1.6.2 Binomial Queues

A binomial tree Br is recursively defined as follows:

B0 =

B1 =

Br =
Br-1

Br-1

Example B4
number of nodes at depth i

4
6

At depth i, there are nodes, where is a binomial coefficient, hence the name.

A binomial queue is a forest of binomial trees. The root of each tree in the forest is
pointed to by an array element.

Example. n=9 in a binary form is (1001)2. Then the binomial queue consits of B3 and
B0 as shown below.

a
Note that the size m of array a is m = log2 n +1. There is no ordering condition on the
keys in the roots of the binomial trees. Obviously the minimum is found in O(log n)
time. The melding of two binomial trees can be done in O(1) time by just comparing the
roots and linking them.

Example

The meld operation for two binomial queues can be implemented like a binary addition.
Let the two binomial queues to be melded be Q1 and Q2. We proceed with melding the 0-
th binomial trees , first binomial trees, second binomial trees, and so forth, with carries to
higher positions.

Example 7 = (0111)2 and 3 = (0011)2. Then 10 = (1010)2.

Q1 Q2

Since we have a carry , there are three binomial trees of size 2. An

arbitrary one can be put at position 1 and the other two are melded and carried over to the
next position.. Now there are two binomial trees of size 4, which are melded to produce a
carry to the next position. Finally we have the following.
The time for meld is O(log n).
The operation “delete_min” is described as follows: Perform find_min first. Deleting
it will cause the tree containing it to be dispersed into fragments, resulting in another
binomial queue. Now meld the original binomial queue from which the tree containing
the minimum was removed with the new binomial queue. Time is O(log n).
The operations decrease_key and increse_key can be implemented in O(log n) time,
since we can repeatedly swap the element updated with ancestors or descendants, and the
longest path length is O(log n).

The Fibonacci heap is a variation of the binomial queue, invented by Fredman and
Tarjan in 1984. The remarkable aspect of this data structure is that we can perform n
delete_min and m decrease_key operations in O(m + log n) time.

You might also like