05c-heaptree (additional note)
TRANSCRIPT
8/13/2019 05c-HeapTree (Additional Note)
http://slidepdf.com/reader/full/05c-heaptree-additional-note 1/14
Page 1
CHAPTER 3
Study of Heap Tree
8/13/2019 05c-HeapTree (Additional Note)
http://slidepdf.com/reader/full/05c-heaptree-additional-note 2/14
Page 2
Heap Structure
•
Definition - A heap structure is a binary tree that has as many nodesas possible at all levels except possibly the bottom (last) level. At the
bottom level, nodes are filled from left to right.
8/13/2019 05c-HeapTree (Additional Note)
http://slidepdf.com/reader/full/05c-heaptree-additional-note 3/14
Page 3
Height of Heap Structure• Theorem – The height of a heap structure of n
nodes is floor(log2n)
• A full binary tree of height h has
Suppose h = 4, then sum of i=0 to 4 is
1+2+4+8 = 15 = 16 -1. notes that 16 is 25
8/13/2019 05c-HeapTree (Additional Note)
http://slidepdf.com/reader/full/05c-heaptree-additional-note 4/14
Page 4
Binary Maxheaps
• Definition - A binary maxheap is a heap structure in which values
are assigned to the nodes such that the value of each node is
greater than or equal to the values of its children (if any).
• In this course we shall refer to a binary maxheap simply as a heap
• Example of heap
8/13/2019 05c-HeapTree (Additional Note)
http://slidepdf.com/reader/full/05c-heaptree-additional-note 5/14
Page 5
Implementing a heap• A heap can be easily implemented as an array
with first index 1
• The nodes of the heap are numbered from top to
bottom and at each level, from left to right
• This numbering scheme finds the of a nodeeasily: the parent of node i is node
• The numbering system finds the children of the
node easily: the left and right children of node i is
2i and 2i + 1
2
i
8/13/2019 05c-HeapTree (Additional Note)
http://slidepdf.com/reader/full/05c-heaptree-additional-note 6/14
Page 6
Node numbers and values of
Binary heap
8/13/2019 05c-HeapTree (Additional Note)
http://slidepdf.com/reader/full/05c-heaptree-additional-note 7/14Page 7
Heap Operations
•
Heap_largest() – Getting the largest value in heap
– Clearly the running time is
• Heap_delete()
– Deleting the largest value from a heap v
containing n nodes
8/13/2019 05c-HeapTree (Additional Note)
http://slidepdf.com/reader/full/05c-heaptree-additional-note 8/14Page 8
Heap Operations• The function siftdown(v, i, n) adjusts an n element array into a heap if the left
subtree and right subtree of node i are heaps
• Clearly the running time is the running time of siftdown().
• The array v represents a heap structure indexed from 1 to n. The left and right subtrees
of node i are heaps. After siftdown(v , i , n) is called, the subtree rooted at i is a heap.
8/13/2019 05c-HeapTree (Additional Note)
http://slidepdf.com/reader/full/05c-heaptree-additional-note 9/14Page 9
Heap Operations
• In the worst case the loop executestimes where h is the height of node i.
)(h
8/13/2019 05c-HeapTree (Additional Note)
http://slidepdf.com/reader/full/05c-heaptree-additional-note 10/14Page 10
Heap Insert
)(logn
This algorithm inserts the value val into a heap containing n elements. The array v represents the heap.
Input Parameters: val,v,nOutput Parameters: v,nheap_insert(val,v,n) {
i = n + 1// i is the child and i/2 is the parent.
// If i > 1, i is not the root.while (i > 1 val > v[i/2]) {
swap (v[i] and v[i/2])i = i/2 pointing to the parent
}v[i] = val
}
The value is inserted right after the last node and then bubbles up
as much as it should. In the worst case the new value must go up
from the bottom to the root of a tree of height , so
the running time is
)1(log 2 n
8/13/2019 05c-HeapTree (Additional Note)
http://slidepdf.com/reader/full/05c-heaptree-additional-note 11/14Page 11
Heapify()
This algorithm rearranges the data in the array v , indexed from 1 to n , sothat it represents a heap.
Input Parameters: v,nOutput Parameters: vheapify(v,n){
// n/2 is the index of the parent of the last nodefor i = n/2 downto 1
siftdown(v,i,n)}
• Obviously the subtrees of the last parent are heaps: the roots of these
subtress cannot be parents.• So siftdown() is called from right to left, from bottom to top, to grow the
heap.
• This way of calling siftdown(v, i, n) ensures that the left and right subtrees
of node i are heaps.
• The running time of the algorithm is , as will be shown.)(n
8/13/2019 05c-HeapTree (Additional Note)
http://slidepdf.com/reader/full/05c-heaptree-additional-note 12/14Page 12
Heapsort
An array of n elements can be sorted with heap operationsThis algorithm sorts the array v[ 1], ... , v [n ] in nondecreasing
order. It uses the siftdown and heapify algorithms
Input Parameters: v,nOutput Parameter: v
heapsort(v,n){
// make v into a heapheapify(v,n)for i = n downto 2{
// v[1] is the largest among v[1], ... , v[i].// Put it in the correct cell.swap(v[1],v[i])// Heap is now at indexes 1 through i - 1.// Restore heap.siftdown(v,1,i - 1 )
}
}
8/13/2019 05c-HeapTree (Additional Note)
http://slidepdf.com/reader/full/05c-heaptree-additional-note 13/14Page 13
Heapsort Explained
• Initially, the entire array is made into a heap.
• In between, the array is partitioned into two parts: the
front part is a heap, the rear part is an sorted arraycontaining the largest elements.
• Finally, the front part becomes null and the rear part
becomes the entire array which is sorted.
8/13/2019 05c-HeapTree (Additional Note)
http://slidepdf.com/reader/full/05c-heaptree-additional-note 14/14Page 14
Heap sort Example
2. Heapify() calls
siftdown() 3 times to
obtain the heap:
3. Before and after
siftdown(v, 1, 5):
4. Before and after siftdown(v, 1, 4):
6. A final swap(v[1], v[2])
completes the sort.
1. input
5. Before and after
siftdown(v, 1, 3):
4
12 5
12
4 5