0% found this document useful (0 votes)
3 views

Heaps

A heap is a complete binary tree that satisfies the heap-order property, where each node's key is either greater than or equal to (max-heap) or less than or equal to (min-heap) its children's keys. Basic operations on heaps include insertion, deletion, and reheap operations to maintain the heap structure. Heaps can be implemented using arrays, and heap sort is a method that utilizes heaps to sort elements efficiently.

Uploaded by

abc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Heaps

A heap is a complete binary tree that satisfies the heap-order property, where each node's key is either greater than or equal to (max-heap) or less than or equal to (min-heap) its children's keys. Basic operations on heaps include insertion, deletion, and reheap operations to maintain the heap structure. Heaps can be implemented using arrays, and heap sort is a method that utilizes heaps to sort elements efficiently.

Uploaded by

abc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

HEAPS

Basic Concepts
A heap is a binary tree having the following properties:
1. It is a complete binary tree, that is, each level of the tree is completely filled, except
the bottom level, where it is filled from left to right.

2. It satisfies the heap-order property, that is, the key value of each node is greater than
or equal to the key value of its children, or the key value of each node is lesser than or
equal to the key value of its children.

All the binary trees of Fig. 1 are heaps, whereas the binary trees of Fig. 2 are not.

The second condition is violated in Fig. 2(a) as the content of the child node 80 is greater
than its parent node 70. The first condition is violated in Fig. 2(b) as at level 2, 30 has a
right child but no left child, that is, at this level, it should be filled from left to right.

70 50

65 50 30 17 10

13 9 30 40 5 7

5
(a) (b) (c)

Fig. Sample heaps (a) Heap with height three


(b) Heap with height two (c) Heap with height one

83 40

70 50 30 14

10 80 30 7 20

(a) (b)

Fig. Binary trees but not heaps (a) Sample 1(b) Sample 2

1
->Min-heap and Max-heap: There are two types of heaps,
the min-heap and the max-heap.
Min-heap: The structure shown in Fig.1 is called min-heap.
In min-heap, the key value of each node is lesser than or equal to the key value of its
children. In addition, every path from root to leaf should be sorted in ascending order.
Figure 2 is an example of a min-heap.

Data
1

2 5

All ≥ Data All ≥ Data


10 7 9

Fig. 1 Structure of min-heap Fig. An example of a min-heap

In addition, every path from the root to leaf should be sorted in descending order. Figure
2 is an example of a max-heap.

Data
9

8 4

All ≤ Data All ≤ Data


6 2 3

Fig. A max-heap Fig. An example of a max-heap

2
IMPLEMENTATION OF HEAP 9

To implement heaps using array is an easy task. We


simply number the nodes in the heap from top to bot- 8 4
tom, number the nodes on each level from left to right,
th th
and store the i node in the i location of the array.
The root of the tree is stored at index 0, its left child at 6 2 3
index 1, its right child at index 2, and so on.
For example, consider the Fig . Fig. Sample heap
Figure shows the corresponding array representation of the heap.

Data 9 8 4 6 2 3

Index 0 1 2 3 4 5 6 7

Fig Array representation of heap in Fig above

In this array,

1. parent of the node at index i is at index (i - 1)/2

2. left child of the node at index i is at index 2 × i + 1

3. right child of the node at index i is at index 2 × i + 2

For example, in Fig.above,

1. The node having value 8 is at the 1st location.


2. Its parent is at 0/2, that is, at the 0th location (value is 9).
3. Its left child is at 2 × 1 +1, that is, at the 3rd location (value is 6).
4. Its right child is at 2 × 1 + 2, that is, at the 4th location (value is 2).

HEAP AS ABSTRACT DATA TYPE:-


A heap is a complete binary tree, which satisfies the heap-order property, that is, the key
value of each node is greater than or equal to the key value of its children (or the key
value of each node is lesser than or equal to the key value of its children). The basic
operations on heap are insert, delete, max-heap, and min-heap.
ADT Heap

1. Create()Heap
2. Insert(Heap, Data)Heap

3
3. DeleteMaxVal(Heap)Heap
4. ReHeapDown(Heap, Child)Heap
5. ReHeapUp(Heap,Root)Heap
6. End

-> Operations on Heaps:

The basic operations on heaps are listed as follows:


1. Create—creates an empty heap to which the root points

2. Insert—inserts an element into the heap

3. Delete—deletes max (or min) element from the heap

4. ReheapUp—rebuilds the heap when we use the insert() function

5. ReheapDown—rebuilds the heap when we use the delete() function

A heap is generally not traversed, searched, or printed. To implement the insert and
delete operations, we need two other operations: reheapUp and reheapDown. The
advanced operations include merge, which merges two heaps.

