0% found this document useful (0 votes)
22 views22 pages

L11 Heap

Uploaded by

Akash Sahu
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)
22 views22 pages

L11 Heap

Uploaded by

Akash Sahu
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/ 22

The Heap Data Structure

Click to add text

Instructor: Ashok Singh Sairam


Lecture Plan
• Heap
§ Representation, Types, ….
• Operations on Heaps
§ Max-heapify
§ Build Heap
§ Heapsort
§ Priority Queue

MA512: Data Structures and Algorithms


2
Heap

A heap is a binary tree that is filled in order

8
From the heap property, it follows that:
7 4 “The root is either the maximum or the
5 2 minimum element of the heap!”

MA512: Data Structures and Algorithms


3
Array Representation of Heaps
• A heap can be stored as an array A.
§ Root of tree is A[1]
§ Left child of A[i] = A[2i]
§ Right child of A[i] = A[2i + 1]
§ Parent of A[i] = A[ i/2 ]
§ Heapsize[A] ≤ length[A]
• The elements in the subarray
A[(n/2+1) .. n] are leaves

Fig: Max heap


MA512: Data Structures and Algorithms
4
Heap Types
• Max-heaps (largest element at root), have the
max-heap property:
§ for all nodes i, excluding the root:
A[PARENT(i)] ≥ A[i]
• Min-heaps (smallest element at root), have the
min-heap property:
§ for all nodes i, excluding the root:
A[PARENT(i)] ≤ A[i]

MA512: Data Structures and Algorithms


5
Adding/Deleting Nodes
• New nodes are always inserted at the bottom
level (left to right)
• Nodes are removed from the bottom level (right
to left)

MA512: Data Structures and Algorithms


6
Operations on Heaps
• Maintain/Restore the max-heap property
§ MAX-HEAPIFY
• Create a max-heap from an unordered array
§ BUILD-MAX-HEAP
• Sort an array in place
§ HEAPSORT
• Priority queues

MA512: Data Structures and Algorithms


7
Maintaining the Heap Property
• Correct a single violation of the heap
property in a subtree’s root A[i]
§ Left and Right subtrees of i are
max-heaps
• To eliminate the violation:
§ Exchange with larger child
§ Move down the tree
§ Continue until node is not smaller
than children

MA512: Data Structures and Algorithms


8
Procedure Max-Heapify
Assumption:
i. Array
representation
ii. Assumes that A[i]
violates max-heap,
but the left and right
subtree are max
heaps

MA512: Data Structures and Algorithms


9
Example: Max-Heapify(A,2)

A[2]  A[4]

A[4] violates the heap property


A[2] violates the heap property

A[4]  A[9]

Heap property restored


MA512: Data Structures and Algorithms
10
Running Time of Max-Heapify
• Intuitively:
- h
-
- 2h
- O(h)

• Running time of MAX-HEAPIFY is O(lgn)


• Can be written in terms of the height of the heap, as being
O(h)
§ Since the height of the heap is lgn

MA512: Data Structures and Algorithms


11
Building a Heap
• Convert an array A[1 … n] into a max-heap (n = len[A])
• The elements in the subarray A[(n/2+1) .. n] are leaves
§ Each is a 1-element heap
• Apply MAX-HEAPIFY on elements between 1 and n/2
1

2 3

1 3
4 5 6 7

8
2 9 10
16 9 10

14 8 7

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

MA512: Data Structures and Algorithms


12
Example: A 4 1 3 2 16 9 10 14 8 7

1 1 1
i=5 i=4 i=3
4 4 4
2 3 2 3 2 3

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

8
2 9 10
16 9 10 8 2 9 10
16 9 10 8 14 9 10
16 9 10
14 8 7 14 8 7 2 8 7

1 1 1
i=2 i=1
4 4 16
2 3 2 3 2 3
1 10 16 10 14 10
4 5 6 7 4 5 6 7 4 5 6 7

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

MA512: Data Structures and Algorithms


13
Correctness: Build-Max-Heap

1
i=5
4
2 3

1 3
4 5 6 7

8
2 9 10
6 9 10
14 8 7

MA512: Data Structures and Algorithms


14
Correctness: Build-Max-Heap
• Maintenance:
§ For loop starts from i=��� /2⌋ and
move backwards to 1 1

§ Children of i are numbered higher 4


than i and by loop invariant they are 2 3

already root of max-heap 4


1
5 6
3
7

§ Max-heapify requires both the right 8


2 9 10
6 9 10
and left child of i to be roots of max 14 8 7
heap, in order to max i a max heap
root
• The above condition is true as i decrements in the for loop

MA512: Data Structures and Algorithms


15
Correctness: Build-Max-Heap
• Termination: At termination i =0. By loop
invariant, each node 1,2,..,n is the root of a max-
heap

MA512: Data Structures and Algorithms


16
Running Time of Build-Max-Heap
• A simple upper bound

O(lgn) O(n)

 Running time: O(nlgn)


• This is not an asymptotically tight upper bound

MA512: Data Structures and Algorithms


17
Running Time of Build-Max-Heap

Height #nodes at level i

h1 = 3 (lgn)

h2 = 2

h3 = 1

h4 = 0

MA512: Data Structures and Algorithms


18
Running Time of Build-Max-Heap: A
Tighter Bound

MA512: Data Structures and Algorithms


19
Deleting a Node
Two possibilities
• Last node - Trivial
• Any node(i) – Replace A[i] with last node; max-heapify(A,i)

MA512: Data Structures and Algorithms


20
Acknowledgement
• Dr George Bebis, Foundation Professor, Dept of
Computer Science and Engineering, University of
Nevada Reno

MA512: Data Structures and Algorithms


21
MA512: Data Structures and Algorithms
22

You might also like