Emmma W.
asked 08/24/21Slove the code in C++ .Do not copy paste from net please.
int findMaxNode(TreeNode *tree){ if(tree == NULL){ return -1; }else if(tree->right == NULL){ return tree->data; }else{ return findMaxNode(tree->right); } } void makeEmpty(TreeNode *&tree){ if(tree == NULL){ return; } makeEmpty(tree->left); makeEmpty(tree->right); delete tree; tree = NULL; } int getHeight(TreeNode *tree){ if(tree == NULL){ return 0; } int lsh = 1+getHeight(tree->left); int rsh = 1+getHeight(tree->right); if(lsh > rsh){ return lsh; }else{ return rsh; } } public: BinarySearchTree(){ root = NULL; } void insertNode(int data){ insertNode(root,data); } void printTree(){ printTree(root); } int treeLength(){ treeLength(root); } TreeNode* retrieveNode(int data){ return retrieveNode(root,data); } bool findNode(int data){ return findNode(root,data); } void deleteNode(int data){ deleteNode(root,data); } int findMinNode(){ findMinNode(root); } int findMaxNode(){ findMaxNode(root); } bool isBalanced(); void makeEmpty(){ makeEmpty(root); } int getHeight(){ getHeight(root); } bool isEmpty(){ if(root == NULL){ return true; }else{ return false; } } };
int main(){
// Create a binary search tree
// Insert nodes: 10, 5, 15, 2, 7, 13, 6, 8, 11, 17
// Print the tree
// Print tree height
// Print the minimum value in the tree
// Print the maximum value in the tree
// Print the number of nodes in the tree
// Manually print the nodes at each level (see example below)
cout << "Level 0: 10" << endl;
// Delete exactly two nodes to make the tree full and complete
// Print tree again
// Insert 8 more nodes but make sure that tree becomes full and complete
// Print tree again
// Delete all the nodes in the tree
// Print the tree again
}
More
1 Expert Answer

Chandra G. answered 08/25/21
Tutor
4.4
(307)
Phd In Computer Science, Lead Developer for DataBlitz, on Unix
int main(){
// Create a binary search tree
BinarySearchTree myTree;
// Insert nodes: 10, 5, 15, 2, 7, 13, 6, 8, 11, 17
myTree.insertNode(10);
myTree.insertNode(5);
myTree.insertNode(15);
myTree.insertNode(2);
myTree.insertNode(7);
myTree.insertNode(13);
myTree.insertNode(6);
myTree.insertNode(8);
myTree.insertNode(11);
myTree.insertNode(17);
// Print the tree
myTree.printTree();
// Print tree height
cout << myTree.getHeight() << endl;
// Print the minimum value in the tree
cout << myTree.findMinNode() << endl;
// Print the maximum value in the tree
cout << myTree.findMaxNode() << endl;
// Print the number of nodes in the tree
// Manually print the nodes at each level (see example below)
// TBD
cout << "Level 0: 10" << endl;
// Delete exactly two nodes to make the tree full and complete
// Print tree again
// Insert 8 more nodes but make sure that tree becomes full and complete
// Print tree again
// Delete all the nodes in the tree
// Print the tree again
}
Still looking for help? Get the right answer, fast.
Ask a question for free
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Find an Online Tutor Now
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Emmma W.
#include using namespace std; struct TreeNode { int data; TreeNode *left; TreeNode *right; }; class BinarySearchTree{ private: TreeNode* root; void insertNode(TreeNode *&tree, int data){ if(tree == NULL){ tree = new TreeNode; tree->data = data; tree->left = NULL; tree->right = NULL; }else if(data < tree->data){ insertNode(tree->left,data); }else{ insertNode(tree->right,data); } } void printTree(TreeNode *tree){ if(tree == NULL){ return; } printTree(tree->left); cout << tree->data << ", "; printTree(tree->right); } int treeLength(TreeNode *tree){ if(tree==NULL){ return 0; } return 1+treeLength(tree->left)+treeLength(tree->right); } bool findNode(TreeNode *tree, int data){ if(tree==NULL){ return false; } if(tree->data == data){ return true; }else if(data < tree->data){ return findNode(tree->left,data); }else{ return findNode(tree->right,data); } } TreeNode* retrieveNode(TreeNode *tree, int data){ if(tree==NULL){ return NULL; } if(tree->data == data){ return tree; }else if(data < tree->data){ return retrieveNode(tree->left,data); }else{ return retrieveNode(tree->right,data); } } void deleteNode(TreeNode *&tree, int data){ if(tree == NULL){ return; } if(tree->data == data){ if(tree->left == NULL && tree->right == NULL){ delete tree; tree = NULL; }else if(tree->left != NULL){ int maxLeftNode = findMaxNode(tree->left); tree->data = maxLeftNode; deleteNode(tree->left,maxLeftNode); }else{ int minRightNode = findMinNode(tree->right); tree->data = minRightNode; deleteNode(tree->right,minRightNode); } }else if(tree->data < data){ deleteNode(tree->right,data); }else{ deleteNode(tree->left,data); } } int findMinNode(TreeNode *tree){ if(tree == NULL){ return -1; }else if(tree->left == NULL){ return tree->data; }else{ return findMinNode(tree->left); } } int findMaxNode(TreeNode *tree){ if(tree == NULL){ return -1; }else if(tree->right == NULL){ return tree->data; }else{ return findMaxNode(tree->right); } } void makeEmpty(TreeNode *&tree){ if(tree == NULL){ return; } makeEmpty(tree->left); makeEmpty(tree->right); delete tree; tree = NULL; } int getHeight(TreeNode *tree){ if(tree == NULL){ return 0; } int lsh = 1+getHeight(tree->left); int rsh = 1+getHeight(tree->right); if(lsh > rsh){ return lsh; }else{ return rsh; } } public: BinarySearchTree(){ root = NULL; } void insertNode(int data){ insertNode(root,data); } void printTree(){ printTree(root); } int treeLength(){ treeLength(root); } TreeNode* retrieveNode(int data){ return retrieveNode(root,data); } bool findNode(int data){ return findNode(root,data); } void deleteNode(int data){ deleteNode(root,data); } int findMinNode(){ findMinNode(root); } int findMaxNode(){ findMaxNode(root); } bool isBalanced(); void makeEmpty(){ makeEmpty(root); } int getHeight(){ getHeight(root); } bool isEmpty(){ if(root == NULL){ return true; }else{ return false; } } }; int main(){ // Create a binary search tree // Insert nodes: 10, 5, 15, 2, 7, 13, 6, 8, 11, 17 // Print the tree // Print tree height // Print the minimum value in the tree // Print the maximum value in the tree // Print the number of nodes in the tree // Manually print the nodes at each level (see example below) cout << "Level 0: 10" << endl; // Delete exactly two nodes to make the tree full and complete // Print tree again // Insert 8 more nodes but make sure that tree becomes full and complete // Print tree again // Delete all the nodes in the tree // Print the tree again }08/24/21