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

Heap Data Structure: Zahoor Jan

The document discusses heap data structures. A heap is a complete binary tree stored in an array. Each node corresponds to an element in the array. A min-heap requires that a parent node's value is less than or equal to its children's values, while a max-heap requires the opposite. The Max-Heapify algorithm is used to restore the max-heap property when an element is inserted or removed. It recursively moves nodes down the tree if a child has a greater value than its parent.

Uploaded by

Computer Science
Copyright
© © All Rights Reserved
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)
51 views

Heap Data Structure: Zahoor Jan

The document discusses heap data structures. A heap is a complete binary tree stored in an array. Each node corresponds to an element in the array. A min-heap requires that a parent node's value is less than or equal to its children's values, while a max-heap requires the opposite. The Max-Heapify algorithm is used to restore the max-heap property when an element is inserted or removed. It recursively moves nodes down the tree if a child has a greater value than its parent.

Uploaded by

Computer Science
Copyright
© © All Rights Reserved
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/ 38

Heap Data Structure Advance Algorithms (Spring 2009)

Heap Data Structure

Zahoor Jan

Department of Computer Science University of Peshawar


Heap Data Structure Advance Algorithms (Spring 2009)

Heaps
A (binary) heap data structure is an array object that can be
viewed as a complete binary tree

Each node of the tree corresponds to an element of the array that


stores the value in the node

A complete binary tree is one where all the internal nodes have
exactly 2 children, and all the leaves are on the same level.
The tree is completely filled on all levels except possibly the
lowest, which is filled from left to right up to a point

Leaf nodes are nodes without children.


Ed
ge
  Interior node

 Leaf node
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)

Heaps
Height of a node: The number of edges starting at that node, and going
down to the furthest leaf.

Height of the heap: The maximum number of edges from the root to a leaf.

o ot
R
Height of root = 3

Height of blue
node = 1

Department of Computer Science University of Peshawar


Heap Data Structure Advance Algorithms (Spring 2009)

Heaps
Complete binary tree– if not full, then the only
unfilled level is filled in from left to right.
1 1
2 3 2 3
4 5 6 7 4 5 6

8 9 10 11 12 13 7 8 9 10 11 12

Department of Computer Science University of Peshawar


Heap Data Structure Advance Algorithms (Spring 2009)

Heaps
The heap property of a tree is a condition that must be true for
the tree to be considered a heap.

Min-heap property: for min-heaps, requires


A[parent(i)]  A[i]
So, the root of any sub-tree holds the least value in that sub-tree.

Max-heap property: for max-heaps, requires


A[parent(i)]  A[i]
The root of any sub-tree holds the greatest value in the sub-tree.

Department of Computer Science University of Peshawar


Heap Data Structure Advance Algorithms (Spring 2009)

Heaps
Looks like a max heap, but max-heap property is violated at indices 11 and 12. Why?
because those nodes’ parents have smaller values than their children.

9
2
3
8 8
4 5
6 7
8 7 8 1
8 9 10 11 12

6 7 5 8 9

Department of Computer Science University of Peshawar


Heap Data Structure Advance Algorithms (Spring 2009)

Heaps
Assume we are given a tree represented by a linear array A, and
such that i  length[A], and the trees rooted at Left(i) and
Right(i) are heaps. Then, to create a heap rooted at A[i], use
procedure Max-Heapify(A,i):
Max-Heapify(A,i) 10

1. l  Left(i) 20
9

2. r  Right(i) 19 18
6 7
3. if l  heap-size[A] and A[l] > A[i]
4. then largest  l 17 16 17 16 1

5. else largest  i
6. if r  heap-size[A] and A[r] > A[largest]
7. then largest  r
8. if largest  i
9. then swap( A[i], A[largest])
Max-Heapify(A, largest)
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)

Heaps
If either child of A[i] is greater than A[i], the greatest child
is exchanged with A[i]. So, A[i] moves down in the heap.
The move of A[i] may have caused a violation of the max-
heap property at it’s new location.
So, we must recursively call Max-Heapify(A,i) at the
location i where the node “lands”.
The above is the top-down approach of Max-Heapify(A,i)
10

20
9

19 18
6 7

17 16 17 16 1

Department of Computer Science University of Peshawar


Heap Data Structure Advance Algorithms (Spring 2009)

Max-Heapify(A,1)
10
20
20 9 10 9
19 18 6 7 19 18 6 7

17 16 17 16 1
17 16 17 16 1

