#include<stdio.h>
#include<stdlib.h>
#include<stack>
using namespace std;
typedef struct node{
struct node*lchild;
struct node*rchild;
struct node*parent;
char data;
}Node,*Bit;
Bit creat(){//先序创建
char b;
int t;
scanf("%c",&b);
t=getchar();//吸收空格
if(b=='#')return NULL;
else{
Bit tree=(Bit)malloc(sizeof(Node));
tree->parent=NULL;
tree->data=b;
printf("please input %c lefttree:",b);
tree->lchild=creat();
printf("please input %c righttree:",b);
tree->rchild=creat();
return tree;
}
}
void jian(Node *tree){//建立parents连接
tree->parent=NULL;
if(tree->lchild!=NULL){
tree->lchild->parent=tree;
jian(tree->lchild);
}
if(tree->rchild!=NULL){
tree->rchild->parent=tree;
jian(tree->rchild);
}
else return;
}
void printzhong(Node *tree){//中序打印
if(tree){
printzhong(tree->lchild);
printf("%c\t",tree->data);
printzhong(tree->rchild);
}
}
int main(){
Bit tree;
printf("please input root data:\n");
tree=creat();
printf("success_init_tree\n");
jian(tree);
printf("success contact\n");
printzhong(tree);
}
04-28
1384
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)