#include <iostream>
#include <conio.h>
typedef struct TNode
{
int x;
TNode *pLeft;
TNode *pRight;
}TNode;
typedef TNode *tree;
void createtree(tree &t)
{
t=NULL;
}
TNode *create(int x)
{
TNode * p;
p=new TNode;
if(p==NULL) exit (1);
p->x=x;
p->pLeft=p->pRight=NULL;
return p;
}
void setLeft(TNode *p,int x)
{
if(p==NULL)
printf("\nKhong co nut \n ");
else
if(p->pLeft!=NULL)
printf("p da co nut ben trai \n");
else
p->pLeft=create(x);
}
void setRight(TNode *p,int x)
{
if(p==NULL)
printf("\nKhong co nut \n ");
else
if(p->pRight!=NULL)
printf("p da co nut ben trai \n");
else
p->pRight=create(x);
}
int add(tree &t,TNode *p)
{
p=new TNode ;
if(t)
{
if(t->x==p->x)
return 0;
else if(t->x>p->x)
return add(t->pLeft,p);
else
return add(t->pRight,p);
}
t=p;
return 1;
}
void NLR(TNode *p)
{
if(p!=NULL)
{
printf("%d",p->x);
NLR(p->pLeft);
NLR(p->pRight);
}
}
void LNR(TNode *p)
{
if(p!=NULL)
{
LNR(p->pLeft);
printf("%d",p->x);
LNR(p->pRight);
}
}
void main()
{
int x;
TNode *p;
tree t;
while (true)
{
printf("\Nx: ");
scanf("%d",&x);
if(x<0) break;
create(x);
}
LNR;
getch();
}
e code hơi tệ k biết sai chỗ nào mong được sự giúp đỡ
#include <conio.h>
typedef struct TNode
{
int x;
TNode *pLeft;
TNode *pRight;
}TNode;
typedef TNode *tree;
void createtree(tree &t)
{
t=NULL;
}
TNode *create(int x)
{
TNode * p;
p=new TNode;
if(p==NULL) exit (1);
p->x=x;
p->pLeft=p->pRight=NULL;
return p;
}
void setLeft(TNode *p,int x)
{
if(p==NULL)
printf("\nKhong co nut \n ");
else
if(p->pLeft!=NULL)
printf("p da co nut ben trai \n");
else
p->pLeft=create(x);
}
void setRight(TNode *p,int x)
{
if(p==NULL)
printf("\nKhong co nut \n ");
else
if(p->pRight!=NULL)
printf("p da co nut ben trai \n");
else
p->pRight=create(x);
}
int add(tree &t,TNode *p)
{
p=new TNode ;
if(t)
{
if(t->x==p->x)
return 0;
else if(t->x>p->x)
return add(t->pLeft,p);
else
return add(t->pRight,p);
}
t=p;
return 1;
}
void NLR(TNode *p)
{
if(p!=NULL)
{
printf("%d",p->x);
NLR(p->pLeft);
NLR(p->pRight);
}
}
void LNR(TNode *p)
{
if(p!=NULL)
{
LNR(p->pLeft);
printf("%d",p->x);
LNR(p->pRight);
}
}
void main()
{
int x;
TNode *p;
tree t;
while (true)
{
printf("\Nx: ");
scanf("%d",&x);
if(x<0) break;
create(x);
}
LNR;
getch();
}
e code hơi tệ k biết sai chỗ nào mong được sự giúp đỡ
Comment