PDA

View Full Version : Giúp mình bài cây nhị phân(mình mới học).



nkt_se7en
01-05-2009, 14:16
Mình có bài nhập các số thực theo cây nhị phân,không hiểu sao phần in cây đã duyệt nó không in.Code của mình đây:


#include <stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int element;
struct node *left, *right;
};
struct node *root;
void insert(struct node *root,int *x);
void nhap(struct node *root,int *x);
void NLR(struct node *root);
int main()
{
int x;
nhap(root,&x);
NLR(root);
}
void insert(struct node *root,int *x)
{
if(root!=NULL)
{
if(*x==root->element) printf("\n nhap du lieu bi trung.");
else if(*x <(root->element))
insert(root->left,x);
else
insert(root->right,x);
}
else
{
root=(struct node*)malloc(sizeof(struct node));
root->element=*x;
root->left=NULL;
root->right=NULL;
}
}
void nhap(struct node *root,int *x)
{

while(1)
{
printf(" Nhap vao phan tu(trong de thoat):");
scanf("%d",x);
if(*x==0) break;
insert(root,x);
}
}
void NLR(struct node *root)
{
if(root!=NULL)
{
printf("%d",root->element);
NLR(root->left);
NLR(root->right);
}
}


mọi người giúp mình,mình mới học nên chưa rõ lắm.