here is my code:
#include<cstdio>
class tree
{
private:
struct node
{
int num;
tree *lft,*rgt;
}*root;
public:
tree();
~tree();
void add(int, tree *,tree *);
void inorder(void);
void preorder(void);
void postorder(void);
};//end of class tree
tree::tree()//constructor
{
root=new node;
root->num=0;
root->lft=NULL;
root->rgt=NULL;
}
tree::~tree(){delete root;}//destructor
void tree::add(int v, tree *L, tree *R)
{
root->num=v;
root->lft=L;
root->rgt=R;
}//end of add()
void tree::inorder(void)
{
if(root!=NULL){
(root->lft)->inorder();
printf("%i",root->num);
(root->rgt)->inorder();
}
}//end of inorder()
void tree::preorder(void)
{
if(root!=NULL){
printf("%i",root->num);
(root->lft)->preorder();
(root->rgt)->preorder();
}
}//end of preorder()
void tree::postorder(void)
{
if(root!=NULL){
(root->lft)->postorder();
(root->rgt)->postorder();
printf("%i",root->num);
}
}//end of postorder()
int main()
{
tree A,B,C,D,E,F,G,H,I;
I.add(8,NULL,NULL);
H.add(6,NULL,NULL);
G.add(7,&H,&I);
F.add(5,NULL,&G);
C.add(6,&F,NULL);
A.add(4,NULL,&C);
D.add(1,NULL,NULL);
E.add(3,NULL,NULL);
B.add(2,&D,&E);
A.add(4,&B,&C);
A.inorder();
printf("\n\n");
A.preorder();
printf("\n\n");
A.postorder();
getchar();
return 0;
}

New Topic/Question
Reply




MultiQuote




|