HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of...

30
HEAPS

description

Def. Heap - a binary tree that is: completely filled in reading from left to right across each level, except the last level may not be full, and the key in each node is greater than (or equal to) both of its children’s keys. Note Since the greatest key is always at the top, a heap is a good data structure to implement a priority queue. Although removal is only O(log n) time, insertion is also O(log n) - better than arrays and linked lists.

Transcript of HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of...

Page 1: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

HEAPS

Page 2: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

Review: what are the requirements of the abstract data type: priority queue?

Quick removal of item with highest priority (highest or lowest key value, depending on application.).

What data structures may be used to implement a priority queue? What are the advantages/disadvantages of each?

Arrays: removal quick but insertion slow O(log n) to find position but O(n) for shiftsLinked lists: removal quick but insertion slow - O(n) to find position and O(1) to relink.

Page 3: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

Def. Heap - a binary tree that is:

completely filled in reading from left to right across each level, except the last level may not be full, and

the key in each node is greater than (or equal to) both of its children’s keys.

Note Since the greatest key is always at the top, a heap is a good data structure to implement a priority queue. Although removal is only O(log n) time, insertion is also O(log n) - better than arrays and linked lists.

Page 4: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

Note: The highest priority key is always accessible at the top, but removing it produces a hole, which takes the same no. of moves as the height of the tree to fill.

Page 5: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

Although we visualize a heap as a linked binary tree,

we usually implement it as an array.

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

100 75 50 60 40 45 30

60 40 45 30

100

75 50

55 15 20 35 25

55 15 20 35 25

Page 6: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

Remember the children of node n are stored in locations: 2n + 1 and 2n + 2 if the first object is

stored in location zero in the array but in 2n and 2n+1 when the first object is stored in location one in the array and the first location is left empty.

Question: what is the location of the parent of the child at location x? (x - 1)/2 or x/2 if zero is left empty Question: does a heap provide easy searching? no

Also, a heap does not provide easy traversal of nodes in order.

But, a heap provides easy removal of the highest key and easy insertion - both O(log n) using the “trickleup()” and “trickledown()” methods - all that is needed to implement a priority queue.

Page 7: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

“trickledown()” method needed for removal:

•After copying node with highest priority, replace it with the last node in the heap.

•Now the tree is not a heap! so re-heap

• Swap the new root node with the greater of its children.

•Repeat that process down to the lowest level.

•Remove the root node from the previous sample.

Page 8: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

Num 100

60 40 45 30

75 50

55 15 20 35 25

25

Page 9: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

60 40 45 30

25 50

55 15 20 35 25

75Num 100

Page 10: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

25 40 45 30

60 50

55 15 20 35 25

75Num 100

Page 11: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

55 40 45 30

60 50

25 15 20 35 25

75Num 100

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

75 60 50 55 40 45 30 25 15 20 35 25

NOTE: the last item is not really removed, only the length of the array is decremented.

Page 12: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

public Node remove() // delete item with max key { // (assumes non-empty list) Node root = heapArray[0]; heapArray[0] = heapArray[--currentSize]; trickleDown(0); return root; } // end remove()

Page 13: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