ReheapUp
If we have a nearly complete binary tree with n elements, the first n  1 elements satisfy
the order property of heaps, but the last element does not. That is, the structure would be
a heap if the last element was not there. The reheapUp operation repairs the structure so
that it is a heap by lifting the last element up the tree until that element reaches a proper
position in the tree.

As a heap is a complete or nearly complete tree, the node must be placed in the last leaf
level at the first leftmost empty position as in the above Fig. If the new node’s key is
larger than its parent, it is lifted up the tree by exchanging the child and parent keys and
the data. The data eventually moves to the correct position in the heap by repeatedly
exchanging child–parent keys and data. In brief, reheapUp repairs a broken heap by
lifting the last element up the tree until it reaches the correct location in the heap.

The following Figure shows a general heap structure.

Below fig. shows a tree which is not a heap after adding 36.

4
53

32 43

27 23 31 41

24 26 21 36

Fig. A tree, not a heap

Here, 36 is greater than its parent, 23; hence, it is an invalid heap. We therefore exchange
36 and 23 and call reheapUp to test its current position in the heap. We obtain the tree as
shown in Fig.

53

32 43

27 36 31 41

24 26 21 23

Fig. 36 moved up

Once again, 36 is greater than its parent, 32. Therefore, we again exchange the data and
find that when reheapUp is called, the node is placed at the correct position, and hence,
the operation stops. We get the heap as shown in Fig.

53

36 43

27 32 31 41

24 26 21 23

Fig. A heap after 36 is moved up

5
ReheapDown

When we have a nearly complete binary tree that satisfies the heap-order property except
in the root position, we need the reheapDown operation. Such situations occur when the
root is deleted from the tree, leaving two disjointed heaps. To correct such situations, we
move the data in the last tree node to the root. Obviously, such actions disturb the tree’s
heap properties. To restore the heap, we need an operation that will sink the root down
until the heap ordering property is satisfied and thus the operation reheapDown comes
into action. The following Figure shows a reheapDown operation.

Let us consider a broken heap as in Fig.

21

32
43

27 23 31 41

24 26

Fig. Original tree, not a heap

Here, the root 21 is smaller than its subtrees. We examine them and select the larger of
the two to exchange it with the root, which is now 43.

Having made the exchange, as in Fig. 12.18, we check whether 21 is smaller than its keys

43

32 21

27 23 31 41

24 26

Fig. Root 21 moved down to the right

6
Once again, we exchange 21 with the larger subtree 41 and get the tree as in Fig. 12.19.

43

32 41

27 23 31 21

24 26

Fig. 12.19 21 moved down again yielding a heap

Insert
A node can be inserted in a heap which has already been built, if there is an empty
location in the array. To insert a node, we need to search the first empty leaf in the array.
We find it immediately after the last node in the tree. To insert a node, we move the new
data to the first empty leaf and perform reheapUp. Let us consider the heap already built
as in below Fig.

Let us consider the heap already built as in Fig. 12.20.

98

76 52

65 28 43 39

87
To be inserted
Heap

98 76 52 65 28 43 39 87

Fig. 12.20 Sample heap

7
The heap in Fig. 12.20 has seven elements in it. Let us consider that the element 87 is to
be inserted. Initially, 87 is stored at the last empty location as the first empty leaf of the
heap. Thus, we heapify it to store the element in the proper position. The resultant heap is
shown in Fig. 12.21.

98

87 52

76 28 43 39

65

Heap

98 87 52 76 28 43 39 65

Fig. 12.21 Heap after insertion of 87


Delete
While removing a node from a heap, the most common and meaningful logic is to delete
the root. The heap is thus left without a root. To reconstruct the heap, we move the data
in the last heap node to the root and perform reheapDown. Let us consider the heap tree
as shown in the following Fig. The data at the top of the heap is returned by the delete
operation.

80

69 34

58 10 25 21

47

80 69 34 58 10 25 21 47 ...

Fig. 12.22 Sample heap

8
69
When the delete operation is performed for the heap in Fig. 12.22,
it returns the element 80 at the root. In the delete operation, 47
58 34
(the last node value) is placed at the root value. Now,
reheapDown is per-formed again to reconstruct a heap. The
reconstructed heap is shown in Fig. 12.23. 47 10 25 21

Fig. 12.23 Reconstructed heap


after deletion of 80

9
Creating a Heap
The unsorted keys are taken sequentially one at a time and added into a heap. The size of
the heap grows with the addition of each key. The ith key (ki) is added into an existing
heap of size i  1 and a heap of size i is obtained. Initially, the node is placed in the heap
of size i  1 in such a way that an almost complete constraint is satisfied. The value of ki
is then compared with its parent’s key value. If k i is greater, the contents of the newly
added node and that of the parent’s node are exchanged. This process continues until
either ki is at the root node or the parent’s key value is not less than ki. The final tree is a
heap of size i.
For example, consider the following unsorted list of keys.
8, 20, 9, 4, 15
Figures 12.26(a)–(j) show the building of a heap using this list of keys.

