Please help me doing my homework. this homework done with C++ Data Structure.
This is Questions:
http://img520.imageshack.us/img520/4417/qqqq1gvq5.gif
#include <iostream>
#include <stdio>
#include <assert>
#include <string>
#include "bt.h"
class Calc
{
public:
} ;
int main()
{
bSearchTreeType<Calc> obj;
string command;
cout<<"I-Inputtheexpression"<<endl;
cout<<"N-InorderTraversal"<<endl;
cout<<"P-PreorderTraversal"<<endl;
cout<<"O-PostorderTraversal"<<endl;
cout<<"F-Numberofoperatorinthetree"<<endl;
cout<<"E-Evaluatethetree"<<endl;
cout<<"Q-Quit"<<endl;
cin>>command;
do
{
if(command=="I")
{
Calc expr;
cout<<"your choice is "<<command<<endl;
cout<<"Input the expression:";
cin>>expr;
obj.insert(expr);
}
if(command=="F")
{
cout<<"your choice is "<<command<<endl;
cout<<" operators are in the tree";
}
if(command=="O")
{
cout<<"your choice is "<<command<<endl;
obj.postorderTraversal();
cout<<endl;
}
if(command=="P")
{
cout<<"your choice is "<<command<<endl;
obj.preorderTraversal();
cout<<endl;
}
if(command=="N")
{
cout<<"your choice is "<<command<<endl;
obj.inorderTraversal();
cout<<endl;
}
if(command=="E")
{
cout<<"your choice is "<<command<<endl;
cout<<"Result is";
}
cout<<"I-Input the expression"<<endl;
cout<<"N-InorderTraversal"<<endl;
cout<<"P-PreorderTraversal"<<endl;
cout<<"O-PostorderTraversal"<<endl;
cout<<"F-Number of operator in the tree"<<endl;
cout<<"E-Evaluate the tree"<<endl;
cout<<"Q-Quit"<<endl;
cin>>command;
}while(command!="Q");
system("pause");
}
this is header file: this is also lots of mistake and correction.
#include <iostream>
template<class T>
struct node
{
T info;
node<T> *llink;
node<T> *rlink;
};
template <class T>
class Binarytree
{
public:
const Binarytree<T> & operator=(const Binarytree<T> &);
bool isempty();
void inordertraversal();
void preordertraversal();
void postordertraversal();
int treeheight();
int treenodecount();
int treeleavescount();
void destroytree();
Binarytree(const Binarytree<T> & othertree);
Binarytree();
~Binarytree(){destroy(root);}
protected:
node<T> *root;
private:
void copytree(node<T>* &copiedtreeroot,node<T> *othertreeroot);
void destroy(node<T>* &p);
void inorder(node<T> *p);
void preorder(node<T> *p);
void postorder(node<T> *p);
int height(node<T> *p);
int max(int x,int y);
int nodecount(node<T> *p);
int leavescount(node<T> *p);
};
template <class T>
bool Binarytree<T>::isempty()
{
return(root==NULL);
}
template <class T>
Binarytree<T>::Binarytree()
{
root=NULL;
}
template<class T>
void Binarytree<T>::inordertraversal()
{
inorder(root);
}
template <class T>
void Binarytree<T>::preordertraversal()
{
preorder(root);
}
template <class T>
void Binarytree<T>::postordertraversal()
{
postorder(root);
}
template <class T>
int Binarytree<T>::treeheight()
{
return height(root);
}
template <class T>
int Binarytree<T>::treeleavescount()
{
return leavescount(root);
}
template <class T>
void Binarytree<T>::inorder(node<T> *p)
{
if(p!=NULL)
{
inorder(p->llink);
cout<<p->info<<" ";
inorder(p->rlink);
}
}
template <class T>
void Binarytree<T>::preorder(node<T> *p)
{
if(p!=NULL)
{
cout<<p->info<<" ";
preorder(p->llink);
preorder(p->rlink);
}
}
template <class T>
void Binarytree<T>::postorder(node<T> *p)
{
if(p!=NULL)
{
postorder(p->llink);
postorder(p->rlink);
cout<<p->info<<" ";
}
}
template <class T>
int Binarytree<T>::height(node<T> *p)
{
if(p==NULL)
return 0;
else
return 1+max(height(p->llink),height(p->rlink));
}
template <class T>
int Binarytree<T>::max(int x,int y)
{
if(x>=y)
return x;
else
return y;
}
template <class T>
void Binarytree<T>::destroy(node<T> * &p)
{
if(p!=NULL)
{
destroy(p->llink);
destroy(p->rlink);
delete p;
p=NULL;
}
}
template <class elemType>
void bSearchTreeType<elemType>::insert(const elemType& item)
{
node<T> *p, *q, *r;
r = newnode<elemType>;
r.info = item;
r.llink = rlink =NULL;
if(root==NULL)
root=r;
else
p=root;
while(p!=NULL){
q = p;
if(p.info == item)
{
cerr<<"Dublicates are not allowed";
return ;
}
if(item<p.info)
p=p.llink;
else
p=p.rlink;
}
if(item < q.info)
q.llink=r;
else
q.rlink = r;
}