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

Lecture 22 Heap

A heap is a complete binary tree data structure that maintains a specific order property, either as a max heap (parent nodes are greater than or equal to their children) or a min heap (parent nodes are less than or equal to their children). It can be efficiently implemented using an array, where the relationships between parent and child nodes can be calculated using simple index formulas. Key operations on heaps include insertion, deletion, and heapifying an arbitrary array, all of which maintain the heap properties.

Uploaded by

rai6sul
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)
5 views

Lecture 22 Heap

A heap is a complete binary tree data structure that maintains a specific order property, either as a max heap (parent nodes are greater than or equal to their children) or a min heap (parent nodes are less than or equal to their children). It can be efficiently implemented using an array, where the relationships between parent and child nodes can be calculated using simple index formulas. Key operations on heaps include insertion, deletion, and heapifying an arbitrary array, all of which maintain the heap properties.

Uploaded by

rai6sul
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/ 47

Heap

11/05/08 1
Heap
A heap is a data structure that stores a
collection of objects (with keys), and has the
following properties:
– Complete Binary tree
– Heap Order

It is implemented as an array where each node


in the tree corresponds to an element of the
array.
Heap
• A heap is a complete binary tree except the bottom level adjusted to the left.
• The value of each node is greater than that of its two children. (Max Heap)
• The value of each node is less than that of its two children. (Min Heap)
• Height of the heap is log2n.
• Example

1 1
0 0
0 0
2 1 2 1
1 6 1 7
1 1 1 1 1 1
4 9 7 4 9 6

Figure: Not a Heap Figure: A Heap


Heap
• The binary heap data structures is an array that can be viewed as a
complete binary tree. Each node of the binary tree corresponds to
an element of the array.
• The array is completely filled on all levels except possibly lowest.

1
9

1 1
2 6

1 4 7

19 12 16 1 4 7

Array A
What is a Heap?
A heap is also known as a priority queue and
can be represented by a binary tree with the
following properties:
Structure property: A heap is a completely filled binary
tree with the exception of the bottom row, which is filled from
left to right

Heap Order property: For every node x in the heap,


the parent of x greater than or equal to the value of x.
(known as a maxHeap).
Definition
• Max Heap
– Store data in ascending order
– Has property of
A[Parent(i)] ≥ A[i]
• Min Heap
– Store data in descending order
– Has property of
A[Parent(i)] ≤ A[i]
Max Heap Example
1
9

1 1
2 6

1 4 7

19 12 16 1 4 7

Array A
Min heap example
1

4 16

7 12 19

1 4 16 7 12 19

Array A
Heap
• The root of the tree A[1] and given index i of a node, the indices of
its parent, left child and right child can be computed

PARENT (i)
return floor(i/2)
LEFT (i)
return 2i
RIGHT (i)
return 2i + 1
Array implementation
For any node i, the following formulas apply:
The index of its parent = i / 2

5 Index of left child = 2 * i


3 Index of right child = 2 * i + 1
4 2
4 5
1 2 1 1
5 1 3 8
1
3 5 7
2

53 44 25 15 21 13 18 3 12 5 7

0 1 2 3 4 5 6 7 8 9 10 11
Heap Implementation
• We can use an array (due to the regular structure or completeness of binary tree).

• For a node N with location i, the following factors can be calculated.

1. Left child of N is in location (2 * i).

2. Right child of N is in location (2 * i + 1).

3. Parent of N is in location [i/2].

• Example
1
0
1 2 3 4 5 6 7 8 9 0
10 2 1
21 17 14 19 16
0 1 7
1 1 1
4 9 6
Figure: Heap and Its Array Representation
Insertion
Algorithm
1. Add the new element to the next available position at the
lowest level
2. Restore the max-heap property if violated
General strategy is percolate up (or bubble up):
if the parent of the element is smaller than the
element, then interchange the parent and child.

OR

Restore the min-heap property if violated