public void trickleDown(int index) { int largerChild; Node top = heapArray[index]; // save root while(index < currentSize/2) // while node has at { // least one child, int leftChild = 2*index+1; int rightChild = leftChild+1; // find larger child if(rightChild < currentSize && // (rightChild exists?) heapArray[leftChild].iData < heapArray[rightChild].iData) largerChild = rightChild; else

//what is passed to index? 0

Page 14: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

largerChild = leftChild; // top >= largerChild? if(top.iData >= heapArray[largerChild].iData) break; // shift child up heapArray[index] = heapArray[largerChild]; index = largerChild; // go down } // end while heapArray[index] = top; // root to index } // end trickleDown()

Page 15: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

“trickleup()” method is needed for insertion:•Insert new node at bottom of tree (at heap[heap.length] then increment length.

•Compare with parent and swap if needed.

•Repeat until root node is reached.

Page 16: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

55 40 45 30

60 50

25 15 20 35

75

90

Page 17: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

55 40 90 30

60 50

25 15 20 35

75

45

Page 18: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

55 40 50 30

60 90

25 15 20 35

75

45

Page 19: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

55 40 50 30

60 75

25 15 20 35

90

45

Page 20: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

------------------------------------------------------------- public boolean insert(int key) { if(currentSize==maxSize) return false; Node newNode = new Node(key); heapArray[currentSize] = newNode; trickleUp(currentSize++); return true; } // end insert()

Page 21: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

public void trickleUp(int index) { int parent = (index-1) / 2; Node bottom = heapArray[index];

while( index > 0 && heapArray[parent].iData < bottom.iData ) { heapArray[index] = heapArray[parent]; // move it down index = parent; parent = (parent-1) / 2; } // end while heapArray[index] = bottom; } // end trickleUp()

Page 22: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

Note: trickledown() and trickleup() require the most work for removal and insertion, but that is never more than the no. of levels of the tree.

no.node min no.levels height12345678910111213141516

1223333444444445

0112222333333334

Note: height = log2 n

=(int) log2 n

No. levels = height + 1

Page 23: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

Heapsort - another use of the adt heap.The heap provides a simple and efficient sorting algorithm:

•Insert items from an unordered array into a heap then

•Remove from the heap and replace in the original array:

for (j = 0; j < size; j++)

theHeap.insert( anArray[j]);

for (j = 0; j < size; j++)

anArray[j] = theHeap.remove();

Since insert and remove operate in O(log n) time and each must be executed for every node in the array (n) the heapsort operates in O(n*log n ) time.

What other sorts run in that time? Quicksort & mergesort

Page 24: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

Question: what is the disadvantage of the heapsort?Space requirement: 2 arrays needed

This heapsort algorithm space requirement can be improved by placing the random data into a heap within the same array then:1. Remove top and hold temporarily.

2. Trickledown() value that was put in place of top.

3. Copy temp to position of last value that had been put in place of top.

Question: In what order is the data after this heapsort?

Answer: ascending order.

Page 25: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

To convert an array of random data to a heap within the array itself you may use an iterative approach or a recursive method. Iteratively this would be:

for (j = size/2 - 1; j >= 0; j--)

theHeap.trickleDown(j)

Where in the binary tree is the item with index size/2 - 1?

The rightmost node that has children. (Remember: the number of leaves = number of internal nodes +1.) Starting there to convert the random array into a heap with the array itself, trickledown is called about half as many times as it is in the insert() method.

Page 26: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

45 60 50 75 55 40

0 1 2 3 4 5Random array:

Start trickledown here

45 60 40 75 55 50

45 75 50 60 55 40

(only 1 swap needed this time)

45 75 50 60 55 40

Page 27: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

75 45 50 60 55 40

The array is now a heap.

45 75 50 60 55 40

0 1 2 3 4 5

75 60 50 45 55 40

Page 28: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

75 60 50 45 55 40

Use the remove() method and insert() at bottom to sort within the array itself.

Now trickledown()

60 40 50 45 55 75

40 60 50 45 55 75

0 1 2 3 4 5

60 55 50 45 40 75

Remember remove decrements heap size.

Page 29: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

60 55 50 45 40 75

Remove biggest and insert at bottom (of what is left of heap)

40 55 50 45 60 75trickledown()

55 40 50 45 60 75

40 45 50 55 60 75

Remove biggest and insert at bottom (of what is left of heap)

55 45 50 40 60 75

trickledown() 50 45 40 55 60 75

Page 30: HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

50 45 40 55 60 75

Remove biggest and insert at bottom (of what is left of heap)

40 45 50 55 60 75

trickledown()45 40 50 55 60 75

Remove biggest and insert at bottom (of what is left of heap)

40 45 50 55 60 75

No trickledown() needed