You are to use a binary search tree to implement a priority queue. Write a C++ p
ID: 3577531 • Letter: Y
Question
You are to use a binary search tree to implement a priority queue. Write a C++ program that do the following: It first prompts the user for entering a sequence of data elements of type int and constructs a binary search tree by inserting these elements one by one. This binary search tree represents a max priority' queue. The user then inserts or deletes from the max priority queue from a menu. Note that, to demonstrate that the binary search tree is correctly restructured after each insertion or deletion, print out the tree using level-order traversal. In addition, you have to implement a function called computsize which returns the total number of elements in the left subtree of an element given by the user.Explanation / Answer
# include <iostream>
# include <cstdlib>
using namespace std;
struct node
{
int info;
struct node *left;
struct node *right;
}*root;
class BST
{
public:
void find(int, node **, node **);
void case_a(node *,node *);
void preorder(node *);
void inorder(node *);
void case_b(node *,node *);
void case_c(node *,node *);
void del(int);
void postorder(node *);
void display(node *, int);
void insert(int);
BST()
{
root = NULL;
}
};
int main()
{
BST bst;
int yourchoice, num;
node *temp;
while (1)
{
cout<<"1.Insert Element "<<endl;
cout<<"2.Delete Element "<<endl;
cout<<"3.Inorder Traversal"<<endl;
cout<<"4.Preorder Traversal"<<endl;
cout<<"5.Postorder Traversal"<<endl;
cout<<"6.Display"<<endl;
cout<<"7.Quit"<<endl;
cout<<"What you want to do : ";
cin>>yourchoice;
switch(yourchoice)
{
case 1:
temp = new node;
cout<<"Enter the number to be inserted : ";
cin>>temp->info;
bst.insert(root, temp);
case 2:
if (root == NULL)
{
cout<<"Sorry, Tree is empty, nothing to delete"<<endl;
continue;
}
cout<<"Enter the number You want to be deleted : ";
cin>>num;
bst.del(num);
break;
case 3:
cout<<"Inorder Traversal of Binary Search Tree:"<<endl;
bst.inorder(root);
cout<<endl;
break;
case 4:
cout<<"Preorder Traversal of Binary Search Tree:"<<endl;
bst.preorder(root);
cout<<endl;
break;
case 5:
cout<<"Post order Traversal of Binary Search Tree:"<<endl;
bst.postorder(root);
cout<<endl;
break;
case 6:
cout<<"Your Binary Search Tree:"<<endl;
bst.display(root,1);
cout<<endl;
break;
case 7:
exit(1);
default:
cout<<"Wrong Choice"<<endl;
}
}
}
void BST::find(int item, node **par, node **loc)
{
node *ptr, *ptrsave;
if (root == NULL)
{
*loc = NULL;
*par = NULL;
return;
}
if (item == root->info)
{
*loc = root;
*par = NULL;
return;
}
if (item < root->info)
ptr = root->left;
else
ptr = root->right;
ptrsave = root;
while (ptr != NULL)
{
if (item == ptr->info)
{
*loc = ptr;
*par = ptrsave;
return;
}
ptrsave = ptr;
if (item < ptr->info)
ptr = ptr->left;
else
ptr = ptr->right;
}
*loc = NULL;
*par = ptrsave;
}
void BST::insert(node *bst, node *newnode)
{
if (root == NULL)
{
root = new node;
root->info = newnode->info;
root->left = NULL;
root->right = NULL;
cout<<"Root Node is Added"<<endl;
return;
}
if (bst->info == newnode->info)
{
cout<<"Element is already inserted in the bst"<<endl;
return;
}
if (bst->info > newnode->info)
{
if (bst->left != NULL)
{
insert(bst->left, newnode);
}
else
{
bst->left = newnode;
(bst->left)->left = NULL;
(bst->left)->right = NULL;
cout<<"Node is Added To Left side..."<<endl;
return;
}
}
else
{
if (bst->right != NULL)
{
insert(bst->right, newnode);
}
else
{
bst->right = newnode;
(bst->right)->left = NULL;
(bst->right)->right = NULL;
cout<<"Node is Added To Right Side..."<<endl;
return;
}
}
}
void BST::del(int item)
{
node *parent, *location;
if (root == NULL)
{
cout<<"Tree empty"<<endl;
return;
}
find(item, &parent, &location);
if (location == NULL)
{
cout<<"Item not present in bst"<<endl;
return;
}
if (location->left != NULL && location->right != NULL)
case_c(parent, location);
if (location->left != NULL && location->right == NULL)
case_b(parent, location);
if (location->left == NULL && location->right != NULL)
case_b(parent, location);
if (location->left == NULL && location->right == NULL)
case_a(parent, location);
free(location);
}
void BST::case_a(node *par, node *loc )
{
if (par == NULL)
{
root = NULL;
}
else
{
if (loc == par->left)
par->left = NULL;
else
par->right = NULL;
}
}
void BST::case_b(node *par, node *loc)
{
node *child;
if (loc->left != NULL)
child = loc->left;
else
child = loc->right;
if (par == NULL)
{
root = child;
}
else
{
if (loc == par->left)
par->left = child;
else
par->right = child;
}
}
void BST::case_c(node *par, node *loc)
{
node *ptr, *ptrsave, *suc, *parsuc;
ptrsave = loc;
ptr = loc->right;
while (ptr->left != NULL)
{
ptrsave = ptr;
ptr = ptr->left;
}
suc = ptr;
parsuc = ptrsave;
if (suc->left == NULL && suc->right == NULL)
case_a(parsuc, suc);
else
case_b(parsuc, suc);
if (par == NULL)
{
root = suc;
}
else
{
if (loc == par->left)
par->left = suc;
else
par->right = suc;
}
suc->left = loc->left;
suc->right = loc->right;
}
void BST::preorder(node *ptr)
{
if (root == NULL)
{
cout<<"bst is empty"<<endl;
return;
}
if (ptr != NULL)
{
cout<<ptr->info<<" ";
preorder(ptr->left);
preorder(ptr->right);
}
}
void BST::inorder(node *ptr)
{
if (root == NULL)
{
cout<<"BST is Empty..."<<endl;
return;
}
if (ptr != NULL)
{
inorder(ptr->left);
cout<<ptr->info<<" ";
inorder(ptr->right);
}
}
void BST::postorder(node *ptr)
{
if (root == NULL)
{
cout<<"bst is empty"<<endl;
return;
}
if (ptr != NULL)
{
postorder(ptr->left);
postorder(ptr->right);
cout<<ptr->info<<" ";
}
}
void BST::display(node *ptr, int level)
{
int i;
if (ptr != NULL)
{
display(ptr->right, level+1);
cout<<endl;
if (ptr == root)
cout<<"ThisIsRoot ->: ";
else
{
for (i = 0;i < level;i++)
cout<<" ";
}
cout<<ptr->info;
display(ptr->left, level+1);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.