c++ Write six basic functions for the BST: Insert, Delete, Search, Find_max, Fin
ID: 3698531 • Letter: C
Question
c++
Write six basic functions for the BST: Insert, Delete, Search, Find_max,
Find_min, and Print_BST
1. Search(x): Find out the index that stores element x using binary search tree
mechanism. Print out all the elements in the search path.
2. Find_max( ): Find and print maximum value in BST
3. Find_min( ): Find and print minimum value in BST
4. Print_BST( ): Print out the BST structure in the form of array with index.
5. Insert(x): Insert a value element x into BST
6. Delete(x): Delete element x in BST including ALL 3 situations
After you finished the all functions, following are the things you need to carry out:
1. Insert(5)
2. Insert(8)
3. Insert(3)
4. Insert(1)
5. Insert(4)
6. Insert(9)
7. Insert(18)
8. Insert(20)
9. Insert(19)
10. Insert(2)
11. Perform Print_BST( )
12. Perform Find_max( )
13. Perform Find_min( )
14. Perform Search(3) in the BST
15. Perform Search(18) in the BST
16. Perform Search(19) in the BST
17. Delete(19) in the BST, perform Print_BST( )
18. Delete(2) in the BST, perform Print_BST( )
19. Delete(8) in the BST, perform Print_BST( )
20. Delete(3) in the BST, perform Print_BST( )
21. Delete(5) in the BST, perform Print_BST( )
Explanation / Answer
#include "stdafx.h" #include using namespace std; class node { public: int myValue; node *left; node *right; void printNode(node* rootNode) { if (rootNode != NULL) { printNode(rootNode->left); cout left = NULL; newNode->right = NULL; newNode->myValue = val; return newNode; } node * insertNode(int val, node* rootNode) { if (rootNode == NULL) return createNode(val); if (val myValue) rootNode->left = insertNode(val, rootNode->left); else rootNode->right = insertNode(val, rootNode->right); return rootNode; } node * find_max(node * rootNode) { node * temp = rootNode; while (temp->right != NULL) { temp = temp->right; } return temp; } node * find_min(node * rootNode) { node * temp = rootNode; while (temp->left != NULL) { temp = temp->left; } return temp; } node* deleteNode(node* rootNode, int value) { if (rootNode == NULL) return rootNode; if (value > rootNode->myValue) rootNode->right = deleteNode(rootNode->right, value); else if (value myValue) rootNode->left = deleteNode(rootNode->left, value); else { if (rootNode->left == NULL) { node *temp = rootNode->right; delete(rootNode); return temp; } else if (rootNode->right == NULL) { node *temp = rootNode->left; delete(rootNode); return temp; } node* temp = find_min(rootNode->right); rootNode->myValue = temp->myValue; rootNode->right = deleteNode(rootNode->right, temp->myValue); } return rootNode; } }; int main() { node tree; node * rootNode = NULL; rootNode = tree.insertNode(5, rootNode); rootNode = tree.insertNode(8, rootNode); rootNode = tree.insertNode(3, rootNode); rootNode = tree.insertNode(1, rootNode); rootNode = tree.insertNode(4, rootNode); rootNode = tree.insertNode(9, rootNode); rootNode = tree.insertNode(18, rootNode); rootNode = tree.insertNode(20, rootNode); rootNode = tree.insertNode(19, rootNode); rootNode = tree.insertNode(2, rootNode); coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.