Professional Documents
Culture Documents
Page 1
Heap Structure
Definition - A heap structure is a binary tree that has as many nodes as possible at all levels except possibly the bottom (last) level. At the bottom level, nodes are filled from left to right.
Page 2
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
Page 4
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 node i easily: the parent of node i is node 2
The numbering system finds the children of the node easily: the left and right children of node i is 2i and 2i + 1
Page 5
Page 6
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
Page 7
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.
Page 8
Heap Operations
In the worst case the loop executes (h) times where h is the height of node i.
Page 9
Heap Insert
This algorithm inserts the value val into a heap containing n elements. The array v represents the heap.
Input Parameters: val,v,n Output Parameters: v,n heap_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 log 2 ( n 1) , so the running time is (log n)
Page 10
Heapify()
This algorithm rearranges the data in the array v, indexed from 1 to n, so that it represents a heap. Input Parameters: v,n Output Parameters: v heapify(v,n) { // n/2 is the index of the parent of the last node for 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 (n) , as will be shown.
Page 11
Heapsort
An array of n elements can be sorted with heap operations This algorithm sorts the array v[1], ... , v[n] in nondecreasing order. It uses the siftdown and heapify algorithms
Input Parameters: v,n Output Parameter: v heapsort(v,n) { // make v into a heap heapify(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 ) } }
Page 12
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 array containing the largest elements. Finally, the front part becomes null and the rear part becomes the entire array which is sorted.
Page 13
Page 14