C++ Help: Build a binary search tree, using links (not an array) for 15 records.
ID: 3818563 • Letter: C
Question
C++ Help:
Build a binary search tree, using links (not an array) for 15 records. The data in these records will hold names and their associated weights. Read the data from the screen.
Required functionality (Each # should be separate methods):
Build the tree from the unique set of names (names are the key value) and their associated weights.
Execute a preorder traversal
Execute an inorder traversal
Execute a postorder traversal
Find and print the height of the tree using recursion, do not add a height variable to the tree structure, the algorithm stack should hold this.
Determine the number of leaves and print the result (remember a leaf has no children).
Implement search functionality that will search for a name and indicate the weight for that individual if they exist in the structure, otherwise stating no match exists.
Determine the lowest weight contained in the tree.
Find the first name in alphabetical order (this should not go through every node, unless the tree happens to be a linked list).
Explanation / Answer
Answer:
C++ Programe to implements BST:
#include<iostream>
#include<sstdlib>
using namespace std;
/*
* Node Declaration
*/
struct node
{
int info;
struct node *left;
struct node *right;
}*root;
/*class Declaration
*/
class BST
{
public:
void find(int, node **,node **);
void insert(int);
void del(int);
void case_a(node *,node *);
void case_b(node *,node *);
void case_c(node *,node *);
void preorder(node *);
void inorder(node *);
void postorder(node *);
void display(node *,int);
BST()
{
root=NULL;
}
};
/*
*Main contains Menu
*/
int main()
{
int choice,name,weigth;
BST bst;
node *temp;
while(1)
{
cout<<"----------------------------------"<<endl;
cout<<"enter your name"<<endl;
cin>>"name";
cout<<"Operations on BST"<<endl;
cout<<"------------------------"<<endl;
cout<<"1.Insert Element"<<endl;
cout<<"2.Delete Element"<<endl;
cout<<"3.Inorder Traversal"<<endl;
cout<<"4.Prerder Traversal"<<endl;
cout<<"5.Postorder Traversal"<<endl;
cout<<"6.Display"<<endl;
cout<<"7.Quit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case1:
temp=new node;
cout<<"Enter the number to be inserted: ";
cin>>temp->info;
bst.insert(root,temp);
case 2:
if(root==NULL)
{
cout<<"Thee is empty, nothing to delete"<<endl;
continue;
}
cout<<"Enter the number to be deleted: ";
cin>>num;
bst.del(num);
break;
case 3:
cout<<"Inrder Traversal of BST: "<<endl;
bst.inorder(root);
cout<<endl;
break;
case 4:
cout<<"Preorder Traversal of BST: "<<endl;
bst.preorder(root);
cout<<endl;
break;
case 5:
cout<<Postorder Traversal of BST: "<<endl;
bst.postorder(root);
cout<<endl;
break;
case 6:
cout<<"Display BST: "<<endl;
bst.display(root,1);
cout<<endl;
break;
case 7:
exit(1);
default:
cout<<"wrong choice"<<endl;
}
}
}
/*
* Find Element in the tree
*/
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=rot->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;
}
/*
* Inserting Element into the Tree
*/
void BST::insert(node *tree,node *newnode)
{
if(rootr==NULL)
{
root=new node;
root->info=newnode->info;
root->left=NULL;
root->right=NULL;
cout<<"Root Node is added"<<endl;
return;
}
if(tree->info==newnode->info)
{
cout<<"Element already in the tree"<<endl;
return;
}
if(tree->info>newnode->info)
{
if(tree->left!=NULL)
{
insert(tree->left,newnode);
}
else
{
tree->left=newnode;
(tree->left)->left=NULL;
(tree->left)->right=NULL;
cout<<"Node added to left"<<endl;
return;
}
}
else
{
if(tree->right!=NULL)
{
insert(tree->right,newnode);
}
else
{
tree->right=newnode;
(tree->right)->left=NULL;
(tree->right)->right=NULL;
cout<<"Node Added to right"<<endl;
return;
}
}
}
/*
*Delete Element from the tree
*/
vid BST::del(int item)
{
node *parent,*location;
if(root==NULL)
{
cout<<"Tree empty"<<endl;
return;
}
find(item,&parent,&location);
if(location==NULL)
{
cuit<<"item not present in tree"<<endl;
return;
}
if(location->left==NULL&&location->right==NULL)
case_a(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(loction->left!=Null&&location->right!=NULL)
case_c(parent,location);
free(location);
}
/*
*Case A
*/
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;
}
}
/*
* Case B
*/
void BST::case_b(node *par,nde *loc)
{
node *child;
if(lc->left!=NULL)
child=loc->left;
else
child=loc->right;
if(par==NULL)
{
root=child;
}
else
if(lkoc==par->left)
par->left=child;
else
par->right=child;
}
}
/*
*Case C
*/
void BST::case_c(node *par,node *loc)
{
node *patr,*pyrsave,*suc,*parsuc;
ptrsave=lc;
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;
voidf BST::inorder(node *ptr)
{
if(root==NULL)
{
cout<<"tree is empty"
return;
}
if(ptr!=NULL)
{
inrder(ptr->left);
cout<<ptr->info<<"";
inorder(ptr->right);
}}
voidf BST::postorder(node *ptr)
{
if(root==NULL)
{
cout<<"tree is empty"
return;
}
if(ptr!=NULL)
{
inrder(ptr->left);
cout<<ptr->info<<"";
inorder(ptr->right);
}}
voidf BST::preorder(node *ptr)
{
if(root==NULL)
{
cout<<"tree is empty"
return;
}
if(ptr!=NULL)
{
inrder(ptr->left);
cout<<ptr->info<<"";
inorder(ptr->right);
}}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.