20
20
10 9
19 9
19 18 6 7
10 18 6 7
17 16 17 16 1
17 16 17 16 1

20 20

19 9 19 9

10 18 6 7 17 18 6 7

17 16 17 16 1 10 16 17 16 1

Department of Computer Science University of Peshawar


Heap Data Structure Advance Algorithms (Spring 2009)

Run Time of Max-Heapify()


Run time of Max-Heapify().
How many nodes, in terms of n, is in the sub-tree with the most nodes?
2n/3
So, worst case would follow a path that included the most nodes
T(n) = T(2n/3) + (1)
Why (1)? Note that all the work in lines 1-7 is simple assignments or
comparisons, so they are constant time, or (1).

1
1 1
2 3
2 2 3
4 5
4 5 6 7

2(2) / 3 = 2 8 9 10 11
12 2(5) / 3 = 3
33 2(11) / 3 = 8
78
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)

Heaps
So, what must we do to build a heap?
We call Max-Heapify(A,i) for every i starting at last node
and going to the root.
Why Bottom-up?
Because Max-Heapify() moves the larger node upward
into the root. If we start at the top and go to larger
node indices, the highest a node can move is to the root
of that sub-tree. But what if there is a violation between
the sub-tree’s root and its parent?
So, we must start from the bottom and go towards the
root to fix the problems caused by moving larger nodes
into the root of sub-trees.

Department of Computer Science University of Peshawar


Heap Data Structure Advance Algorithms (Spring 2009)

Heaps
Bad-Build-Max-Heap(A)
1. heap-size[A]  length[A]
2. for i  length[A] downto 1
3. do Max-Heapify(A,i)

Can this implementation be improved?


Sure can!
1

2
3

4 5
6 7

8 9 10 11

Department of Computer Science University of Peshawar


Heap Data Structure Advance Algorithms (Spring 2009)

Heaps
Without going through a formal proof, notice that there is no
need to call Max-Heapify() on leaf nodes. At most, the
internal node with the largest index is at most equal to
length[A]/2.
Since this may be odd, should we use the floor or the ceiling?

In the case below, it is clear that we really need the floor


of length[A]/2 which is 5, and not the ceiling, which is 6.

Build-Max-Heap(A)
1. heap-size[A]  length[A] 1

2. for i  length[A]/2 downto 1 2 3

3. do Max-Heapify(A,i) 4 5 6 7

8 9 10 11

Department of Computer Science University of Peshawar


Heap Data Structure Advance Algorithms (Spring 2009)

Example: building a heap (1)


1 2 3 4 5 6 7 8 9 10
4 1 3 2 16 9 10 14 8 7 1
4
2 3
1 3
4 5 6 7
2 16 9 10

8 9 10
14 8 7
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)

Example: building a heap (2)


1
4
2 3
1 3
4 5 6 7
2 16 9 10

8 9 10
14 8 7
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)

Example: building a heap (4)


1
4
2 3
1 3
4 5 6 7
14 16 9 10

8 9 10
2 8 7
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)

Example: building a heap (5)


1
4
2 3
1 10
4 5 6 7
14 16 9 3

8 9 10
2 8 7
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)

Example: building a heap (6)


1
4
2 3
16 10
4 5 6 7
14 7 9 3

8 9 10
2 8 1
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)

Example: building a heap (7)


1
16
2 3
14 10
4 5 6 7
8 7 9 3

8 9 10
2 4 1
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)

Priority Queues
A priority queue is a data structure for maintaining a set S of elements, each with
an associated value called a key.

We will only consider a max-priority queue.

If we give the key a meaning, such as priority, so that elements with the highest
priority have the highest value of key, then we can use the heap structure to
extract the element with the highest priority.

Department of Computer Science University of Peshawar


Heap Data Structure Advance Algorithms (Spring 2009)

Priority Queues
Max priority queue supports the following operations:

Max-Heap-Insert(A,key): insert key into heap, maintaining heap property

Heap-Maximum(A): returns the heap element with the largest key

Heap-Extract-Max(A): returns and removes the heap element with the largest key,
and maintains the heap property

Heap-Increase-Key(A,i,key): used (at least) by


Max-Heap-Insert() to set A[i]  A[key], and then maintaining the heap property.

Department of Computer Science University of Peshawar


Heap Data Structure Advance Algorithms (Spring 2009)

Priority Queues
Heap-Maximum(A)
1. return A[1]

