#include <stdio.h>
#include<string.h>
#include<stdlib.h>
//二叉树数据结构
typedef struct BiTNode{
char data; //存数据
struct BiTNode *lchild; //指向左子结点的指针
struct BiTNode *rchild; //指向右子结点的指针
}BiTNode,*BiTree;//别名bitnode bitree
//结点数据结构
typedef struct NODE
{
int data;
struct NODE *lch, *rch;
} NODE;
void CreateBiTree(BiTree &T)//根据输入的字符串,创建二叉树。
{
char ch;
scanf("%c",&ch);
if(ch=='#')T=NULL;
else{
//T=(BiTNode *)malloc(sizeof(BiTNode));!不行
T= new BiTNode;
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
//访问结点,输出
void visit(BiTNode *pp)
{
printf("%c ",pp->data);
}
//先序遍历
void PreOrder(BiTree T){
if (T == NULL) return;
visit(T);
PreOrder(T->lchild);
PreOrder(T->rchild);
}
//中序遍历
void InOrder(BiTree T){
if(T == NULL) return;
//T=
InOrder(T->lchild);
visit(T);
InOrder(T->rchild);
}
//后序遍历
void PostOrder(BiTree T){
if(T == NULL) return;
PostOrder(T->lchild);
PostOrder(T->rchild);
visit(T);
}
//树深度
int TreeDepth(BiTree T){
if(T == NULL) return 0;
int leftD=TreeDepth(T->lchild);
int rightD=TreeDepth(T->rchild);
if (leftD>rightD) return leftD+1;
else return rightD+1;
}
int main()
{
BiTree T;
printf("ok/n");
CreateBiTree(T);
InOrder(T);
printf("/n%d",TreeDepth(T));