//************** // concordance BST // // inherits from generic BST and // adds special print functions, etc // // // note in this code, T->Element is a // CData object..because that's how we // defined CTree-- a BST of CDatas // so all the comparisons, etc // get converted to CData's member functions // like operator <, >, ==, =, <<, etc // // DL Frey 10/21/98 //************************* #include "ctree.h" // PrintSomeNodes // // in-order traversal that prints all nodes whose // count is >= frequency // void CTree::PrintSomeNodes (Tree_Node *T, int frequency) const { if (T->Left) PrintSomeNodes (T->Left, frequency); if ( (T->Element).getCount() >= frequency) cout << T->Element << endl; if (T->Right) PrintSomeNodes ( T->Right, frequency); } // PrintNodes // this is the public interface // it calls PrintSomeNodes (above) or // BST's Print_Tree, based on frequency // void CTree::PrintNodes (int frequency) const { if (frequency == 0) Print_Tree (Root); else PrintSomeNodes (Root, frequency); } // PrintANode // uses BST's Find to locate the node // then prints it void CTree::PrintANode (const CData& word) const { Tree_Node* T; T = Find (word, Root); if (T) { cout << T->Element << endl; } else { cout << word.getText() << " is not in the tree" << endl; } } // Insert -- // code duplicated from BST's insert, except that // if the word exists, we increment it's count instead // of doing nothing. // // A ALTERNATIVE approach --- // // we could have used BST::Find that returns a pointer // to the node if it's in the tree. // if BST::Find returns NULL, we can use BST::Insert // if BST::Find returns a pointer, increment the count // in that node //******************************************* void CTree::Insert (const CData& word, Tree_Node* & T) { if (T == NULL) { // create a new node - Left/Right default to NULL T = new Tree_Node(word); } else if (word < T->Element) { Insert (word, T->Left); } else if (word > T->Element) { Insert (word, T->Right); } else { // its already here-- increments its occurrence count // note that T->Element is a CData class // and note the syntax to call the member function Increment() (T->Element).Increment(); } }