Implementation of BST: Partially completed C++ implementation of BST is given. It implements to the INSERT and INORDER traversal. You need to implement the following
Implementation of BST:
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.
#include
#include
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 );
};
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* parent = NULL;
while (x != NULL)
{
y = x;
if (z->key key)
x = x->left;
else
x = x->right;
}
parent = y;
if (y == NULL)
root = z;
else if (z->key key)
y->left = z;
else
y->right = z;
}
void BinarySearchTree::INORDER_TREE_WALK(node* x)
{
if (x != NULL)
{
if (x->left) INORDER_TREE_WALK(x->left);
cout key
if (x->right) INORDER_TREE_WALK(x->right);
}
}
int main()
{
BinarySearchTree bst;
int choice, key;
while (true)
{
cout
cout
cout
cout
cout
cout
cout
cin >> choice;
switch (choice)
{
case 1: cout
cin >> key;
bst.TREE_INSERT(key);
break;
case 2: cout
cout
cout
bst.INORDER_TREE_WALK(bst.root);
break;
case 3: system("pause");
return 0;
break;
default:
cout
}
}
}
C:\Windows system321cmd.exe CEA. Binary Search Tree Example 1 Insert a Node 2. Pre-order Traversa 3 Post-order Traversal 4. In order Traversal 5 Find Max 6. Remove Max Successor 8 Delete a Node 8 Exit Enter your choiceStep by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started