CPS 100, Spring 1997, Tries/Graphs

These problems count as written assigment points. They are due on Wednesday, April 30, at 8:00 am.. The problems will serve as a good practice for the final. Submit electronically using
   submit100 trie README solution.cc
where you indicate how long the assignment took and with whom you collaborated in your README file. All work, code and otherwise, should be in your file solution.cc.

These problems are designed as a review of the algorithms, data structures, and techniques we have discussed to date. You must acknowledge in writing anyone with whom you check answers. You may discuss approaches, but each person should turn in an individual write-up of the problems. Please keep discussions at a high-level, i.e., discuss algorithms and methods for solving problems, but not the specific code that solves the problems.

When is a binary tree a search tree (4 points)

Write a function IsBST that evaluates to true if its parameter is a binary search tree and false otherwise. struct Tree { int info; Tree * left; Tree * right; }; bool IsBST(Tree * tree) // post: returns true if tree is a binary search tree // otherwise returns false

Complexity I (2 points)

What is the asymptotic complexity of the code you wrote in the previous problem? Support your answer.











Number Nodes (4 points)

Using the same declaration for binary trees as in the first problem, write a function LevelNumber that numbers each node in a binary tree with the node's distance from the root. The root should be numbered with zero, the root's children with distance 1, etc. You may find it useful to write another function called from LevelNumber. Do not worry about destroying the info field values already in the tree. Do not use an auxiliary queue or other structure. void LevelNumber(Tree * tree) // post: all nodes of tree "level numbered"

Trie some Times


A trie is a tree in which each node has as many (potential) children as there are characters in whatever alphabet is being used. In this problem assume that the alphabet is lower case letters 'a' -- 'z', with the 'a'-child the zero-th child (i.e., map 'a'->0, ... 'z'->25).

Tries storing words are implemented with the following declarations:

const int ALPH_SIZE = 26; struct Trie { bool isWord; // true if is a word Vector<Trie *> index; Trie(bool value=false) : index(ALPH_SIZE,0), isWord(value) {} }; The edges followed from the root to a node in a trie define a potential word. If the isWord field of a node is true, then the path to the node represents a word. For example, in the trie diagrammed below, the words a, am, ma, me, mad are shown below, as are partial paths towards words like "maze", and "ameliorate". *

The routine PrintTrie below can be used to print all the strings stored in a trie.

// define a trie and stick stuff in it Trie * t; t = InitTrie(); // now has lots of stuff in it PrintTrie(t); // print it void PrintTrie(Trie * t) { AuxPrintTrie(t,""); } void AuxPrintTrie(Trie * t, string path) // precondition: path represents word to current node in t // postcondition: all strings in t are printed { int k; if (t != 0) { if (t->isWord) // word ends at this node { cout << path << endl; } for(k=0; k < ALPH_SIZE; k++) // look at all paths from node { if (t->index[k] != 0) // path exists { AuxPrintTrie(t->index[k],path + char('a'+k)); } } } }

Problem 1 : AddToTrie (4 points)

Write a function AddToTrie that adds a string to a trie. Note: there is no recursion necessary (although recursion is ok). Traverse the trie, following a branch for each letter of the string s. If there is no branch to take, create one using new before taking it.

void AddToTrie(Trie * & t, const string & s) // pre: t is properly formed trie, // post: t updated with addition of s

Problem 2: TrieIntersect (4 points) OPTIONAL

Write a function TrieIntersect that returns a trie that is the intersection of two tries. Trie * TrieIntersect(Trie * lhs, TrieNode * rhs) All and only strings included in both lhs and rhs should be in the trie created and returned by TrieIntersect. Ideally, TrieIntersection should visit each nodes of lhs and rhs exactly once.

You might model the function on the function TrieUnion below and the hint that follows.

Trie * TrieUnion(Trie * lhs, Trie * rhs) // postconditon: returns a trie that is the union of lhs and rhs // i.e., every word in lhs or in rhs is in returned trie { Trie tempNode; Trie * ptr = 0; int k; bool somethingBelow = false; if (first == 0) return CopyTrie(second); else if (sec == 0) return CopyTrie(first); else { // make recursive calls for(k=0; k < ALPH_SIZE; k++) { if (temp.index[k] = TrieUnion(lhs->index[k],rhs->index[k])) { somethingBelow = true; // something found } } temp.isWord = first->isWord || sec->isWord; // in union? if (somethingBelow || temp.isWord) // something to return { ptr = new Trie; // make new node *ptr = temp; // copy temp to ptr } return ptr; } } Hint: assume both lhs and rhs aren't NULL, otherwise nothing is in the intersection. Make all recursive calls first; if any of the recursive calls returns a non-NULL trie, then a non-NULL trie will be returned for the intersection. Otherwise (all recursive calls return empty tries) you need to check to see if the isWord fields of both lhs and rhs are true. If both are true, the current path to lhs/rhs represents a word in the intersection, and a non-NULL trie will be returned. Both must be true for the returned trie to be marked as containing the word represented by the current path to lhs/rhs.

To summarize: make recursive calls to determine what's in the trie-intersection below, and examine isWord fields to determine if the current node/path represents a word in the intersection. Return an empty trie unless there is something below or the current node/path is in the intersection.


Graph Stuff

*

Part A (4 points)

Draw diagrams for both the adjacency list and adjacency matrix representations of the graph above.









Part B (4 points)

Write out a possible vertex ordering for both depth-first and breadth-first traversals of the graph starting at vertex 0.


Owen L. Astrachan
Last modified: Tue Apr 29 09:43:48 EDT