Menu

(Get Answer) – Write Two Function To Print All Words In Prefix Tree C

Question Description

In this lab, you will start with a solution to Lab 4, and you will add new functionality to the program. When the user types “list”, the program should print out all of the words that have been added to the PrefixTree so far, in alphabetical order. To add this functionality, you will need to implement two functions, PrefixTree::printWordList() and PrefixTreeNode::printAllWords().

PrefixTreeNode::printAllWords() is the function that will be doing most of the work. This function should recursively print all of the words that end at or below this node in the PrefixTree. The main idea is that if this node is final, you should print the word that ends here, then recursively print all of the words for all of this node’s (non-NULL) children, in alphabetical order.

To print out the word, I recommend keeping a string as a global variable: add characters to the word as you go down the tree, and remove characters as you go back up. (A better solution would be a static data member of PrefixTreeNode, but we haven’t discussed these static data members in class.) Luckily, string supports the push_back() and pop_back() operations, though you could also use concatenation and resize() or substr() to add and remove characters. Note that the root node contains the character ”–don’t add this character to the word.

PrefixTree::printWordList() will just need to print all of the words, starting at the root.

Like Lab 4, you will just submit a source file that includes the provided header file and implements these two functions.

  • Header file: prefixtree-lab5.hPreview the document
  • Source file: prefixtree-lab5.cppPreview the document
  • Driver file: prefixtree-lab5-driver.cppPreview the document
HTML tutorial

Leave a Reply

Your email address will not be published.