bạn xem dùm mình code này với. mình làm như bạn mà nó ko xuất ra đc. fix dùm mình nha
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node
{
int data;
node *left,*right;
};
node* ptree;
void init(node* &ptree)
{
ptree=NULL;
}
void createNode(node* ptree,int x)
{
if(ptree==NULL) return;
node* p=new node;
p->data=x;
p->left=NULL;
p->right=NULL;
}
void insertNode(node* &ptree, int n)
{
if(ptree==NULL)
createNode(ptree,n);
else
if(ptree->data < n)
insertNode(ptree->right,n) ;
else
if(ptree->data > n)
insertNode(ptree->left,n) ;
}
void nhap(node* &ptree)
{
int n;
int x;
printf("nhap tong so nut: ");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("\nNhap nut %d: ",i+1);
scanf("%d",&x);
insertNode(ptree,x);
}
}
void LNR(node* ptree)
{
if(ptree==NULL) return;
printf("\t%d",ptree->data);
LNR(ptree->left);
LNR(ptree->right);
}
main()
{
node* ptree;
init(ptree);
nhap(ptree);
LNR(ptree);
getch();
}