General strategy is percolate up (or bubble up):
if the parent of the element is larger than the
element, then interchange the parent and child.
The heap property
• A node has the heap property if the value in the
node is as large as or larger than the values in its
children
1 1 1
2 2 2
1 1
8 3 8 8
2 4
Blue node has Blue node has Blue node does not
heap property heap property have heap property
• All leaf nodes automatically have the heap property
• A binary tree is a heap if all nodes in it have the
heap property
siftUp
• Given a node that does not have the heap property, you
can give it the heap property by exchanging its value with
the value of the larger child
1 1
2 4
1 1
8 8
4 2
Blue node does not Blue node has
have heap property heap property

• This is sometimes called sifting up


• Notice that the child may have lost the heap property
Constructing a heap I
• A tree consisting of a single node is automatically a heap
• We construct a heap by adding nodes one at a time:
– Add the node just to the right of the rightmost node in the deepest
level
– If the deepest level is full, start a new level
• Examples:

Add a new Add a new


node here node here
Constructing a heap II
• Each time we add a node, we may destroy the heap property of its parent
node
• To fix this, we sift up
• But each time we sift up, the value of the topmost node in the sift may
increase, and this may destroy the heap property of its parent node
• We repeat the sifting up process, moving up in the tree, until either
– We reach nodes whose values don’t need to be swapped (because the
parent is still larger than both children), or
– We reach the root
Constructing a heap III
1 1
8 8
0 0
1
8 8 5
0
1 2 3

1 1 1
0 0 2
1 1
8 5 5 5
2 0
1
8 8
2 4
Other children are not affected
1 1 1
2 2 4
1 1 1
5 5 5
0 4 2
1 1 1
8 8 8
4 0 0

• The node containing 8 is not affected because its parent gets


larger, not smaller
• The node containing 5 is not affected because its parent gets larger,
not smaller
• The node containing 8 is still not affected because, although its
parent got smaller, its parent is still greater than it was originally
1 1
9 9

1 1 1 1
2 6 2 6

4 7 1 4 7 17
1
Insert 17
1
9

1 1
2 7 swap

1 4 7 16

Percolate up to maintain the


heap property
Heap Operations
(1) Insertion
(2) Delete-max or delete-min
(3) Make-heap (organize an arbitrary array as a heap)
Insertion
• Find the left-most open position and insert the new element NewElement.
• while ( NewElement > Its Parent ) exchange them.
• Example:
9 9 9
7 7 7
8 8 8
8 5 8 5 8 7
5 5 0
4 7 5
8 7 0 5 4
4
0 8 8

Figure: Item 70 is inserted.


Algorithm: INSHEAP(Heap, N, Item)
1. Set N := N +1 and Ptr := N Here,
2. Repeat steps 3 to 6 while Ptr > 1
Heap = Max heap
3. Set Par := Ptr/2
4. If Item < Heap[Par], then N = Total no. of items in the array

Set Heap[Ptr] := Item and Return. Item = New item to be inserted


1. Set Heap[Ptr] := Heap[Par]
2. Set Ptr := Par
3. Heap[1] := Item
4. Return.

The Complexity Analysis of Insert Operation


• Insertion takes O( log2n ) times.
• Reasons: 1) No. of operations = O( height of the heap)
2) Height of the heap: O ( log2 n )
Deletion
• Delete max
– Copy the last number to the root ( overwrite the maximum
element stored there ).
– Restore the max heap property by percolate down.

• Delete min
– Copy the last number to the root ( overwrite the minimum
element stored there ).
– Restore the min heap property by percolate down.
A sample heap
• Here’s a sample binary tree after it has been heapified
2
5
2 1
2 7

1 2 1 1
9 2 4 5
1 1 2 1
3 9
8 4 1 1

• Notice that heapified does not mean sorted


• Heapifying does not change the shape of the binary tree;
this binary tree is balanced and left-justified because it
started out that way
Removing the root
• Notice that the largest number is now in the root
• Suppose we discard the root:1
1
2 1
2 7

1 2 1 1
9 2 4 5
1 1 2 1
3 9
8 4 1 1
• How can we fix the binary tree so it is once again balanced
and left-justified?
• Solution: remove the rightmost leaf at the deepest level
and use it for the new root
The reHeap method I
• Our tree is balanced and left-justified, but no longer a heap
• However, only the root lacks the heap property
1
1
2 1
2 7

1 2 1 1
9 2 4 5
1 1 2
3 9
8 4 1

• We can siftUp() the root


