a. Write a class called node . This class willbe used to store a positive intege
ID: 3615399 • Letter: A
Question
a. Write a class called node. This class willbe used to store a positive integer value in abinary search tree. It should have pointers that point to a leftand right child. Finally, it should have a static integer valuecalled numNodes that keeps track of how many nodeobjects have been instantiated. This class should have aconstructor that initializes its private variables and incrementsnumNodes. It should have a destructor thatdecrements numNodes. The class will need get andset functions for each of its member variables except there will beno setNumNodes function. Note you will have todefine the function getNumNodes as a staticfunction. The class should have a print function that prints thevalue of the node.
b. Write a class called bsTree. This class willcontain a pointer to the root node of a binary search treeconstructed of node objects. It should have aconstructor that initializes the root node pointer to NULL. Itshould have a destructor that deletes all the nodes in the tree.Place a print statement in the destructor stating which node isbeing deleted. The class should have a function to print out thenodes in the tree in order using the built in print function of thenode class. The class should have a function toadd a node to the binary search tree and anotherto search the binary search tree for a key value.The search function should return a pointer to the node with valueequal to the search key or NULL otherwise. Make sure to use thenew and delete commands whencreating a new node or deleting an existing node, respectively.
c. Write a main program to test your bsTreeclass. Your main program should have a menu that performs thefollowing functions
(1) Add a node to a tree.
(2) Print a tree in preorder format.
(3) Print a tree in postorder format.
(4) Print a tree in inorder format.
(5) Print total number of nodes instantiated in the tree.
(6) Search tree for a node.
(7) Delete all nodes in the tree.
(8) Exit
**** i am very lost... any help would be appreciated
Explanation / Answer
Dear, #include<iostream.h>#include<conio.h>
class Tree
{
public:
int data;
Tree *lchild,*rchild,*root,*stack[25],*p;
Tree() //CONSTRUCTOR
{
root=NULL;
root->lchild=NULL;
root->rchild=NULL;
}
void menu()
{
cout<<endl;
cout<<"TREE OPERATIONS"<<endl;
cout<<"================="<<endl;
cout<<"1.CREATE"<<endl;
cout<<"2.PREORDER-TRAVERSAL"<<endl;
cout<<"3.INORDER-TRAVERSAL"<<endl;
cout<<"4.POSTORDER-TRAVERSAL"<<endl;
cout<<"5.EXIT"<<endl;
}
//ENTER THE NODES ALONG WITH LEFT CHILD AND RIGHT CHILD
void create()
{
int n,temp1,temp2;
cout<<"Please enter thenumber of nodes ";
cin>>n;
Tree *t[20];
cout<<"Enter thenodes"<<endl;
for(int i=1;i<=n;i++)
{
t[i]=newTree();
cin>>t[i]->data;
}
for(i=1;i<=n;i++)
{
cout<<"Enterthe leftchild and rightchild of "<<t[i]->data;
cin>>temp1>>temp2;
if(temp1==0)
t[i]->lchild=NULL;
else
t[i]->lchild=t[temp1];
if(temp2==0)
t[i]->rchild=NULL;
else
t[i]->rchild=t[temp2];
}
root=t[1];
}
//Inorder Traversal
void inorder(Tree *r)
{
if(root==NULL)
cout<<"tree empty"<<endl;
else
{
int top=0;
p=root;
stack[top]=p;
p=p->lchild;
while(top>=0)
{
while(p!=NULL)
{
top++;
stack[top]=p;
p=p->lchild;
}
p=stack[top];
top--;
cout<<p->data<<",";
p=p->rchild;
if(p!=NULL)
{
top++;
stack[top]=p;
p=p->lchild;
}
}//END OF WHILE
}//END OF IF
}//END OF FUNCTION
//Preorder Traversal
void preorder(Tree *r)
{
if(root==NULL)
cout<<"Tree empty"<<endl;
else
{
int top=0;
p=root;
stack[top]=NULL;
while(p!=NULL)
{
cout<<p->data<<",";
if(p->rchild!=NULL)
{
top++;
stack[top]= p->rchild;
}
if(p->lchild!=NULL)
p=p->lchild;
else
{
p=stack[top];
top--;
}
} //END OF WHILE
}//END OF IF
}//END OF FUNCTION
//Postorder Traversal
void postorder(Tree *r)
{
if(root==NULL)
cout<<"Tree empty"<<endl;
else
{
int top=0;
p=root;
while(top>=0)
{
while(p!=NULL)
{
top++;
stack[top]=p;
if(p->rchild!=NULL)
{
top++;
stack[top]=p->rchild;
}
p->rchild->data=-(p->rchild->data);
p=p->lchild;
}
p=stack[top];
top--;
while(p->data>0 && top>=0)
{
cout<<p->data<<",";
p=stack[top];
top--;
}
if(p->data < 0)
p->data=-(p->data);
}//END OF WHILE
}//END Of IF
}//END OF FUNCTION
};
void main() //Main Function
{
int ch=0;
clrscr();
Tree t;
while(ch!=5)
{
t.menu();
cout<<"Please enter the choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:t.create();break;
case 2:t.preorder(t.root);break;
case 3:t.inorder(t.root);break;
case 4:t.postorder(t.root);break;
case 5:break;
}//END OF SWITCH
}// END OF WHILE
}//END Of MAIN
I hope this will helpful foryou................... #include<iostream.h>
#include<conio.h>
class Tree
{
public:
int data;
Tree *lchild,*rchild,*root,*stack[25],*p;
Tree() //CONSTRUCTOR
{
root=NULL;
root->lchild=NULL;
root->rchild=NULL;
}
void menu()
{
cout<<endl;
cout<<"TREE OPERATIONS"<<endl;
cout<<"================="<<endl;
cout<<"1.CREATE"<<endl;
cout<<"2.PREORDER-TRAVERSAL"<<endl;
cout<<"3.INORDER-TRAVERSAL"<<endl;
cout<<"4.POSTORDER-TRAVERSAL"<<endl;
cout<<"5.EXIT"<<endl;
}
//ENTER THE NODES ALONG WITH LEFT CHILD AND RIGHT CHILD
void create()
{
int n,temp1,temp2;
cout<<"Please enter thenumber of nodes ";
cin>>n;
Tree *t[20];
cout<<"Enter thenodes"<<endl;
for(int i=1;i<=n;i++)
{
t[i]=newTree();
cin>>t[i]->data;
}
for(i=1;i<=n;i++)
{
cout<<"Enterthe leftchild and rightchild of "<<t[i]->data;
cin>>temp1>>temp2;
if(temp1==0)
t[i]->lchild=NULL;
else
t[i]->lchild=t[temp1];
if(temp2==0)
t[i]->rchild=NULL;
else
t[i]->rchild=t[temp2];
}
root=t[1];
}
//Inorder Traversal
void inorder(Tree *r)
{
if(root==NULL)
cout<<"tree empty"<<endl;
else
{
int top=0;
p=root;
stack[top]=p;
p=p->lchild;
while(top>=0)
{
while(p!=NULL)
{
top++;
stack[top]=p;
p=p->lchild;
}
p=stack[top];
top--;
cout<<p->data<<",";
p=p->rchild;
if(p!=NULL)
{
top++;
stack[top]=p;
p=p->lchild;
}
}//END OF WHILE
}//END OF IF
}//END OF FUNCTION
//Preorder Traversal
void preorder(Tree *r)
{
if(root==NULL)
cout<<"Tree empty"<<endl;
else
{
int top=0;
p=root;
stack[top]=NULL;
while(p!=NULL)
{
cout<<p->data<<",";
if(p->rchild!=NULL)
{
top++;
stack[top]= p->rchild;
}
if(p->lchild!=NULL)
p=p->lchild;
else
{
p=stack[top];
top--;
}
} //END OF WHILE
}//END OF IF
}//END OF FUNCTION
//Postorder Traversal
void postorder(Tree *r)
{
if(root==NULL)
cout<<"Tree empty"<<endl;
else
{
int top=0;
p=root;
while(top>=0)
{
while(p!=NULL)
{
top++;
stack[top]=p;
if(p->rchild!=NULL)
{
top++;
stack[top]=p->rchild;
}
p->rchild->data=-(p->rchild->data);
p=p->lchild;
}
p=stack[top];
top--;
while(p->data>0 && top>=0)
{
cout<<p->data<<",";
p=stack[top];
top--;
}
if(p->data < 0)
p->data=-(p->data);
}//END OF WHILE
}//END Of IF
}//END OF FUNCTION
};
void main() //Main Function
{
int ch=0;
clrscr();
Tree t;
while(ch!=5)
{
t.menu();
cout<<"Please enter the choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:t.create();break;
case 2:t.preorder(t.root);break;
case 3:t.inorder(t.root);break;
case 4:t.postorder(t.root);break;
case 5:break;
}//END OF SWITCH
}// END OF WHILE
}//END Of MAIN
I hope this will helpful foryou................... I hope this will helpful foryou...................
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.