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

Heaps

Hashing

Uploaded by

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

Heaps

Hashing

Uploaded by

degagaalemayehu5
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/ 31

PRIORITY QUEUES – BINARY HEAPS

1
RECALL QUEUES
 FIFO: First-In, First-Out

 Some contexts where this seems right?

 Some contexts where some things should be


allowed to skip ahead in the line?

2
QUEUES THAT ALLOW LINE JUMPING
 Need a new ADT
 Operations: Insert an Item,
Remove the “Best” Item

6 2
15 23
insert deleteMin
12 18
45 3 7
3
PRIORITY QUEUE ADT
1. PQueue data : collection of data with priority

2. PQueue operations
 insert
 deleteMin

3. PQueue property: for two elements in the


queue, x and y, if x has a lower priority value
than y, x will be deleted before y

4
APPLICATIONS OF THE PRIORITY
QUEUE
 Select print jobs in order of decreasing length
 Forward packets on routers in order of urgency

 Select most frequent symbols for compression

 Sort numbers, picking minimum first

 Anything greedy

5
POTENTIAL IMPLEMENTATIONS

insert deleteMin
Unsorted list (Array) O(1) O(n)
Unsorted list (Linked-
O(1) O(n)
List)
Sorted list (Array) O(n) O(1)*

Sorted list (Linked-List) O(n) O(1)


6
RECALL FROM LISTS, QUEUES,
STACKS
 Use an ADT that corresponds to your needs

 The right ADT is efficient, while an overly


general ADT provides functionality you aren’t
using, but are paying for anyways

 Heaps provide O(log n) worst case for both


insert and deleteMin, O(1) average insert

7
BINARY HEAP PROPERTIES
1. Structure Property
2. Ordering Property

8
BRIEF INTERLUDE: SOME DEFINITIONS:
A Perfect binary tree – A binary tree with all leaf
nodes at the same depth. All internal nodes
have 2 children.

height h
11 2h+1 – 1 nodes
2h – 1 non-leaves
5 21 2h leaves
2 9 16 25
1 3 7 10 13 19 22 30

9
HEAP STRUCTURE PROPERTY
A binary heap is a complete binary tree.
Complete binary tree – binary tree that
is completely filled, with the possible
exception of the bottom level, which is
filled left to right.
Examples:

10
REPRESENTING COMPLETE
BINARY TREES IN AN ARRAY

1 A From node i:
2 3
B C
4 5 6
F
7
G left child:
D E
8 9 10 11 12 right child:
H I J K L
parent:

implicit (array) implementation:


A B C D E F G H I J K L
0 1 2 3 4 5 6 7 8 9 10 11 12 13
11
HEAP ORDER PROPERTY
Heap order property: For every non-root node
X, the value in the parent of X is less than (or
equal to) the value in X.

10
10
20 80
20 80
40 60 85 99
30 15
50 700
not a heap
12
HEAP OPERATIONS
 findMin:
 insert(val): percolate up.

 deleteMin: percolate down.

10

20 80

40 60 85 99

50 700 65

13
HEAP – INSERT(VAL)
Basic Idea:
1. Put val at “next” leaf position
2. Percolate up by repeatedly exchanging
node until no longer needed

14
INSERT: PERCOLATE UP
10

20 80

40 60 85 99

50 700 65 15

10

15 80

40 20 85 99
50 700 65 60
15
INSERT CODE (OPTIMIZED)
void insert(Object o) { int percolateUp(int hole,
Object val) {
assert(!isFull());
while (hole > 1 &&
size++; val < Heap[hole/2])
newPos = Heap[hole] = Heap[hole/2];
hole /= 2;
percolateUp(size,o); }
Heap[newPos] = o; return hole;
}
}

16
HEAP – DELETEMIN

Basic Idea:
1. Remove root (that is always the min!)
2. Put “last” leaf node at root
3. Find smallest child of node
4. Swap node with its smallest child if needed.
5. Repeat steps 3 & 4 until no swaps needed.

17
DELETEMIN: PERCOLATE DOWN

10

20 15

40 60 85 99

50 700 65

15

20 65

40 60 85 99

50 700 18
DELETEMIN CODE (OPTIMIZED)
Object deleteMin() { int percolateDown(int hole,
assert(!isEmpty()); Object val) {
while (2*hole <= size) {
returnVal = Heap[1];
left = 2*hole;
size--; right = left + 1;
newPos = if (right ≤ size &&
percolateDown(1, Heap[right] < Heap[left])
target = right;
Heap[size+1]); else
Heap[newPos] = target = left;
Heap[size + 1];
if (Heap[target] < val) {
return returnVal;
Heap[hole] = Heap[target];
} hole = target;
}
else
break;
19
}
return hole;
}
BUILDING A HEAP
12 5 11 3 10 6 9 4 8 1 7 2

20
BUILDING A HEAP
 Adding the items one at a time is O(n log n)
in the worst case

21
WORKING ON HEAPS
 What are the two properties of a heap?
 StructureProperty
 Order Property

 How do we work on heaps?


 Fix the structure
 Fix the order

22
BUILDHEAP: FLOYD’S METHOD
12 5 11 3 10 6 9 4 8 1 7 2

Add elements arbitrarily to form a complete tree.


Pretend it’s a heap and fix the heap-order property!

12

5 11

3 10 6 9

23
4 8 1 7 2
BUILDHEAP PSEUDOCODE

private void buildHeap() {


for ( int i = currentSize/2; i > 0; i-- )
percolateDown( i );
}

runtime:

24
BUILDHEAP: FLOYD’S METHOD

12

5 11

3 10 6 9

4 8 1 7 2

25
BUILDHEAP:
12 FLOYD’S METHOD

5 11

3 10 2 9

4 8 1 7 6

26
BUILDHEAP:
12 FLOYD’S METHOD12

5 11 5 11

3 10 2 9 3 1 2 9

4 8 1 7 6 4 8 10 7 6

27
BUILDHEAP:
12 FLOYD’S METHOD12

5 11 5 11

3 10 2 9 3 1 2 9

4 8 1 7 6 4 8 10 7 6
12

5 2

3 1 6 9
28

4 8 10 7 11
BUILDHEAP:
12 FLOYD’S METHOD12

5 11 5 11

3 10 2 9 3 1 2 9

4 8 1 7 6 4 8 10 7 6
12 12

5 2 1 2

3 1 6 9 3 5 6 9
29

4 8 10 7 11 4 8 10 7 11
FINALLY…

3 2

4 5 6 9

12 8 10 7 11

30
PROBLEMS
 Build Heap Using the following sequences
show the necessary steps 15, 7, 12, 28,
36, 1, 37, 13, 4, 25, 3, 9, 27, 2, 5, 16

31

You might also like