Advanced Programming II, Fall 2001

Programming Assignment 1 - An Example Solution


Table of Contents

Introduction

This problem and the solution presented here is based on PORTPREP: A Portable Repeated String Finder by L. Jones, E. Gassie, and S. Radhakrishnan, Software - Practice and Experience, January, 1989 (Vol. 19, No. 1), Pages 64-77.

Design

Implementation

There may be zero, one, or two command-line arguments. Each node stores a word. The word itself is stored in word, while a lower-case version of the word is stored in key to simplify comparisons.

The nodes are arranged in an n-ary tree using the sibling-child pointer representation. This representation has the advantage of needing only two pointers per node, but yet is capable of representing a parent with an arbitrary number of children. The cost of this representation is a slight increase in complexity when traversing the tree.

The root of the tree holds the empty word "". A node n at level i > 0 represents the last word of an i-tuple; the whole tuple can be found by traversing the tree back to the root. The value n.count is the number of times the i-tuple has appeared so far.

As promised, the root of the tree holding the tuples represents the empty string. The node pointer in level[i] points to the last word of the current i-tuple in the tree; level[0] points to the root of the tree. level contains max_tuple_size elements. junk is a little hack to give elements in level something to point to until max_tuple_size words have been read, at which point all elements in level point into tuple_tree. junk is not used thereafter. Initialize the tuple counter to count upto max-tuples; max must be positive. Given nptr, a pointer to a node, and the word w, return a pointer to the child of *nptr that has w as its key. If *nptr doesn't have such a child, make one and return a pointer to the new child. Append the word w to the current set of tuples, increasing the count of each new tuple by one. After the new tuples have been counted, adjust the tuple set by tossing out the largest tuple and adding the smallest tuple. Print to the output stream outs the count and tuples for all min- to max-tuples collected so far. Print to the output stream outs the count and tuples for all min- to max-tuples collected so far. Free the node *np, after deleting *np's children and siblings. This procedure eat stack space as if it were candy, but it's simple to write and understand. Free all the nodes allocated to count the tuples. Return a copy of s in which all letters are lower case.

Index


This page last modified on 27 October 2001.