CS 415, Section 001 | Sonoma State University | Spring, 2023 |
Algorithm Analysis
|
||
Instructor: Henry M. Walker
Lecturer, Sonoma State University |
Although much of this course is well developed, some details can be
expected to evolve as the semester progresses.
Any changes in course details will be announced promptly during class.
This assignment is in three parts: Heaps, Heap Sort and Horner's Rule.
This section is divided into
A max-heap is a binary tree with the property that the value at each node is larger (according to some ordering) than the values stored in either child. Similarly, a min-heap is a binary tree with the property that the value at each node is smaller (according to some ordering) than the values stored in either child.
Further, we restrict our attention to trees which are completely balanced. All nodes have 2 children, except at the bottom of the tree. The last row of the tree may not be full, but any items on the last level are moved left as much as possible. For example, the following tree illustrates the shape of a tree and the ordering of values within the tree for a max-heap.
To identify the nodes in a tree, the simplest approach is to number the nodes level-by-level from the root downward. Within a level, nodes are numbered left to right. Also, for simplicity, many textbooks assign the index 1 to the root node.
In examining the label nodes, a pattern emerges: for each node labeled i, the left child has label 2*i and the right node has label 2*i+1.
Note: As previously noted, some textbooks, such as Data Structures and Problem Solving Using Java, Fourth Edition by Mark Allen Weiss. uses this numbering scheme in writing code, with the top node numbered 1. If one considers an array element 0, Weiss suggests filling that position with -∞ for a min-heap or ∞ for a max-heap.
If we start labeling at 0 rather than 1, the labeling of nodes becomes:
In the context of 0-based labeling, identify a pattern for moving from a node with label j to its left child and its right child. What labels would be found on the left and right nodes of a node with label j?
Since nodes in a heap fit a straight forward identification system, there is a natural mapping between the logical nodes in a heap and the indexed elements of an array.
In class we considered how to insert an item into a heap and maintain the structure. In each case, we first place the item at the end of a tree (as far left as possible in the last row). The item then is worked up from its position, until the data in the tree are appropriately ordered.
The following heap repeats the structure given at the beginning of this lab:
Using this structure as a start and following the standard procedure to add an element to a heap, insert the values 27, 46, 40, 95, and 85. Show the structure of the tree and the values in each node after each insertion.
In class, we also considered how to remove the top-priority item from a heap: remove the root as the item to be returned, move the last item from the end of the heap to the root, and work that item down in the heap until the data are properly ordered.
The Heap Sort draws upon the heap data structure, which are
discussed in Section 6.4 of the textbook. The key
procedure percDown
has the following specification:
/** ******************************************************************************* * percDown function * * @param array the array to be made into a heap, starting at hold * * @param hole: the node index (or base) of subtree for start of processing * * @param size the size of the array * * @pre all nodes in left and right subtrees of the hole node are heaps * * @post all nodes in the tree from the hole node downward form a heap * *********************************************************************************/ void percDown(int array [ ], int hole, int size)
Implement the percDown
procedure.
Notes
With procedure percDown
, a Heap Sort algorithm uses
the following code:
** ******************************************************************************* * heap sort, main function * * @param a the array to be sorted * * @param n the size of the array * * @post the first n elements of a are sorted in non-descending order * *********************************************************************************/ void heapSort (int a [ ], int n) { // Build Heap for (int i=n/2 ; i>=0 ; i-- ) { percDown(a, i, n); } for (int i=n-1 ; i>0 ; i-- ) { int tmp = a[0]; a[0] = a[i]; a[i] = tmp ; // deleteMax percDown( a, 0, i); // Maintain heap ordering property } }
Additional details may be found in Levitin's text, Section 6.4.
Using the percDown
and heapSort
procedures, write an entire program that
For the Heap Sort part of the assignment, turn in both a listing of the complete program (including both Steps 4 and 5) and the output produced by running the code.
A polynomial function has the form
p(x) = anxn + an-1xn-1 + ... + a2x2 + a1x + a0
Write a function compute_poly that takes three parameters:
and returns the value of the polynomial p(x).
Notes:
int a = {1, 0, 0, 5};then the polynomial being evaluated is 5x3 + 1, NOT x3 + 5.
For this part of the assignment, turn in both a listing of the complete program and the output produced by running the code.
created December 1, 2018 revised December 2, 2018 revised December 27-30, 2021 revised February 4, 2022 reformatted and heap material added July 28, 2022 reorganized with brute force/merge sort added October 3-6, 2022 revised December 30, 2022 reorganized and revised Summer, 2023 |
|
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |
Copyright © 2011-2022
by Henry M. Walker.
|