1 Heaps. 2 Background: Priority Queues. zQueues are a First-In, First-Out data structure; zPriority...

32
1 Heaps
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    213
  • download

    0

Transcript of 1 Heaps. 2 Background: Priority Queues. zQueues are a First-In, First-Out data structure; zPriority...

1

Heaps

2

Background: Priority Queues.

Queues are a First-In, First-Out data structure;

Priority Queues are similar, except those of highest priority must be removed first.

How should we implement this?

3

Priority Queue Implementation

Arrays Inserts: O(logn) to find spot; O(n) to move

all others down. Deletes: O(1) to remove; O(n) to move

others.Linked Lists :

Inserts: O(n) to find spot; O(1) to do insert. Deletes: O(1) to remove.

4

Priority Queue Implementation

AVL-Trees Inserts: O(logn) to find spot; O(logn) to do

rotation(s). Deletes: O(logn) to remove; O(logn) to do

rotations.Heaps :

Inserts: O(1) to find spot; O(logn) to do insert. Deletes: O(1) to find; O(logn) to delete/rotate.

5

Heaps Vs. AVL-Trees

AVL-Trees have a lot of overhead to balance.

AVL-Trees store the values in order.

Heaps only provide access to the least/greatest.

This requires less overhead and is easier to implement.

6

What is a Heap?BINARY MIN HEAP is a complete binary

tree in which each node is empty or has a key value that is less than both of its children. Both children are Binary Min Heaps

A COMPLETE BINARY TREE is a binary tree where the root is either empty OR TL is a perfect tree of height h-1 and TR is a complete tree of height h-1 OR TL is a complete tree of height h-1 and TR is a

perfect tree of height h-2

7

More Definitions

A PERFECT TREE is one where the root is either empty or has HL – HR = 0 and both sub trees are also perfect trees.

i.e. the last level (and all levels) are completely filled.

8

Heap as a Tree

As a tree, inserts would fill a level entirely, left to right before going on to the next level:

A

B C

D E F G

9

Heap Insert Algorithm

The previous slide discussed where the new node would go, but how to maintain the order of the heap?

Algorithm: Compare new node to parent; if the new

node is greater than the parent, stop. Else, swap new node and parent. Repeat

algorithm at parent (percolate up).

10

Heap Example

Now let’s do an extended example, inserting a series of priority values into a heap.

We will be inserting 90, 50, 25, 10, 15, 20, 75.

Let’s start with 90:

11

Heap Example

90 is no problem. Now, insert 50:

90

Left to insert: 50, 25, 10, 15, 20, 75

12

Heap Example

Does this require a swap? Yes:

90

50

Left to insert: 25, 10, 15, 20, 75

13

Heap Example

Done. Now insert 25:

50

90

Left to insert: 25, 10, 15, 20, 75

14

Heap Example

Swap?Yes:

50

90

Left to insert:10, 15, 20, 75

25

15

Heap Example

Now insert 10:

25

90

Left to insert:10, 15, 20, 75

50

16

Heap Example

Swap?Yes:

25

90

Left to insert:15, 20, 75

50

10

17

Heap Example

Swap again?Yes:

25

10

Left to insert:15, 20, 75

50

90

18

Heap Example

Done. Now insert 15:

10

25

Left to insert:15, 20, 75

50

90

19

Heap Example

Swap?Yes:

10

25

Left to insert:20, 75

50

90 15

20

Heap Example

Swap again?No. 15 is where it needs to be. Now 20:

10

15

Left to insert:20, 75

50

90 25

21

Heap Example

Swap?Yes:

10

15

Left to insert:75

50

90 25 20

22

Heap Example

Swap again?No. 20 is good. Finally, insert 75:

10

15

Left to insert:75

20

90 25 50

23

Heap Example

Swap?No. 75 is good; we are done.

10

15

Left to insert:done

20

90 25 50 75

24

Deleting from a Heap

The minimum value is at the root of the tree; thus, it can be found easily, but how do we rebalance?

Algorithm: Remove Root’s value as minimum value Replace root with last node; delete last. If root is less than both its children, stop. Else, swap root and min child; repeat from

min child node (percolate down).

25

Heap Delete Example

First, 10 must be removed and replaced with 75:

10

15 20

90 25 50 75

26

Heap Delete Example

Is 75 less than both of its children?No. Swap with minimum (15):

75

15 20

90 25 50

27

Heap Delete Example

Is 75 less than both of its children?No. Swap with minimum (25):

15

75 20

90 25 50

28

Heap Delete Example

75 is at the terminal level; done.

15

25 20

90 75 50

29

Implementation Problem

The problem with the tree/pointer representation is how to maintain the “next” pointer.

I think this can be done by keeping a counter and manipulating it to decide left or right from the root.

But there is another way:

30

Array Implementation

Any binary tree may be represented in an array.

The only questions really are Where are the two children of a node? Where is a node’s parent?

Answer (for a node at position i): The two children are at 2*i and 2*i+1 The parent is at i div 2.

31

Array Example:

For this tree:

Here’s the array:

10

15 20

90 25 50 75

15

2

10

1

25

5

90

4

20

3

75

7

50

6

32

The End Slide