新编了一段程序。二叉树中序遍历。但就一个错误,怎么也不行。求助达三大四师兄帮忙啊
#include<stdlib.h>
#include<stdio.h>
struct tree
{struct tree*left;
int data;
struct tree*right;
};
typedef struct tree treenode;
typedef treenode *b_tree
b_tree insert_node(b_tree root,int node)
{
b_tree nnode;
b_tree cnode;
b_tree pnode;
nnode=(b_tree)malloc(sizeof(treenode));
nnode->node=node;
nnode->right=NULL;
nnode->left=NULL;
if(root==NULL)
return nnode;
else
{
cnode=root;
while(cnode!=NULL)
{
pnode=cnode;
if(cnode->data>node)
cnode=cnode->left;
else
cnode=cnode->right;
}
if(pnode->data>node)
pnode->left=nnode;
else
pnode->right=nnode;
}
return root;
}
b_tree create_btree(int*data,int len)
{
b_tree root=NULL;
int i;
for(i=0;i<len;i++)
root=insert_node(root,data[i]);
return root;
}
void inorder(b_tree point)
{
if(point!=NULL)
{
inorder(point->left);
printf("%d",point->data);
inorder(point->right);
}
}
void main()
{
b_tree root=NULL;
inti,j;
int value;
int nodelist[20];
printf("\n please input the letter(exit for 0):\n");
j=0;
scanf("%d",&value);
while(j!=0)
{
nodelist[j]=value;
j=j+1;
scanf("%d",&value);
}
root=create_btree(nodelist,j);
printf("\n the ordertrv result is:\n");
inorder(root);
printf("\n")
}
[ 此贴被kangtalc在2006-12-12 21:57重新编辑 ]