Heap-Extract-Max(A)
1. if heap-size[A] < 1
2. then error “heap underflow”
3. max  A[1]
4. A[1]  A[heap-size[A] ]
5. heap-size[A]  heap-size[A] - 1
6. Max-Heapify(A,1)
7. return max

Department of Computer Science University of Peshawar


Heap Data Structure Advance Algorithms (Spring 2009)

Priority Queues
Heap-Increase-Key(A,i,key)
1. if key < A[i]
2. then error “new key is smaller than current key”
3. A[i]  key
4. while i > 1 and A[Parent(i)] < A[i]
5. do exchange A[i]  A[Parent(i)]
6. i  Parent(i)

Max-Heap-Insert(A,key)
1. heap-size[A]  heap-size[A]+1
2. A[heap-size[A]]  – ∞
3. Heap-Increase-Key(A, heap-size[A], key)

Department of Computer Science University of Peshawar


Heap Data Structure Advance Algorithms (Spring 2009)

Example: increase key (1)


1
16
2 3
14 10
4 5 6 7
8 7 9 3

8 9 10
2 4 1 increase 4 to 15
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)

Example: increase key (2)


1
16
2 3
14 10
4 5 6 7
8 7 9 3

8 9 10
2 15 1
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)

Example: increase key (3)


1
16
2 3
14 10
4 5 6 7
15 7 9 3

8 9 10
2 8 1
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)

Example: increase key (4)


1
16
2 3
15 10
4 5 6 7
14 7 9 3

8 9 10
2 8 1
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)

Heap Sort
Finally, how can we get the keys sorted in an array A? We know that a heap is
not necessarily sorted as the following sequence illustrates:
A = < 100, 50, 25, 40, 30 >
Use the algorithm Heapsort():

Heapsort(A)
1. Build-Max-Heap(A)
2. for i  length[A] downto 2
3. do exchange A[1]  A[i]
4. heap-size[A]  heap-size[A]-1
5. Max-Heapify(A,1)

Build-Max-Heap() is O(n), Max-Heapify() is O(lg n), but is executed n times,


so runtime for Heapsort(A) is O(nlgn).

Department of Computer Science University of Peshawar


Heap Data Structure Advance Algorithms (Spring 2009)

Heapsort
Heapsort(A)
1. Build-Max-Heap(A)
2. for i  length[A] downto 2
3. do exchange A[1]  A[i]
4. heap-size[A]  heap-size[A]-1
5. Max-Heapify(A,1)

On line 1, we create the heap. Keep in mind that the largest element is at the
root. So, why not put the element that is currently in the root at the last index
of A[]?
We will exchange the last element in A, based on the value of heap-size[A],
with A[1], as per line 3.
Then we will reduce heap-size[A] by one so that we can make sure that
putting A[heap-size] into A[1] from line 3 doesn’t violate the heap property. But
we don’t want to touch the max element, so that’s why heap-size is reduced.
Continue in this fashion until i=2. Why don’t we care about i=1? It’s already done
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)

Example: Heap-Sort
1
16
2 3
14 10
4 5 6 7
8 7 9 3

8 9 10
2 4 1 16 14 10 8 7 9 3 2 4 1

Department of Computer Science University of Peshawar


Heap Data Structure Advance Algorithms (Spring 2009)

Example: Heap-Sort (2)


1
14
2 3
8 10
4 5 6 7
4 7 9 3

8 9 10
2 1 16
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)

Example: Heap-Sort (3)


1
10
2 3
8 9
4 5 6 7
4 7 1 3

8 9 10
2 14 16
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)

Example: Heap-Sort (4)


1
9
2 3
8 3
4 5 6 7
4 7 1 2

8 9 10
10 14 16
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)

Example: Heap-Sort (4)


1
8
2 3
7 3
4 5 6 7
4 2 1 9

8 9 10
10 14 16
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)

Example: Heap-Sort (5)


1
7
2 3
4 3
4 5 6 7
1 2 8 9

8 9 10
10 14 16
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)

Example: Heap-Sort (6)


1
4
2 3
2 3
4 5 6 7
1 7 8 9

8 9 10
10 14 16
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)

Example: Heap-Sort (7)


1
3
2 3
2 1
4 5 6 7
4 7 8 9

8 9 10
10 14 16
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)

Example: Heap-Sort (8)


1
2
2 3
1 3
4 5 6 7
4 7 8 9
8 9 10
10 14 16
1 2 3 4 7 8 9 10 14 16
Department of Computer Science University of Peshawar

You might also like