CS 415, Section 001 Sonoma State University Spring, 2023
 
Algorithm Analysis
Instructor: Henry M. Walker

Lecturer, Sonoma State University
Professor Emeritus of Computer Science and Mathematics, Grinnell College

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.

Assignment on Heaps, Sorting and Using Algebra to Improve Efficiency

This assignment is in three parts: Heaps, Heap Sort and Horner's Rule.

Heaps

This section is divided into

Discussion

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.

typical heap

A Simple Labeling of Nodes

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.

standard labeling

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.

detail for standard labeling

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.

0-based Labeling of Nodes

If we start labeling at 0 rather than 1, the labeling of nodes becomes:

0-based labeling
  1. 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?

detail for 0-based labeling

Connecting a Heap with an Array

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.

Insertion into a Heap

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.

  1. The following heap repeats the structure given at the beginning of this lab:

    typical heap

    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.

Deletion from a Heap

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.

  1. Starting with the original heap from the previous problem, use the standard procedure for deleting items from a heap to remove four items in sequence. Show the structure of the tree and the values in each node after each deletion.

Heap Sort

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)
  1. 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.

  1. 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.

Horner's Rule

  1. 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:

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
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.
ccbyncsa.png

Copyright © 2011-2022 by Henry M. Walker.
Selected materials copyright by Marge Coahran, Samuel A. Rebelsky, John David Stone, and Henry Walker and used by permission.
This page and other materials developed for this course are under development.
This and all laboratory exercises for this course are licensed under a Creative Commons Attribution-NonCommercial-Share Alike 4.0 International License.