typedef struct Bintree
{
int value;
struct Bintree *right;
struct Bintree *left;
}BINTREE;
BINTREE * CreateTree()
{
BINTREE *root = NULL;
int v;
cout<<"输入一个节点数值(0 表示结束):"<<endl;
cin>>v;
while(v != 0)
{
BINTREE *node;
node = (BINTREE *)malloc(sizeof(BINTREE));
node->value = v;
node->left = NULL;
node->right = NULL;
InsertNode(root, node, &root);
cout<<"输入一个节点数值(0 表示结束):"<<endl;
cin>>v;
}
return root;
}
void InsertNode(BINTREE *root, BINTREE *node, BINTREE **proot) // 借助双重指针实现在函数体内改变变量值
{
if(root != NULL)
{
if(node->value < root->value)
InsertNode(root->left, node, &root->left);
else
InsertNode(root->right, node, &root->right);
}
else
{
*proot = node;
}
}
你只要保证输入节点时是左右子树同时增长的就可以了
温馨提示:答案为网友推荐,仅供参考