#include <stdio.h>
#include <stdlib.h>
typedef struct node{
char data;
node * lchild;
node * rchild;
}BTree;
BTree * create(BTree * p){
char ch;
scanf("%c",&ch);
if(ch != ' '){
p = (BTree *)malloc(sizeof(BTree));
p->data = ch;
p->lchild = create(p->lchild);
p->rchild = create(p->rchild);
return p;
}else{
return NULL;
}
}
void preorder(BTree * p){
if(p != NULL){
printf("%4c",p->data);
preorder(p->lchild);
preorder(p->rchild);
}
}
void inorder(BTree * p){
if(p != NULL){
inorder(p->lchild);
printf("%4c",p->data);
inorder(p->rchild);
}
}
void postorder(BTree * p){
if(p != NULL){
postorder(p->lchild);
postorder(p->rchild);
printf("%4c",p->data);
}
}
int main(){
BTree * p;
p = create(p);
printf("preorder:\n");
preorder(p);
printf("\n");
printf("inorder:\n");
inorder(p);
printf("\n");
printf("postorder:\n");
postorder(p);
printf("\n");
return 0;
}
二叉树的基本操作(递归)
最新推荐文章于 2021-05-20 15:56:32 发布