(a)

8 20 20

20 8 8 9

(b) (c)

20 20 20

8 9 8 9 15 9

4 4 15 4 8

(d) (e)
HEAP SORT:-
The steps for building heap sort are as follows:
1. Build the heap tree.
2. Start deleteHeap operations, storing each deleted element at the end of the heap
array.

After performing step 2, the order of the elements will be opposite to that in the heap tree.
Hence, if we want the elements to be sorted in ascending order, we need to build the heap
tree in descending order—the greatest element will have the highest priority. Note that
we use only one array, treating its parts differently.

10
1. When building the heap tree, a part of the array will be considered as the
heap, and the remaining part will be the original array.

2. When sorting, a part of the array will be the heap, and the remaining part will
be the sorted array.
Consider the array 13, 17, 11, 6, 15, 8 as an example for heap sort. Using this example,
let us illustrate both the steps for heap sort.
Build heap tree The given array is represented as a tree, complete, but not ordered.

17

15 11

6 13 8

17 15 11 6 13 8

The following steps illustrate sorting by performing the deleteHeap operation till the heap
is empty.
Delete top element 17 The following steps illustrate the deletion of element 17.

Step 1: Store 17 in a temporary place. A hole is created at the top as shown in the
following figure.

15 11

6 13 8

15 11 6 13 8

17

11
Step 2: Swap 17 with the last element of the heap. As 8 will be adjusted in
the heap, its cell will no longer be a part of the heap. Instead, it becomes a
cell from the sorted array.

Sorted array
Heap
15 11 6 13 17

Step 3: Penetrate down the hole (8 is less than 15, so it cannot be inserted in
the previous hole).

15 8

11

6 13

Step 4: Penetrate once more (as 8 is less than 13, here also it cannot be
inserted in the previous hole).

15 8

13 11

15 13 11 6 17

12
Now, 8 can be inserted in the hole.

15

13 11

6 8

15 13 11 6 8 17

Delete top element 15 The following steps illustrate the deletion of the
top element, 15.
Step 1: Store 15 in a temporary place. A hole is created at the top.

13 11

6 8

13 11 6 8 17

15

Step 2: Swap 15 with the last element of the heap. As 8 will be adjusted in
the heap, its cell will no longer be a part of the heap. Instead, it becomes a
cell from the sorted array.

13 11

13 11 6 15 17

13
Step 3: Penetrate down the hole, as 8 is less than 13.
13 8

11

Step 4: This is the representation of the heap after the penetration.


13

8 11

13 8 11 6 15 17

Delete top element 13 The following steps illustrate the deletion of element 13.
Step 1: Store 13 in a temporary place. A hole is created at the top.

8 11

8 11 6 15 17

13

Step 2: Swap 13 with the last element of the heap. As 6 will be adjusted in the heap, its
cell will no longer be a part of the heap. Instead, it becomes a cell from the sorted array.
6

8 11

8 11 13 15 17

14
Step 3: Penetrate down the hole, as 6 is less than 11.

11 6

Step 4: The heap looks like this after the penetration.

11

8 6

11 8 6 13 15 17

Delete top element 11 The following steps illustrate the deletion of the top element 11.
Step 1: Store 11 in a temporary place. A hole is created at the top.

8 6

8 6 13 15 17

11

Step 2: Swap 11 with the last element of the heap. As 6 will be adjusted in the heap, its cell will
no longer be a part of the heap. Instead, it becomes a cell from the sorted array.

8 11 13 15 17

Step 3: Penetrate down the hole, as 6 is less than 8.

8 6

15
Step 4: The heap looks like this after the penetration.

8 6 11 13 15 17

Delete top element 8 The following steps illustrate the deletion of the top element 8.
Step 1: Store 8 in a temporary place. A hole is created at the top.

6 11 13 15 17

Step 2: Swap 8 with the last element of the heap. As 6 will be adjusted in the heap, its cell will
no longer be a part of the heap. Instead, it becomes a cell from the sorted array.

8 11 13 15 17

Step 3: The heap looks like this after the penetration.

6 8 11 13 15 17

Delete top element 6 The following steps illustrate the deletion of the top element 6.
Step 1: Store 6 in a temporary place. A hole is created at the top.

16
6 8 11 13 15 17

Empty heap Now, the heap is empty, so we stop and finally get the sorted array.

6 8 11 13 15 17

17

You might also like