DS 9

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 38

Data Structures & Algorithms

Syed Ali Raza


Lecture 9
Priority Queues

Two kinds of priority queues:


• Min priority queue.
• Max priority queue.
Min Tree Example

4 9 3

4 8 7

9 9

Root has minimum element.


Max Tree Example

4 9 8

4 2 7

3 1

Root has maximum element.


Min Heap Definition

• complete binary tree


• min/max tree
Min Heap With 9 Nodes

4 3

6 7 9 3

8 6
Complete binary tree with 9 nodes
that is also a min tree.
Max Heap With 9 Nodes

8 7

6 7 2 6

5 1
Complete binary tree with 9 nodes
that is also a max tree.
A Heap Is Efficiently Represented As An
Array

8 7

6 7 2 6

5 1

9 8 7 6 7 2 6 5 1

0 1 2 3 4 5 6 7 8 9
Moving Up And Down A Heap
1
9
2 3
8 7
4 5 6 7
6 7 2 6

5 1
8 9
Inserting An Element Into A Max Heap

8 7

6 7 2 6

5 1 7

Complete binary tree with 10 nodes.


Inserting An Element Into A Max Heap

8 7

6 7 2 6

5 1 5
7

New element is 5.
Inserting An Element Into A Max Heap

8 7

6 7 2 6

5 1 7

New element is 20.


Inserting An Element Into A Max Heap

8 7

6 2 6

5 1 7

New element is 20.


Inserting An Element Into A Max Heap

6 8 2 6

5 1 7

New element is 20.


Inserting An Element Into A Max Heap

20

9 7

6 8 2 6

5 1 7

New element is 20.


Inserting An Element Into A Max Heap

20

9 7

6 8 2 6

5 1 7

Complete binary tree with 11 nodes


Inserting An Element Into A Max Heap

20

9 7

6 8 2 6

5 1 7

New element is 15.


Inserting An Element Into A Max Heap

20

9 7

6 2 6

5 1 7 8

New element is 15.


Inserting An Element Into A Max Heap

20

15 7

6 9 2 6

5 1 7 8

New element is 15.


Deletion from a Max Heap
20

15 7

6 9 2 6

•Delete the max value


5 1 7 8

21
Deletion from a Max Heap
20

15 7
15 7
6 9 2 6

5 1 7 8
6 9 2 6

• After the max element is


5 1 7 8 removed.
• Are we finished?

22
Deletion from a Max Heap
20

15 7
15 7
6 9 2 6

5 1 7 8
6 9 2 6

• Heap with 10 nodes.


5 1 7 8 • Reinsert 8 into the heap.

23
Deletion from a Max Heap
8 20

15 7
15 7
6 9 2 6

5 1 7 8
6 9 2 6

• Reinsert 8 into the heap.


5 1 7 • Are we finished?

24
Deletion from a Max Heap
15 20

15 7
8 7
6 9 2 6

5 1 7 8
6 9 2 6

• Exchange the position with 15


5 1 7 • Are we finished?

25
Deletion from a Max Heap
15 20

15 7
9 7
6 9 2 6

5 1 7 8
6 8 2 6

• Exchange the position with 9


5 1 7
• Are we finished?

26
Max Heap Initialization
• Heap initialization means to construct a heap by
adjusting the tree if necessary
• Example: input array = [1,2,3,4,5,6,7,8,9,10,11]

27
Max Heap Initialization
- Start at rightmost array position that has a child.

28
Max Heap Initialization

29
Max Heap Initialization

30
Max Heap Initialization

31
Max Heap Initialization

32
Max Heap Initialization

•Are we finished?
•Done!

33
Exercise
convert the following complete binary tree into max tree

34
Exercise
• The heapified tree

35
Exercise
insert 15 in to the heap

36
Exercise
• now insert 20 into the heap

37
Applications of Heaps
• Sort (heap sort)
• Machine scheduling
• Huffman codes

38

You might also like