Unformatted text preview:

Lecture 39OutlineVector Representation 1Vector Representation 2Vector Representation 3HeapHeapsort 1Heapsort 2Reheapification Downward 1Reheapification Downward 2Heapsort 3Heapsort 4ReheapifyDown 1ReheapifyDown 2MakeHeap 1MakeHeap 2MakeHeap 3In-class ExerciseMonday, April 18 CS 215 Fundamentals of Programming II - Lecture 39 1Lecture 39Log into Linux. We will be adding code to files sort.h and sort-examples.cpp from the last two classes.Homework 14 posted. Due at beginning of class next Monday as part of final exam review. No late work accepted.Questions?Monday, April 18 CS 215 Fundamentals of Programming II - Lecture 39 2OutlineMore SortingReview: Array/vector representation of binary trees (Section 10.2)Heaps (Section 11.1)Heapsort (Section 13.3)Monday, April 18 CS 215 Fundamentals of Programming II - Lecture 39 3Vector RepresentationReview: 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.Monday, April 18 CS 215 Fundamentals of Programming II - Lecture 39 4Vector RepresentationHere 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]vMonday, April 18 CS 215 Fundamentals of Programming II - Lecture 39 5Vector RepresentationThe 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)Monday, April 18 CS 215 Fundamentals of Programming II - Lecture 39 6HeapA 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.)Monday, April 18 CS 215 Fundamentals of Programming II - Lecture 39 7HeapsortThe 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.Monday, April 18 CS 215 Fundamentals of Programming II - Lecture 39 8HeapsortAssume 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 placeMonday, April 18 CS 215 Fundamentals of Programming II - Lecture 39 9Reheapification DownwardThe 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.Monday, April 18 CS 215 Fundamentals of Programming II - Lecture 39 10Reheapification DownwardThis procedure is repeated until 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]vMonday, April 18 CS 215 Fundamentals of Programming II - Lecture 39 11HeapsortThe 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]vMonday, April 18 CS 215 Fundamentals of Programming II - Lecture 39 12HeapsortThe 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)Monday, April 18 CS 215 Fundamentals of Programming II - Lecture 39 13ReheapifyDownThe 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.Monday, April 18 CS 215 Fundamentals of Programming II - Lecture 39 14ReheapifyDown1. Initialize boolean flag heap_ok to false and current to 02. While heap_ok is false and current node has a left child do 2.1 If there is no right child then 2.1.1 Set big_child_index to the left child index 2.2 Else if the left child is larger than the right child then 2.2.1 Set big_child_index to the left child index 2.3 Else 2.3.1 Set big_child_index to the right child index 2.4 If current node value is less than the value at big_child_index then 2.4.1 Swap the current node value and the value at big_child_index 2.4.2 Set current to big_child_index 2.5 Else 2.5.1 Set heap_ok to trueMonday, April 18 CS 215 Fundamentals of Programming II - Lecture 39 15MakeHeapFinally, we need 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


View Full Document

UE CS 215 - Lecture 39

Documents in this Course
Lecture 4

Lecture 4

14 pages

Lecture 5

Lecture 5

18 pages

Lecture 6

Lecture 6

17 pages

Lecture 7

Lecture 7

28 pages

Lecture 1

Lecture 1

16 pages

Lecture 5

Lecture 5

15 pages

Lecture 7

Lecture 7

28 pages

Load more
Download Lecture 39
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 39 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 39 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?