Partially completed C++ implementation of BST is given. It implements to the INS
ID: 3849745 • Letter: P
Question
Partially completed C++ implementation of BST is given. It implements to the INSERT and INORDER traversal. You need to implement the following BST algorithms discussed in the book. Each algorithm should be implemented as a separate method. You are free to change the tree node structure given while implementing these algorithms.
a) Insert
b) Post Order Traversal
c) Pre Order Traversal
d) Find Max
e) Remove Max
f) Successor (see slides for the algorithm)
g) Delete
Program should have a similar interface given below and the user can select the suitable option to perform the desired operation.
C: Windows system 321cmd.exe CA. Binary Search Tree Example 1. Insert a Node 2 Pre-order Traversal 3. Post-Order Traversal 4. In order Traversal 5 Find Max 6. Remove Max 7. Successor 8 Delete a Node 8 Exit Enter your choiceExplanation / Answer
#include <iostream>
#include <cstdlib>
using namespace std;
class BinarySearchTree
{
private:
class node
{
public:
node* left;
node* right;
int key;
string data;
};
public:
node* root;
BinarySearchTree()
{
root = NULL;
}
bool isEmpty() const { return root == NULL; }
void INORDER_TREE_WALK(node*);
void TREE_INSERT(int );
int max = 0;
};
void BinarySearchTree::TREE_INSERT(int d)
{
// This implements the algorithm in page 261 of the textbook
node* z = new node();
z->key = d;
z->left = NULL;
z->right = NULL;
node* y = NULL;
node* x = root;
node* _nodeParent = NULL;
while (x != NULL)
{
y = x;
if (z->key < x->key)
x = x->left;
else
x = x->right;
}
_nodeParent = y;
if (y == NULL)
root = z;
else if (z->key < y->key)
y->left = z;
else
y->right = z;
}
void BinarySearchTree::PREORDER(node *x)
{
if (x != NULL)
{
cout << " " << x->key << " ";
PREORDER(x->left);
PREORDER(x->right);
}
}
void BinarySearchTree::INORDER_TREE_WALK(node* x)
{
if (x != NULL)
{
if (x->left) INORDER_TREE_WALK(x->left);
cout << " " << x->key << " ";
if (x->right) INORDER_TREE_WALK(x->right);
}
}
void BinarySearchTree::POST_TREE_WALK(node* x)
{
if (x != NULL)
{
if (x->left) POST_TREE_WALK(x->left);
if (x->right) POST_TREE_WALK(x->right);
cout << " " << x->key << " ";
}
}
int BinarySearchTree::findMax(node *x)
{
if (x != NULL)
{
findMax(x->right);
if(max < x->key) {
max = x->key;
}
findMax(x->left);
}
else{
return max;
}
return 0;
}
void BinarySearchTree::successor(node *x)
{
if (x != NULL)
{
x = x->right;
cout<<"Successor is: "<<x->key<<endl;
}
else{
cout<<"No successor for this node.."<<endl;
}
}
void BinarySearchTree::DELETEMAX()
{
int val = findMax(root);
node *_nodeParent, *_nodeLocation;
if (root == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
find(val, &_nodeParent, &_nodeLocation);
if (_nodeLocation == NULL)
{
cout<<"Node not found"<<endl;
return;
}
if (_nodeLocation->left == NULL && _nodeLocation->right == NULL)
case_a(_nodeParent, _nodeLocation);
if (_nodeLocation->left != NULL && _nodeLocation->right == NULL)
case_b(_nodeParent, _nodeLocation);
if (_nodeLocation->left == NULL && _nodeLocation->right != NULL)
case_b(_nodeParent, _nodeLocation);
if (_nodeLocation->left != NULL && _nodeLocation->right != NULL)
case_c(_nodeParent, _nodeLocation);
free(_nodeLocation);
}
void BinarySearchTree::DELETE(int val)
{
node *_nodeParent, *_nodeLocation;
if (root == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
find(val, &_nodeParent, &_nodeLocation);
if (_nodeLocation == NULL)
{
cout<<"Node not found"<<endl;
return;
}
if (_nodeLocation->left == NULL && _nodeLocation->right == NULL)
case_a(_nodeParent, _nodeLocation);
if (_nodeLocation->left != NULL && _nodeLocation->right == NULL)
case_b(_nodeParent, _nodeLocation);
if (_nodeLocation->left == NULL && _nodeLocation->right != NULL)
case_b(_nodeParent, _nodeLocation);
if (_nodeLocation->left != NULL && _nodeLocation->right != NULL)
case_c(_nodeParent, _nodeLocation);
free(_nodeLocation);
}
void BinarySearchTree::find(int val, node **_nodeParent, node **_nodeLocation)
{
node *x, *savePtr;
if (root == NULL)
{
*_nodeLocation = NULL;
*_nodeParent = NULL;
return;
}
if (val == root->key)
{
*_nodeLocation = root;
*_nodeParent = NULL;
return;
}
if (val < root->key)
x = root->left;
else
x = root->right;
savePtr = root;
while (x != NULL)
{
if (val == x->key)
{
*_nodeLocation = x;
*_nodeParent = savePtr;
return;
}
savePtr = x;
if (val < x->key)
x = x->left;
else
x = x->right;
}
*_nodeLocation = NULL;
*_nodeParent = savePtr;
}
int main()
{
BinarySearchTree bst;
int choice, key;
while (true)
{
cout << endl << endl;
cout << " Binary Search Tree Example " << endl;
cout << " ----------------------------- " << endl;
cout << " 1. Insert a node " << endl;
cout << " 2. Pre-Order Traversal " << endl;
cout << " 3. Post-Order Traversal " << endl;
cout << " 4. In-Order Traversal " << endl;
cout << " 5. Find max " << endl;
cout << " 6. Remove Max " << endl;
cout << " 7. Successor" << endl;
cout << " 8. Delete a Node " << endl;
cout << " 9. Exit " << endl;
cout << " Enter your choice : ";
cin >> choice;
switch (choice)
{
case 1: cout << " Enter key (int value) to be inserted : ";
cin >> key;
bst.TREE_INSERT(key);
break;
case 2: cout << endl;
cout << " Pre-Order Traversal " << endl;
cout << " -------------------" << endl;
bst.PREORDER(bst.root);
break;
case 3: cout << endl;
cout << " Post-Order Traversal " << endl;
cout << " -------------------" << endl;
bst.POST_TREE_WALK(bst.root);
break;
case 4: cout << endl;
cout << " In-Order Traversal " << endl;
cout << " -------------------" << endl;
bst.INORDER_TREE_WALK(bst.root);
break;
case 5: cout << endl;
cout << " FIND MAX " << endl;
cout << " -------------------" << endl;
bst.findMax();
case 6: cout << endl;
cout << " REMOVE MAX " << endl;
cout << " -------------------" << endl;
bst.DELETEMAX();
break;
case 7: cout << endl;
cout << " Successor" << endl;
cout << " -------------------" << endl;
bst.successor(bst.root);
break;
case 8: cout << endl;
cout << " Delete node" << endl;
cout << " -------------------" << endl;
cout << " Enter key (int value) to be deleted : ";
cin >> key;
bst.DELETE(key);
break;
case 9: system("pause");
return 0;
break;
default:
cout << "Invalid choice";
}
}
}