CS 228 :Quiz 3 C++ Programming & Data Structures April 11, 1997 --------------------------------------------------------------- Time: 35 minutes ( 25 points ) Recitation TU8 TH10 TH11 Th2 Th3 Name: KEY CS login: KEY 1. ( 5 points ) Create a binary tree given the following input values IN THIS ORDER: 19, -1, 5, 9, 3, -5, 11, 21, 6. 19 / \ -1 21 / \ -5 5 / \ 3 9 / \ 6 11 2. ( 5 points ) Give the expression tree for the expression: 2 - 3 * 5 - 9 * 7 - / \ - * / \ / \ 2 * 9 7 / \ 3 5 3. ( 2 points ) Give the result if the tree you got for problem 1 is printed using inorder traversal. ANS: -5 -1 3 5 6 9 11 19 21 4. ( 3 points ) Give the result if the tree you got for problem 2 is printed using the postorder traversal. ANS: 2 3 5 * - 9 7 * - 5. (10 points) You are expected to do two things: (i) Add to the interface the declration of the member function binTreeSearch. (ii) Write the member function binTreeSearch based on the given information for the templated binTree class. The function binSearchTree should take as an argument the value X to be searched. If the node containing the value X is found, the function should return a pointer to that node otherwise, the function should return a null pointer. The following interface definition, although not complete, includes the information needed to answer the question. template class binTree { public: .. binTree() : root( NULL ) {} int IsEmtpty( ) const { return root == null; } // write the DECLARATION of binTreeSearch here const binTreeNode * binTreeSearch( const Etype & ) const; // ANSWER private: struct binTreeNode { Etype val; binTreeNode *left; binTreeNode *right; }; binTreeNode * root }; Write the function binTreeSearch. Use <, >, etc. template const binTreeNode * binTree::binTreeSearch( const Etype & X ) const // PRE: This IS a BST // MODIFIES: Nothing // POST: A pointer to a node containing X is returned // if X is in the BST, otherwise NULL is returned. { // Start out at the root of the BST. binTreeNode * ptr = root; // Traverse the BST until the item is found or NULL is reached. // The order of checking is VERY important. while ( ptr != NULL && ptr->val != X ) { if ( X < ptr->val ) ptr = ptr->left; else ptr = ptr->right; } // Return what was found. return ( ptr ); }