• After doing this, one and only one of its children may have
lost the heap property
The reHeap method II
• Now the left child of the root (still the number 11) lacks
the heap property
2
2
1 1
1 7

1 2 1 1
9 2 4 5
1 1 2
3 9
8 4 1

• We can siftUp() this node


• After doing this, one and only one of its children may have
lost the heap property
The reHeap method III
• Now the right child of the left child of the root (still the
number 11) lacks the heap property:
2
2
2 1
2 7

1 1 1 1
9 1 4 5
1 1 2
3 9
8 4 1

• We can siftUp() this node


• After doing this, one and only one of its children may have
lost the heap property —but it doesn’t, because it’s a leaf
The reHeap method IV
• Our tree is once again a heap, because every node in it has
the heap property
2
2
2 1
2 7

1 2 1 1
9 1 4 5
1 1 1
3 9
8 4 1

• Once again, the largest (or a largest) value is in the root


• We can repeat this process until the tree becomes empty
• This produces a sequence of values in order largest to smallest
Delete-Max
• Replace the root node by the last node at bottom level maintaining completeness.

• While ( new root < its children ) exchange it with the greater of its children.

• Example:

5 2 4 4
0 1 0 0
4 2 4 2 2 2 3 2
0 3 0 3 1 3 0 3
1 3 2 2 1 3 2 1 3 2 1 2 2
8 0 0 1 8 0 0 8 0 0 8 1 0

Figure: Item 50 (root) is deleted.


Algorithm: DELHEAP(Heap, N, Item)
1. Set Item := Heap[1] and Set Last:=Heap[N] and N:=N-1

2. Set Ptr := 1, Left := 2 and Right := 3

3. Repeat steps 5 to 6 while Right<=N

4. If Last >= Heap[Left] and Last >= Heap[Right] then

Set Tree[Ptr] := Last and Return.

5. If Heap[Right] <= Heap[Left] then

Set Heap[Ptr] := Heap[Left] and Ptr := Left.

Else Set Heap[Ptr] := Heap[Right] and Ptr := Right

6. Set Left := 2 * Ptr and Right := Left + 1

1. If Left = N and if Last < Heap[Left] then Set Heap[Ptr] := Heap[Left] and Ptr := Left.

2. Set Heap[Ptr] := Last

3. Return
Heap Sort
• The heapsort algorithm consists of two phases:
- build a heap from an arbitrary array
- use the heap to sort the data
• To sort the elements in the decreasing order, use a min heap
• To sort the elements in the increasing order, use a max heap

1
9

1 1
2 6

1 4 7
Example of Heap Sort
Take out
19
biggest

12 16
Move the last
element
to the root
1 4 7

Sorted
Array
:
A
12 16 1 4 7 19
7
swap
HEAPIFY
() 12 16

1 4

Sorted
Array
:
A
7 12 16 1 4 19
16

12 7

1 4

Sorted
Array
:
A
16 12 7 1 4 19
Take out
16
biggest
Move the last
element
to the root 12 7

1 4

Sorted
Array
:
A
12 7 1 4 16 19
4

12 7

Sorted
Array
:
A
4 12 7 1 16 19
swap 4

HEAPIFY
() 12 7

Sorted
Array
:
A
4 12 7 1 16 19
12

4 7

Sorted
Array
:
A
12 4 7 1 16 19
Take out
biggest 12
Move the last
element to the
root 4 7

Sorted
Array
:
A
4 7 1 12 16 19
1
swap

4 7

Sorted
Array
:
A
1 4 7 12 16 19
7

4 1

Sorted
Array
:
A
7 4 1 12 16 19
Take out
biggest 7
Move the last
element to the
4 1 root

Sorted
Array
:
A
1 4 7 12 16 19
swap 1

HEAPIFY
() 4

Sorted
Array
:
A
4 1 7 12 16 19
Take out
Move the last biggest 4
element to the
root
1

Sorted
Array
:
A
1 4 7 12 16 19
Take out
1 biggest

Sorted
Array
:
A
1 4 7 12 16 19
Sorted
:

1 4 7 12 16 19
END!!!

11/05/08 Shaily Kabir,Dept. of CSE, DU 47

You might also like