搜索二叉树遍历不对,帮找出错误

[复制链接]
查看11 | 回复1 | 2011-2-25 17:29:43 | 显示全部楼层 |阅读模式
#include
#include
typedef int datatype;
typedef struct bitree
{

datatype data;

struct bitree *lchild,*rchild;
}bitree;
bitree *create_root()
{

bitree *root;

root=(bitree *)malloc(sizeof(bitree));

scanf("%d",&root->data);

root->lchild=NULL;

root->rchild=NULL;

return root;
}
void add_node(bitree *n,int value)
{

if(valuedata)

{

if(n->lchild!=NULL){

add_node(n->lchild,value);}

}

else{

bitree *lc=(bitree *)malloc(sizeof(bitree));

lc->lchild=NULL;

lc->rchild=NULL;

lc->data=value;

n->lchild=lc;

}

if(value>n->data)
{
if(n->rchild!=NULL){

add_node(n->rchild,value); }

}
else{

bitree *rc=(bitree *)malloc(sizeof(bitree));

rc->lchild=NULL;

rc->rchild=NULL;

rc->data=value;

n->rchild=rc;

}
}
void inorder(bitree *m)
{

if(m!=NULL)

{

inorder(m->lchild);

printf("%d ",m->data);

inorder(m->rchild);

}
}


void preorder(bitree *m)
{

if(m!=NULL)

{


printf("%d ",m->data);

preorder(m->lchild);

preorder(m->rchild);

}
}

main()
{


bitree *root;

root=NULL;

char c;

root=create_root();
while((c=getchar())!='#')
{

int value;


scanf("%d",&value);

add_node(root,value);
}
inorder(root);
preorder(root);
system("pause");
return 0;
}


回复

使用道具 举报

千问 | 2011-2-25 17:29:43 | 显示全部楼层
de#includeusing namespace std;templatestruct BiNode{ T data; BiNode *lchild, *rchild;}; templateclass BiTree{public: BiTree(); ~BiTree(); void PreOrder(BiNode *root); void InOrder(BiNode *root); void PostOrd
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行