2 1 consists of nodes each of which contains a string

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ; "c" � � � � � � � � "an" � � � � "and" "ca" � � � � � � � � "ant" "cat" Figure 6.2-1: Trie structure (translations not shown). For each node, its key (e.g. “and”) is not explicitly stored in the node; instead, the key is defined by the node’s position in the tree: the key of its parent node + its index in that parent’s pointer array of children. In this problem, you will utilize a trie structure and to implement a simple one-way Englishto-French dictionary. The trie structure, shown in Figure 6.2-1, consists of nodes, each of which contains a string for storing translations for the word specified at that node and an array of pointers to child nodes. Each node, by virtue of its position in the trie, is associated with a string; in the dictionary context, this string is the word (or part of a word) in the dictionary. The dictionary may contain multiple translations for the same word; in this case, words should be separated by commas. For example: the word like, which has two meanings, could translate as comme, a preposition, or as aimer, a verb. Thus, the translation string should be “comme,aimer.” To get you started, we’ve provided code in prob2.c, which can be downloaded from Stellar. You will need to: • fill in the helper functions new node(), delete node() • complete the function add word(), which adds a word to the trie • complete the function lookup word(), which searches the trie for a word and returns its translation(s) Once your code is working, run a few test cases. Hand in a copy of your code, and a printout of your program running in gdb, with a few example translations to demonstrate functionality. 5 Answer: one possible implementation of the four functions is shown below: / ∗ a l l o c a t e new n ode o n t h e h eap o utput : p o i n t e r t o new n ode ( must b e f r e e d ) ∗ / s truct s t r i e n o d e ∗ n ew n ode ( v oid ) { / ∗ TODO: a l l o c a t e a new n ode o n t h e h eap , a nd i n i t i a l i z e a l l f i e l d s t o d e f a u l t v a l u e s ∗...
View Full Document

Ask a homework question - tutors are online