#include
#include
typedef struct Node
{
char data;
struct Node *lc;
struct Node *rc;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T)
{
char key;
scanf("%c",&key);
if(key=='0')
{
T=NULL;
}else
{
T=(Node*) malloc (sizeof(Node));
T->data=key;
CreateBiTree(T->lc);
CreateBiTree(T->rc);
}
}
int main()
{
BiTree root;
printf("请先序输入字符(以0表示叶节点下无数据):\n");
CreateBiTree(root);
printf("创建成功");
return 0;
}
请问为什么这样创建二叉树会重复陷入循环,无法弹出创建成功的提示
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
char data;
struct Node *lc;
struct Node *rc;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T)
{
char key;
scanf(" %c", &key); // 前面加空格,跳过空格和换行符
if(key == '0')
{
T = NULL;
}
else
{
T = (Node*) malloc (sizeof(Node));
T->data = key;
CreateBiTree(T->lc);
CreateBiTree(T->rc);
}
}
int main()
{
BiTree root;
printf("请先序输入字符(以0表示叶节点下无数据):\n");
CreateBiTree(root);
printf("创建成功");
return 0;
}
//为二叉树插入头结点
void addhead_IN(BiThrTree* T)
{
BiThrTree head = (BiThrTree)malloc(sizeof(BiThrNode));
head->LTag = Link;
head->RTag = Thread;
head->leftchild = (*T);
BiThrTree p = *T;
//先把中序遍历的第一个结点的前继指针指向头结点
while (p->LTag == Link)
{
p = p->leftchild;
}
p->leftchild = head;
//下面我们让p指向中序遍历序列的最后一个结点
while (p->rightchild != NULL)
{
while (p->LTag == Link)
p = p->leftchild;
while (p->RTag == Thread && p->rightchild != NULL)
{
p = p->rightchild;
}
p = p->rightchild;//进入它的右子树
}
p->rightchild = head;
p->RTag = Thread;
head->rightchild = p;
*T = head;
}