Please make sure it runs and folows the details in order to recieve points. This
ID: 645669 • Letter: P
Question
Please make sure it runs and folows the details in order to recieve points.
This is all 1 program. In C++ 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):
1) Build the tree from the unique set of names (names are the key value) and their associated weights.
2) Execute a preorder traversal
Execute an inorder traversal
3) Execute a postorder traversal
4) 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).
5) 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.
6) Determine the lowest weight contained in the tree.
7) Find the first name in alphabetical order (this should not go through every node, unless the tree happens to be a linked list).
Below is an example tree...
Please make sure it runs and folows the details in order to recieve points. This is all 1 program. In C++ 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): 1) Build the tree from the unique set of names (names are the key value) and their associated weights. 2) Execute a preorder traversal Execute an inorder traversal 3) Execute a postorder traversal 4) 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). 5) 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. 6) Determine the lowest weight contained in the tree. 7) Find the first name in alphabetical order (this should not go through every node, unless the tree happens to be a linked list). Below is an example tree...Explanation / Answer
#include<iostream.h>
#include<conio.h>
class BSTNode
{
int data;
BSTNode *p,*lc,*rc;
public :
BSTNode()
{
data=0;
p=NULL;
lc=NULL;
rc=NULL;
}
BSTNode(int k)
{
data=k;
p=NULL;
lc=NULL;
rc=NULL;
}
void setData(int k){data=k;}
int getData(){return data;}
void setP(BSTNode *n){p=n;}
BSTNode* getP(){return p;}
void setLc(BSTNode *n){lc=n;}
BSTNode* getLc(){return lc;}
void setRc(BSTNode *n){rc=n;}
BSTNode* getRc(){return rc;}
};
class BST
{
BSTNode *root;
public :
BST();
BSTNode* getRoot();
void addNode(BSTNode *n);
void preorder(BSTNode *ptr);
void inorder(BSTNode *ptr);
void postorder(BSTNode *ptr);
BSTNode* search(int data);
BSTNode* treeMin(BSTNode *ptr);
BSTNode* treeMax(BSTNode *ptr);
BSTNode* sucessor(BSTNode *ptr);
BSTNode* deleteNode(BSTNode *z);
};
BSTNode* BST :: deleteNode(BSTNode *z)
{
BSTNode *y,*x;
if(z->getLc()==NULL || z->getRc()==NULL)
y=z;
else
y=sucessor(z);
if(y->getLc()!=NULL)
x=y->getLc();
else
x=y->getRc();
if(x!=NULL)
x->setP(y);
if(y==root)
root=x;
else
{
x->setP(y->getP());
if(y==y->getP()->getLc())
y->getP()->setLc(x);
else
y->getP()->setRc(x);
}
if(y!=z)
z->setData(y->getData());
return y;
}
BST :: BST()
{
root=NULL;
}
BSTNode* BST :: getRoot()
{
return root;
}
void BST :: addNode(BSTNode *n)
{
int data;
cout<<" Enter the data : ";
cin>>data;
n->setData(data);
BSTNode *ptr=root;
BSTNode *parent;
if(ptr==NULL)
{
root=n;
}
else
{
while(ptr!=NULL)
{
parent=ptr;
if(n->getData()<ptr->getData())
ptr=ptr->getLc();
else
ptr=ptr->getRc();
}
n->setP(parent);
if(parent->getData()>n->getData())
parent->setLc(n);
else
parent->setRc(n);
}
}
BSTNode* BST :: treeMin(BSTNode *ptr)
{
while(ptr->getLc()!=NULL)
ptr=ptr->getLc();
return ptr;
}
BSTNode* BST :: treeMax(BSTNode *ptr)
{
while(ptr->getRc()!=NULL)
ptr=ptr->getRc();
return ptr;
}
void BST :: preorder(BSTNode *ptr)
{
if(ptr!=NULL)
{
cout<<ptr->getData()<<" ";
preorder(ptr->getLc());
preorder(ptr->getRc());
}
}
void BST :: inorder (BSTNode *ptr)
{
if(ptr!=NULL)
{
inorder(ptr->getLc());
cout<<ptr->getData()<<" ";
inorder(ptr->getRc());
}
}
void BST :: postorder(BSTNode *ptr)
{
if(ptr!=NULL)
{
postorder(ptr->getLc());
postorder(ptr->getRc());
cout<<ptr->getData()<<" ";
}
}
BSTNode* BST :: search(int data)
{
BSTNode *ptr=root;
while(ptr!=NULL && ptr->getData()!=data)
{
if(data<ptr->getData())
ptr=ptr->getLc();
else
ptr=ptr->getRc();
}
return ptr;
}
BSTNode* BST :: sucessor(BSTNode *ptr)
{
if(ptr!=NULL && ptr->getRc()!=NULL)
return treeMin(ptr->getRc());
BSTNode *parent=ptr->getP();
while(parent!=NULL && parent->getRc()==ptr)
{
ptr=parent;
parent=parent->getP();
}
return parent;
}
void main()
{
int choice;
BST b;
clrscr();
do
{
clrscr();
cout<<" 1 ---Enter the node";
cout<<" 2 ---PreOrder 3 ---InOrder 4 ---PostOrder";
cout<<" 5 ---Search";
cout<<" 6 ---Tree min 7 ---Tree max";
cout<<" 8 ---sucessor";
cout<<" 10---Delete a node";
cout<<" 11---Exit";
cout<<" Enter the choice : ";
cin>>choice;
if(choice==1)
{
BSTNode *node=new BSTNode();
b.addNode(node);
}
if(choice==2)
{
cout<<" PreOrder of Graph : ";
b.preorder(b.getRoot());
getch();
}
if(choice==3)
{
cout<<" InOrder of Graph : ";
b.inorder(b.getRoot());
getch();
}
if(choice==4)
{
cout<<" PostOrder of Graph : ";
b.postorder(b.getRoot());
getch();
}
if(choice==5)
{
int data;
BSTNode *search;
cout<<" Enter the searching element : ";
cin>>data;
search=b.search(data);
if(search->getData()==data)
cout<<" Data found";
else
cout<<" Data not found";
getch();
}
if(choice==6)
{
cout<<" Tree Minimum : "<<b.treeMin(b.getRoot())->getData();
getch();
}
if(choice==7)
{
cout<<" Tree Maximum : "<<b.treeMax(b.getRoot())->getData();
getch();
}
if(choice==8)
{
int data;
BSTNode *search;
BSTNode *suc;
cout<<" Enter the data : ";
cin>>data;
search=b.search(data);
if(search->getData()==data)
{
suc=b.sucessor(search);
cout<<" Sucessor of "<<data <<" : " <<suc->getData();
}
else
{
cout<<" Data not found";
}
getch();
}
if(choice==10)
{
int data;
cout<<"Enter the deleting node : ";
cin>>data;
BSTNode *search=b.search(data);
if(search->getData()!=data)
cout<<" Data not found ";
else
{
BSTNode *del=b.deleteNode(search);
cout<<"Node "<<del->getData()<<" is deleated.";
}
getch();
}
}while(choice!=11);
}