Title: HEAPS
1HEAPS
2Review 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/disadvant
ages of each?
Arrays removal quick but insertion slow O(log
n) to find position but O(n) for shifts
Linked lists removal quick but insertion slow -
O(n) to find position and O(1) to relink.
3Def. 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 childrens 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.
4Note 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.
5Although we visualize a heap as a linked binary
tree,
we usually implement it as an array.
100
25
75 50
60 40
45 30
55 15
20 35
6Remember 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 2n1
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.
7- 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.
8(No Transcript)
9(No Transcript)
10(No Transcript)
1175
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.
12 public Node remove() // delete item
with max key //
(assumes non-empty list) Node root
heapArray0 heapArray0
heapArray--currentSize trickleDown(0)
return root // end remove()
13//what is passed to index?
0
public void trickleDown(int index)
int largerChild Node top
heapArrayindex // save root
while(index lt currentSize/2) // while node
has at
// least one child, int
leftChild 2index1 int rightChild
leftChild1
// find larger child if(rightChild lt
currentSize // (rightChild exists?)
heapArrayleftChild.iData lt
heapArrayrightChild.i
Data) largerChild rightChild
else
14 largerChild leftChild
// top gt largerChild?
if(top.iData gt heapArraylargerChild.iData)
break
// shift child up heapArrayindex
heapArraylargerChild index
largerChild // go down //
end while heapArrayindex top
// root to index // end trickleDown()
15trickleup() method is needed for insertion
- Insert new node at bottom of tree
(at heapheap.length then increment length. - Compare with parent and swap if needed.
- Repeat until root node is reached.
16(No Transcript)
17(No Transcript)
18(No Transcript)
19(No Transcript)
20 -------------------------------------------------
------------ public boolean insert(int key)
if(currentSizemaxSize)
return false Node newNode new
Node(key) heapArraycurrentSize
newNode trickleUp(currentSize)
return true // end insert()
21public void trickleUp(int index)
int parent (index-1) / 2 Node bottom
heapArrayindex while( index gt 0
heapArrayparent.iData lt bottom.iData )
heapArrayindex
heapArrayparent // move it down
index parent parent (parent-1) /
2 // end while
heapArrayindex bottom // end
trickleUp()
22Note 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 height
123456789
1223333444444445
0112222333333334
10111213141516
(int) log2 n
No. levels height 1
23Heapsort - 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 lt size j)
theHeap.insert( anArrayj) for (j 0 j lt
size j) anArrayj 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(nlog n )
time.
Quicksort mergesort
What other sorts run in that time?
24Question 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.
25To 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 gt 0 j--) theHeap.trickle
Down(j)
Where in the binary tree is the item with index
size/2 - 1?
26 (only 1 swap needed this time)
27The array is now a heap.
28Use the remove() method and insert() at bottom to
sort within the array itself.
Now trickledown()
29Remove biggest and insert at bottom (of what is
left of heap)
trickledown()
Remove biggest and insert at bottom (of what is
left of heap)
trickledown()
30Remove biggest and insert at bottom (of what is
left of heap)
trickledown()
Remove biggest and insert at bottom (of what is
left of heap)
No trickledown() needed