Question: Consider the following BST and TNode classes. template class TNode public: T val; TNode* left; TNode* right; template class BST public: BST); BST(); bool Search(T&

 Consider the following BST and TNode classes. template class TNode public:T val; TNode* left; TNode* right; template class BST public: BST); BST();bool Search(T& val); void Insert(T& val); int count; // stores the number

Consider the following BST and TNode classes. template class TNode public: T val; TNode* left; TNode* right; template class BST public: BST); BST(); bool Search(T& val); void Insert(T& val); int count; // stores the number of // nodes in the subtree // rooted at this node. private: TNode *root; }; int count(TNode * node) { if (node == nullptr) return; else return node->count; } }; In addition to the value, each node stores in a data member named count the number of nodes in the tree rooted at that node (see the illustration for an example). 10 count -9 5 count = 6 15 count = 2 3 count = 2 7 count = 3 20 count = 1 2 count - 1 6 count = 1 9 count = 1 What modifications need to be made to the following code for the insert function in order for the count variable to be updated correctly? Provide only the line number(s) and the modified code at these lines only (do not re-write the whole function). 1 template 2 void BST:: insert(T& val){ TNode* newNode = new TNode (val , nullptr, nullptr); What modifications need to be made to the following code for the insert function in order for the count variable to be updated correctly? Provide only the line number(s) and the modified code at these lines only (do not re-write the whole function). 1 template 2 void BST:: insert(T& val){ 3 TNode* newNode = new TNode (val , nullptr, nullptr); 4 5 6 7 if (isEmpty()) { root = newNode; return; } 8 9 10 TNode * p = nullptr; TNode * C = root; while (c != nullptr) { p = 0; 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 } if (val > C->data) C = C->right; else C = C->left; } if (val > p->data ) p->right = newNode; else p->left = newNode; Implement the member function int countLess Than(T & val, TNode * node), which returns the number of values in the tree that are less than val. Examples. In the illustration above: countLess Than(10, root) should return 6. countLess Than(9, root) should return 5. countLess Than(20, root) should return 8. countLess Than(4, root) should return 2. countLess Than(2, root) should return 0. Complete the following code to perform this task: (assume that the BST has no duplicates and that val is not necessarily in the tree) 1 template 2 int BST:: countlessThan(T& val, tNode * node) { 3 if (node == nullptr) 4 return B 5 6 if (val == node->val) 7 return 8 else if (val val) 9 return D 10 else if (val > node->val) 11 return 13 } Hint: Remember that each node stores the size of the subtree rooted at that node

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!