Lecture 40OutlineVector Representation 1Vector Representation 2Vector Representation 3HeapHeapsort 1Heapsort 2Reheapification Downward 1Reheapification Downward 2Heapsort 3Heapsort 4ReheapifyDown 1ReheapifyDown 2MakeHeap 1MakeHeap 2MakeHeap 3In-class ExerciseWednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 1Lecture 40Log into Linux. We will be adding code to files sort.h and sort-examples.cpp from the last two classes.Questions?Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 2OutlineMore SortingReview: Array/vector representation of binary trees (Section 10.2)Heaps (Section 11.1)Heapsort (Section 13.3)Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 3Vector RepresentationReview: A binary tree is said to be complete, if every level except the deepest level is full and the nodes in the deepest level are as far left as possible.When a binary tree is complete, we can use a simple array/vector representation by numbering the nodes starting at the root and going from left to right at each level and then top to bottom. Call this number i. Then we store each node's value in an array at index i.Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 4Vector RepresentationHere is a picture:4527 4221 23 22 3519 4 502654138 9745 27 42 21 23 22 35 19 4 5[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]vWednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 5Vector RepresentationThe root is always at v[0].Suppose that the data for a node appears in v[i]. The locations of the parent and children node can be computed. For a non-root node, the parent is always located at v[(i-1)/2] (using integer division)The children (if they exist) are located at v[2i+1] (left child) and v[2i+2] (right child)Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 6HeapA heap is a complete, binary tree of elements that can be compared using a less-than operator (<). For each node, the node's value is never less than its children's node values. The previous example tree is a heap.(Note: this meaning of "heap" is unrelated to the heap used in dynamic memory allocation.)Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 7HeapsortThe heapsort algorithm is based on the following idea:Interpret the vector to be sorted as a binary tree. Arrange the elements so that the resulting binary tree is a heap.Repeat the following steps until all values are in their correct places:The root of a heap is the largest value in the tree, so we can place it in its correct place by swapping it with the last element of the tree and reducing the size of the tree by one node.The resulting tree is not a heap, but can be made into one easily.Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 8HeapsortAssume we have a heap, here is the first step:The red portion of the vector is sorted. The remaining part of the vector is a heap except for the root element. Need to reposition the root element.527 4221 23 22 3519 4 455 27 42 21 23 22 35 19 4 45[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]vout of placeWednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 9Reheapification DownwardThe process of repositioning the out-of-place root element is called reheapification downward.It begins by comparing the root element with its children's values. If one or both children are larger, then the root element is swapped with the larger of the two children. (Why the larger?) In the example, 5 swaps with 42.This moves the "problem" down one level.Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 10Reheapification DownwardThis procedure is repeated until there the value reaches a leaf, or no children are larger. In the example, 5 swaps with 35 and we're done.4227 521 23 22 3519 4 4542 27 5 21 23 22 35 19 4 45[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]vout of place4227 3521 23 22 519 4 4542 27 35 21 23 22 5 19 4 45[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]vWednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 11HeapsortThe second pass does the following:427 3521 23 22 519 42 454 27 35 21 23 22 5 19 42 45[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]vout of place3527 421 23 22 519 42 4535 27 4 21 23 22 5 19 42 45[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]vout of place3527 2221 23 4 519 42 4535 27 22 21 23 4 5 19 42 45[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]vWednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 12HeapsortThe Heapsort function template receives and passes back a vector, v, to be sorted. The algorithm is:1. Convert the vector into a heap using MakeHeap(v)2. Initialized unsorted to v.size( ).3. While unsorted is greater than 1 do 3.1 Decrement unsorted 3.2 Swap v[0] with v[unsorted] 3.3 Restore the unsorted side to a heap using ReheapifyDown (v, unsorted)Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 13ReheapifyDownThe reheapification process is encapsulated in the ReheapifyDown function template. This function receives and passes back a vector, v, and receives the number of elements, n, in the tree to be adjusted. The algorithm is on the next slide.Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 14ReheapifyDown1. Initialize boolean flag heapOK to false and current to 02. While heapOK is false and current node has a left child do 2.1 If there is no right child then 2.1.1 Set bigChildIndex to the left child index 2.2 Else if the left child is larger than the right child then 2.2.1 Set bigChildIndex to the left child index 2.3 Else 2.3.1 Set bigChildIndex to the right child index 2.4 If current node value is less than the value at bigChildIndex then 2.4.1 Swap the current node value and the value at bigChildIndex 2.4.2 Set current to bigChildIndex 2.5 Else 2.5.1 Set heapOK to trueWednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 15MakeHeapFinally, we need is a procedure to convert an arbitrary vector into a heap.The main idea is to "add" node values one at a time to the "end" of the tree and position the new node value by pushing it up the tree until it is larger than its children. This process is called reheapification upwards.The MakeHeap function template receives and passes back a vector, v, that is converted into a
View Full Document