Trie some Times
These problems are due electronically by 8:00 pm Wednesday, April 24
submit100 trie README solution.cc
Your work MUST be your own, there is no group collaboration for this
assignemnt.
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 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'', ``an'', ``ma'', ``me'', and ``met'' are shown as are some
non-word paths in the trie.
The routine PrintTrie
below can be used to print all the strings stored in a trie.
Trie * t;
t = InitTrie(); // now has lots of stuff in it
AllStrings(t);
void AllStrings(Trie * t)
{
AuxAllStrings(t,"");
}
void AuxAllStrings(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
{
AuxAllStrings(t->index[k],path + char('a'+k));
}
}
}
}
Problem 1 : AddToTrie (3 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 (3 points)
Write a function TrieIntersect that returns a trie that is the
intersection of two tries.
Trie * TrieIntersect(Trie * lhs, TrieNode * rhs)
All strings 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
Use the directed graph below for all these graph questions.
Part A (2 points)
Draw diagrams for both the adjacency list and adjacency matrix
representations of the graph above.
Part B (2 points )
Write out a possible vertex ordering for both depth-first and breadth-first
traversals of the graph starting at vertex 0.