[Help] Cây nhị phân tìm kiếm!

Đâ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);
}

[QUOTE=11520002;60602]Đâ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…
Node *creatnode(int k)
{
Node *p;
p=new Node;
p->key=k;
p->left==NULL;
p->right=NULL;
return p;
}
[/QUOTE]

p->left=NULL;

Hì! Thanks nhiều!!! Mình cứ nghĩ là ko thể sai đc nên khi kiểm tra chủ quan nên tìm ko ra lỗi!!! Thanks bác nhiều!!!:smiley:

#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);

lân sau bạn cho vào thế này cho mọ người dễ xem nha:love:
[QUOTE=11520002;60602]Đâ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);
}[/QUOTE]

Nếu có thêm cái BBCODE Syntax-Highlighter này thì hay quá. Hy vọng Admin thêm vào, Forum IT mà không có cái này thì thấy thiếu thiếu