Đây là code tạo 1 cây nhị phân! Mình đã kiểm tra kĩ lắm rồi, debug rồi mà mãi vẫn ko tìm ra lỗi nhưng khi chạy thử thì ko đc! Mong các anh chị và các bạn giúp mình với:
#include "stdafx.h"
#include <conio.h>
#include <stdio.h>
typedef struct tree_node
{
int key;
struct tree_node *left,*right;
}Node;
typedef Node *tree;
Node *creatnode(int k)
{
Node *p;
p=new Node;
p->key=k;
p->left==NULL;
p->right=NULL;
return p;
}
int addnode(tree &t,Node *p)
{
if(t)
{
if(t->key==p->key)
return 0;
else
{
if(p->key<t->key)
addnode(t->left,p);
else
addnode(t->right,p);
}
}
else
{
t=p;
return 1;
}
}
void print(tree t)
{
if(t)
{
print(t->left);
printf("%d ",t->key);
print(t->right);
}
}
void main()
{
Node *p;
tree t;
int k;
t=NULL;
do
{
printf("nhap vao khoa k");
scanf("%d",&k);
if(k==0)
break;
p=creatnode(k);
if(addnode(t,p)==1)
printf("nhap thanh cong");
else
printf("so nay ban da nhap roi");
}
while(k!=0);
printf("cay ban vua nhap la");
print(t);
}
#include "stdafx.h"
#include <conio.h>
#include <stdio.h>
typedef struct tree_node
{
int key;
struct tree_node *left,*right;
}Node;
typedef Node *tree;
Node *creatnode(int k)
{
Node *p;
p=new Node;
p->key=k;
p->left==NULL;
p->right=NULL;
return p;
}
int addnode(tree &t,Node *p)
{
if(t)
{
if(t->key==p->key)
return 0;
else
{
if(p->key<t->key)
addnode(t->left,p);
else
addnode(t->right,p);
}
}
else
{
t=p;
return 1;
}
}
void print(tree t)
{
if(t)
{
print(t->left);
printf("%d ",t->key);
print(t->right);
}
}
void main()
{
Node *p;
tree t;
int k;
t=NULL;
do
{
printf("nhap vao khoa k");
scanf("%d",&k);
if(k==0)
break;
p=creatnode(k);
if(addnode(t,p)==1)
printf("nhap thanh cong");
else
printf("so nay ban da nhap roi");
}
while(k!=0);
printf("cay ban vua nhap la");
print(t);
}
Comment