The Binary Heap

30
The Binary Heap

description

the binary heap

Transcript of The Binary Heap

Page 1: The Binary Heap

The Binary Heap

Page 2: The Binary Heap

Binary Heap• Looks similar to a binary search tree

• BUT all the values stored in the subtree rooted at a node are greater than or equal to the value stored at the node.– In a min heap that is.– We will mainly talk about min heaps, but it’s the same concept.

Page 3: The Binary Heap

Binary Heap

• What are they used for?– Priority Queue– Performing Heapsort

Page 4: The Binary Heap

Binary Heap

• Priority Queue– A queue where you always extract the item with

the highest priority next.– What operations do we need?• An efficient deleteMin() and Insert()• For our implementation we will achieve O(log n) time

for the deleteMin() and Insert() operations.

Page 5: The Binary Heap

Binary Heap• Balanced binary tree– Can even store a binary heap in an array instead of

an actual binary tree.– The children of node i are nodes 2i and 2i+1.

• Consider:

• Draw the tree• What node is the parent of node i?– Floor(i/2)

10 20 30 40 50 60 70 80 90 100 110 120 130

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

Page 6: The Binary Heap

Binary Tree

10 20 30 40 50 60 70 80 90 100 110 120 130

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

10

20

40

80 90 100 110

50

30

60

120 130

70

Page 7: The Binary Heap

Heap Operation : Insert

• Insert into the next open spot in the heap, or the next array location.– This will keep the heap balanced.

• However, in all likelihood this is not where the element belongs– Thus we have to do the Percolate Up procedure– For example, if you inserted 25 into the next open

spot, it would be out of order.

Page 8: The Binary Heap

Percolate Up

• If the parent of the new node is greater than the inserted value, swap them.– This is a single percolate up step.

• Continue this process until the inserted node’s parent stores a number lower than it.

• Since the height of the tree is O(lg n), this is an O(lg n) operation.

• What does this remind you of?

Page 9: The Binary Heap

Insert

• Consider the tree we had before– Insert 65

• Where do you insert it?10

20

40

80 90 100 110

50

30

60

120 130 25

70

Now what?

Page 10: The Binary Heap

Insert

• Consider the tree we had before– Insert 65

• Where do you insert it?10

20

40

80 90 100 110

50

30

60

120 130 25

70

Page 11: The Binary Heap

Insert

• Consider the tree we had before– Insert 65

• Where do you insert it?10

20

40

80 90 100 110

50

30

60

120 130

25

70

Page 12: The Binary Heap

Insert

• Consider the tree we had before– Insert 65

• Where do you insert it?10

20

40

80 90 100 110

50 60

120 130 70

25

30

Page 13: The Binary Heap

Heap Operation: deleteMin

• First part is easy– Just return the value stored in the root.

• Then what do we replace it with?– Place the “last” node in the vacated root.– Probably not the correct location.– Percolate Down

Page 14: The Binary Heap

Percolate Down

• Compare the element to its children.– If one of the children is less than the node, swap

the lowest.• This is a single percolate down step.– CONTINUE until this “last” node has children with

larger values than it.

• Real life example

Page 15: The Binary Heap

Heap Operation: Heapify

• Heapify or Bottom-Up Heap Construction• How to construct a heap out of unsorted

elements.1) Place all the unsorted elements in a complete binary

tree.2) Going through the nodes in backwards order, and

skipping the leaf nodes, run Percolate Down on each of these nodes.• Notice that each subtree below any node that has already

run Percolate Down is already a heap.• So after we run Percolate Down on the root, the whole

tree is a heap.

Page 16: The Binary Heap

Heapify• Let’s run Heapify on the following elements:

80 110 90 30 60 120 40 10 50 100 70 20 130

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

80

110

30

10 50 100 70

60

90

120

20 130

40

Where do we start?

120

Page 17: The Binary Heap

Heapify• Let’s run Heapify on the following elements:

80 110 90 30 60 20 40 10 50 100 70 120 130

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

80

110

30

10 50 100 70

60

90

130

40

120

20

Where do we look next?

60

Page 18: The Binary Heap

Heapify• Let’s run Heapify on the following elements:

80 110 90 30 60 20 40 10 50 100 70 120 130

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

80

110

30

10 50 100 70

60

90

130

4020

Where do we look next?

120

30

Page 19: The Binary Heap

Heapify• Let’s run Heapify on the following elements:

80 110 90 30 60 20 40 10 50 100 70 120 130

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

80

110

50 100 70

60

90

130

4020

Where do we look next?

12030

10

90

Page 20: The Binary Heap

Heapify• Let’s run Heapify on the following elements:

80 110 90 30 60 20 40 10 50 100 70 120 130

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

80

110

50 100 70

60

20

130

40

12030

10 90

Page 21: The Binary Heap

Heapify• Let’s run Heapify on the following elements:

80 110 90 30 60 20 40 10 50 100 70 120 130

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

80

110

50 100 70

60

20

130

40

12030

10 90

Where do we look next?110

Page 22: The Binary Heap

Heapify• Let’s run Heapify on the following elements:

80 110 90 30 60 20 40 10 50 100 70 120 130

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

80

10

50 100 70

60

20

130

40

12030

10 90110

Page 23: The Binary Heap

Heapify• Let’s run Heapify on the following elements:

80 110 90 30 60 20 40 10 50 100 70 120 130

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

80

10

50 100 70

60

20

130

40

120

90

110

30

Page 24: The Binary Heap

Heapify• Let’s run Heapify on the following elements:

80 110 90 30 60 20 40 10 50 100 70 120 130

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

80

10

50 100 70

60

20

130

40

120

90

110

30

Where do we look next?

80

Page 25: The Binary Heap

Heapify• Let’s run Heapify on the following elements:

80 110 90 30 60 20 40 10 50 100 70 120 130

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

10

80

50 100 70

60

20

130

40

120

90

110

30

Page 26: The Binary Heap

Heapify• Let’s run Heapify on the following elements:

80 110 90 30 60 20 40 10 50 100 70 120 130

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

10

50 100 70

60

20

130

40

120

90

110

80

30

Page 27: The Binary Heap

Heapify• Let’s run Heapify on the following elements:

80 110 90 30 60 20 40 10 50 100 70 120 130

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

10

100 70

60

20

130

40

120

90

110

30

80

50

Page 28: The Binary Heap

Heapify• Why can we NOT go through the nodes in forward order?

• What’s the running time of the algorithm to create a heap?– In a heap with n nodes, we run Percolate Down on n/2 of

those nodes.– The max number of steps of any Percolate Down is?

O(log n)– Thus an upper bound for Make-Heap is

O(n log n)• We can do a more careful analysis for a tighter bound for

the running time.

Page 29: The Binary Heap

Make-Heap Analysis

• On the board.

Page 30: The Binary Heap

Heap Sort

• Now that we know the basic operations to perform on a heap, we can use these to sort values using a heap.

• Basic Idea:1) Insert all items into a heap2) Extract the minimum items n times in a row storing the values

sequentially in an array.• What is the run time of Heap Sort?– Since each insert and extraction takes O(lg n) time.– This sort works in O(n lg n) time.

• Let’s do an example.