Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

hope u can see it clearly. its a big five code in C++ BST TODO part is the question(red part) templatetypename T> int NodeT); :

hope u can see it clearly. its a big five code in C++ BST

TODO part is the question(red part)

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

templatetypename T> int NodeT); : nodeCount 0; = Binary Search Tree (BST) class implementation template class BST protected: Noderoot; bool_debug false; Enable extra output / Root of the tree nodes /* Clone a passed in tree, returns pointer to new tree Node * cloneTree(NodecT *t){ if( t == nullptr ) return nullptr; else return new Nodevalue, cloneTree( t->left), cloneTree( t->right) ); /* Recursively delete the tree nodes */ void makeEmptyHelper(Node *t) ( if( t != nullptr ) { makeEmptyHelper( t-left); makeEmptyHelper( t->right ); delete t; /* Add new T val to the tree void addHelper(Node root, T val) f if (root->value> val) [ if (lroot->left) root->left Node(val); = new ) else f addHelper(root->left, val); else if (lroot->right) f root->right new Node(val); else addHelper(root->right, val); /t Print tree out in inorder (A + B) void printInOrderHelper(Node *root, std::ostream& out) t if (lroot) return printInorderHelper(root-left, out); out value right, out); /* Print tree out in post order (A B +) void printPostorderHelper(Node root, std::ostream& out) if (lroot) return; printPostOrderHelper(root->left, out); printPostOrderHelper(root->right, out); out root->value Print tree out in pre order (+ AB) void printPreOrderHelper(Node root, std::ostream& out) if (lroot) return; out value left, out); printPreOrderHelper(root->right, out); Print tree out in Level order MA TODO:Implement void printLevelorderHelper(Node root, std::ostream& out) { if (lroot) return out left) nodesCountHelper(root->right); /* Return height of tree (root-_ nullptr-> 0) */ int heightHelper(Nodeleft), heightHelper (root->right)); Print out Longest path from root to a Leaf void printMaxPathHelper (Node root) if (lroot) return coutroot->value heightHelper(root->right)) printMaxPathHelper(root->left); else printMaxPathHelper(root->right); /t Delete a given value from tree bool deleteValueHelper(Node*parent, Nodevaluevalue) [ if (current->leftnullptr || current->rightnullptr) Node* temp current->left; if (current->right) tempcurrent->right; if (parent) ( if (parent->leftcurrent) parent->left = temp; else parent-right - temp else this->_root-temp; else Node* validSubs-current->right; while (validSubs->left) validSubs validSubs->left; T temp current->value; current->value-validSubs->value validSubs->value-temp; return deleteValueHelper(current, current->right, temp); delete current; return true; return deleteValueHelper (current, current->left, value) |I deleteValueHelper(current, current->right, value); bool containsHelper(Nodevalue val ) else if root->value val) II Search Left return false); return( true); return containsHelper(root->left, val)); return containsHelper(root->right, val)) else /t PUBLIC AP1 / public: BST)_root( nullptr) BST initializer_list vals) : _root nullptr) i Basic initialization constructor for( auto val : vals) this->add(val); Destructor -Needs to free all nodes in the tree MA TODO: Implement BST if( this->_debug) ( cout debug) f cout _debug) { cout _debug) cout _debug) t cout makeEmptyHelper (this->_root); this->root nullptr; void add(T val) if (this->_root) this->addHelper(this->_root, val); else { this-> root new Node (val); bool empty) return( this->root nullptr ); /The print functions take an optional ostream handle Not providing one will have them default to std::cout (the terminal) void print (std::ostream& out-std::cout) f printInOrderHelper(this->_root); void printinorder(std: :ostream& printInorderHelper(this->_root, out); out=std::cout) { void printPostOrder(std::ostream& out-std::cout) printPostOrderHelper(this->_root, out); void printPrerder (std::ostream& out-std::cout) printPreOrderHelper (this->_root, out); void printLevelorder(std::ostream& out-std::cout) printLevelOrderHelper(this->_root, out); vector& returnLevelOrder) return returnLevelOrderHelper(this->_root); int size() return nodesCount() int nodesCount()f return nodesCountHelper (this->_root); int height) return heightHelper(this->_root); void printMaxPath) printMaxPathHelper(this->_root); bool deleteValue (T value) return this->deleteValueHelper(nullptr, this->_root, value); bool contains( T value f return containsHelper(this->_root, value); void debug_on() this->_debug-true; #endif templatetypename T> int NodeT); : nodeCount 0; = Binary Search Tree (BST) class implementation template class BST protected: Noderoot; bool_debug false; Enable extra output / Root of the tree nodes /* Clone a passed in tree, returns pointer to new tree Node * cloneTree(NodecT *t){ if( t == nullptr ) return nullptr; else return new Nodevalue, cloneTree( t->left), cloneTree( t->right) ); /* Recursively delete the tree nodes */ void makeEmptyHelper(Node *t) ( if( t != nullptr ) { makeEmptyHelper( t-left); makeEmptyHelper( t->right ); delete t; /* Add new T val to the tree void addHelper(Node root, T val) f if (root->value> val) [ if (lroot->left) root->left Node(val); = new ) else f addHelper(root->left, val); else if (lroot->right) f root->right new Node(val); else addHelper(root->right, val); /t Print tree out in inorder (A + B) void printInOrderHelper(Node *root, std::ostream& out) t if (lroot) return printInorderHelper(root-left, out); out value right, out); /* Print tree out in post order (A B +) void printPostorderHelper(Node root, std::ostream& out) if (lroot) return; printPostOrderHelper(root->left, out); printPostOrderHelper(root->right, out); out root->value Print tree out in pre order (+ AB) void printPreOrderHelper(Node root, std::ostream& out) if (lroot) return; out value left, out); printPreOrderHelper(root->right, out); Print tree out in Level order MA TODO:Implement void printLevelorderHelper(Node root, std::ostream& out) { if (lroot) return out left) nodesCountHelper(root->right); /* Return height of tree (root-_ nullptr-> 0) */ int heightHelper(Nodeleft), heightHelper (root->right)); Print out Longest path from root to a Leaf void printMaxPathHelper (Node root) if (lroot) return coutroot->value heightHelper(root->right)) printMaxPathHelper(root->left); else printMaxPathHelper(root->right); /t Delete a given value from tree bool deleteValueHelper(Node*parent, Nodevaluevalue) [ if (current->leftnullptr || current->rightnullptr) Node* temp current->left; if (current->right) tempcurrent->right; if (parent) ( if (parent->leftcurrent) parent->left = temp; else parent-right - temp else this->_root-temp; else Node* validSubs-current->right; while (validSubs->left) validSubs validSubs->left; T temp current->value; current->value-validSubs->value validSubs->value-temp; return deleteValueHelper(current, current->right, temp); delete current; return true; return deleteValueHelper (current, current->left, value) |I deleteValueHelper(current, current->right, value); bool containsHelper(Nodevalue val ) else if root->value val) II Search Left return false); return( true); return containsHelper(root->left, val)); return containsHelper(root->right, val)) else /t PUBLIC AP1 / public: BST)_root( nullptr) BST initializer_list vals) : _root nullptr) i Basic initialization constructor for( auto val : vals) this->add(val); Destructor -Needs to free all nodes in the tree MA TODO: Implement BST if( this->_debug) ( cout debug) f cout _debug) { cout _debug) cout _debug) t cout makeEmptyHelper (this->_root); this->root nullptr; void add(T val) if (this->_root) this->addHelper(this->_root, val); else { this-> root new Node (val); bool empty) return( this->root nullptr ); /The print functions take an optional ostream handle Not providing one will have them default to std::cout (the terminal) void print (std::ostream& out-std::cout) f printInOrderHelper(this->_root); void printinorder(std: :ostream& printInorderHelper(this->_root, out); out=std::cout) { void printPostOrder(std::ostream& out-std::cout) printPostOrderHelper(this->_root, out); void printPrerder (std::ostream& out-std::cout) printPreOrderHelper (this->_root, out); void printLevelorder(std::ostream& out-std::cout) printLevelOrderHelper(this->_root, out); vector& returnLevelOrder) return returnLevelOrderHelper(this->_root); int size() return nodesCount() int nodesCount()f return nodesCountHelper (this->_root); int height) return heightHelper(this->_root); void printMaxPath) printMaxPathHelper(this->_root); bool deleteValue (T value) return this->deleteValueHelper(nullptr, this->_root, value); bool contains( T value f return containsHelper(this->_root, value); void debug_on() this->_debug-true; #endif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Positive thinking and its effects on wellbeing.

Answered: 1 week ago