Title: CS 261 Data Structures
1CS 261 Data Structures
2Heap Implementation Constructors
- void buildHeap(struct vector data)
- int max vectorSize(data)
- // All nodes greater than max/2 - 1 are leaves
and thus adhere to the heap property! - for (int i max / 2 - 1 i gt 0 i--)
- adjustHeap(data, max, i) // Make subtree
rooted at i a heap. -
- At the beginning, only the leaves are proper
heaps - Leaves are all nodes with indices greater than
max / 2 - At each step, the subtree rooted at index i
becomes a heap
3Heap Implementation Build heap
- void buildHeap(struct vecctor data)
- int max vectorSize(data)
- // All nodes greater than max/2 - 1 are
leaves and thus adhere to the heap property! - for (int i max / 2 - 1 i gt 0 i--)
- adjustHeap(data, max, i) // Make
subtree rooted at i a heap. -
- For all subtrees that are are not already heaps
(initially, all inner, or non-leaf, nodes) - Call adjustHeap with the largest node index that
is not already guaranteed to be a heap - Iterate until the root node becomes a heap
- Why call adjustHeap with the largest non-heap
node? - Because its children, having larger indices, are
already guaranteed to be heaps
4Heap Implementation adjustHeap
12
First iteration adjust largest non-leaf node
(index 4)
5
7
adjustHeap
4
7
8
3
i (max/2-1)
max
9
11
10
2
3 8
11
5 7
7 9
8 11
9 10
10 2
1 7
2 5
4 3
0 12
6 4
Already heaps (leaf nodes)
5Heap Implementation adjustHeap (cont.)
12
Second iteration adjust largest non-heap node
(index 3)
5
7
adjustHeap
4
7
8
2
max
i (no adjustment needed)
9
11
10
3
3 8
11
5 7
7 9
8 11
9 10
10 3
1 7
2 5
4 2
0 12
6 4
Already heaps
6Heap Implementation adjustHeap (cont.)
12
Third iteration adjust largest non-heap node
(index 2)
5
7
adjustHeap
4
7
8
2
i
max
9
11
10
3
3 8
11
5 7
7 9
8 11
9 10
10 3
1 7
2 5
4 2
0 12
6 4
Already heaps
7Heap Implementation adjustHeap (cont.)
12
Fourth iteration adjust largest non-heap node
(index 1)
adjustHeap
4
7
5
7
8
2
max
9
11
10
3
3 8
11
5 7
7 9
8 11
9 10
10 3
1 7
2 4
4 2
0 12
6 5
Already heaps
i
8Heap Implementation adjustHeap (cont.)
12
Fifth iteration adjust largest non-heap node
(index 0 ? root)
adjustHeap
4
2
5
7
8
3
max
9
11
10
7
3 8
11
5 7
7 9
8 11
9 10
10 7
1 2
2 4
4 3
0 12
6 5
Already heaps
i
9Heap Implementation adjustHeap (cont.)
2
4
3
5
7
8
7
9
11
10
12
3 8
5 7
7 9
8 11
9 10
10 12
1 3
2 4
4 7
0 2
6 5
Already heaps (entire tree)
10Heap Implementation sort
- void heapSort(Vector data)
- buildHeap(data)
// Build initial heap. - for (int i vectorSize(data)1 i gt 0 i--)
// For each of the n elements - EleType tmp vectorGet(data,i)
// Swap last element with - vectorPut(data, I vectorGet(data, 0))
// the first (smallest) element. - vectorPut(data, 0, temp) //
So, sorts data in reverse order. - adjustHeap(data, i, 0)
// Rebuild heap property. -
-
- Sorts the data in descending order (from largest
to smallest) - Builds heap from initial (unsorted) data
- Iteratively swaps the smallest element (at index
0) with last unsorted element - Adjust the heap after each swap, but only
considers the unsorted data
11View from Middle of Execution
12Heap Analysis sort
- Execution time
- Build heap n calls to adjustHeap n log n
- Loop n calls to adjustHeap n log n
- Total 2n log n O(n log n)
- Advantages/disadvantages
- Same average as merge sort and quick sort
- Doesnt require extra space as the merge sort
does - Doesnt suffer if data is already sorted or
mostly sorted