#include <stdio.h>
#include <stdlib.h>
typedef char ElenType;
typedef struct BiTNode{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
//创建一棵二叉树,约定用户遵照前序遍历的方式输入数据
CreateBiTree(BiTree *T){
char c;
scanf("%c",&c);
if('-'== c){//这个是分隔符AB-C--DE---
*T= NULL;
}
else{
*T=(BiTNode *)malloc(sizeof(BiTNode));
(*T)->data =c;
CreateBiTree(&(*T)->lchild);
CreateBiTree(&(*T)->rchild);
}
}
//访问二叉树节点的具体操作
visit(char c,int level){
printf("%c be located %d \n",c,level);
}
//遍历二叉树
PreOrderTreaverse(BiTree T,int level){
if(T){
visit(T->data,level);
PreOrderTreaverse(T->lchild,level+1);
PreOrderTreaverse(T->rchild,level+1);
}
}
int main(void)
{
int level =1;
BiTree T = NULL;
CreateBiTree(&T);
PreOrderTreaverse(T,level);
return 0;
/*
*结果
A be located 1
B be located 2
C be located 3
D be located 2
E be located 3
*/
}
结果