#include "stdio.h"
#include <stdio.h>
#include "stdlib.h"
#include "string.h"
#include <iostream>
#define NULL 0
typedef char DataType;
typedef struct BiNode
{
DataType data;
struct BiNode *lchild, *rchild;
} BiNode,*BiTree;
BiTree root;
int CreatBiTree(BiTree &T)
{
char c;
scanf("%c",&c);
if (c=='#') T = NULL;
else {
T = (BiNode *)malloc(sizeof(BiNode));
T->data = c;
CreatBiTree (T->lchild);
CreatBiTree (T->rchild);
}
}
typedef BiTree ElementType;
typedef struct SNode *Stack;
typedef struct SNode{
ElementType Data;
struct SNode *Next;
}SNode;
Stack CreateStack(){
Stack S;
S=(Stack)malloc(sizeof(struct SNode));
S->Next=NULL;
return S;
}
int IsEmpty(Stack S){
return(S->Next==NULL);
}
void Push(ElementType item,Stack S){
struct SNode *TmpCell;
TmpCell=(struct SNode *)malloc(sizeof(struct SNode));
TmpCell->Data=item;
TmpCell->Next=S->Next;
S->Next=TmpCell;
}
ElementType Pop(Stack S){
struct SNode *FirstCell;
ElementType TopElem;
if(IsEmpty(S)){
printf("堆栈空\n");
return NULL;
}else{
FirstCell=S->Next;
S->Next=FirstCell->Next;
TopElem=FirstCell->Data;
free(FirstCell);
return TopElem;
}
}
void CountLeaf0 (BiTree T, int& count){
if ( T ) {
if ((!T->lchild)&& (!T->rchild))
count++;
CountLeaf0( T->lchild, count);
CountLeaf0( T->rchild, count);
}
}
void CountLeaf1 (BiTree T, int& count){
if ( T ) {
if (( (!T->lchild)&&(T->rchild) )||( (!T->rchild)&&(T->lchild) ))
count++;
CountLeaf1( T->lchild, count);
CountLeaf1( T->rchild, count);
}
}
void CountLeaf2 (BiTree T,int& count){
if ( T ) {
if ((T->lchild)&&(T->rchild))
count++;
CountLeaf2( T->lchild, count);
CountLeaf2( T->rchild, count);
}
}
int Depth (BiTree T ){
int depthval,depthLeft,depthRight;
if ( !T )
depthval = 0;
else {
depthLeft = Depth( T->lchild );
depthRight= Depth( T->rchild );
depthval = 1 + (depthLeft > depthRight ?
depthLeft : depthRight);
} return depthval;
}
void DestroyBiTree(BiTree &T)
{
if (T) return;
else {
DestroyBiTree(T->lchild);
DestroyBiTree(T->rchild);
free(T);
}
}
void PreOderTraversal(BiTree BT){
BiTree T=BT;
Stack S=CreateStack();
while(T||!IsEmpty(S)){
while(T){
printf("%c",T->data);
Push(T,S);
T=T->lchild;
}
T=Pop(S);
T=T->rchild;
}
};
void InOderTraversal(BiTree BT){
BiTree T=BT;
Stack S=CreateStack();
while(T||!IsEmpty(S)){
while(T){
Push(T,S);
T=T->lchild;
}
T=Pop(S);
printf("%c",T->data);
T=T->rchild;
}
};
void PostOderTraversal(BiTree BT){
BiTree T=BT;
BiTree TempT=NULL;
Stack S=CreateStack();
while(T||!IsEmpty(S)){
while(T){
Push(T,S);
T=T->lchild;
}
T=Pop(S);
Push(T,S);
if(T->rchild==NULL||T->rchild==TempT){
printf("%c",T->data);
TempT=T;
Pop(S);
T=NULL;
}
else
T=T->rchild;
}
}
main()
{
printf("请输入您先建立的二叉树\n");
BiTree T;
CreatBiTree(T);
printf("创建成功");
printf("请输入您想输入的操作\n1 先序遍历\n2 中序遍历\n");
printf("3 后序遍历\n4 叶子节点数\n5 结点度数为1\n6 ");
printf("结点度数为2\n7 树的高度\n8 清空一棵树\n9 操作台\n");
printf("\n");
int G;
do{
scanf("%d",&G);
if(G==1){
printf("你建立的树先序遍历为:\n");
PreOderTraversal(T);
printf("\n");
}
else if(G==2){
printf("你建立的树中序遍历为:\n");
InOderTraversal(T);
printf("\n");
}
else if(G==3){
printf("你建立的树后序遍历为:\n");
PostOderTraversal(T);
printf("\n");
}
else if(G==4){
int l=0;
CountLeaf0(T,l);
printf("你建立的树叶子节点数有%d个:\n",l);
printf("\n");
}
else if(G==5){
int j=0;
CountLeaf1(T,j);
printf("你建立的树中结点度数为1有%d个 :\n",j);
printf("\n");
}
else if(G==6){
int w=0;
CountLeaf2(T,w);
printf("你建立的树中结点度数为2有%d个 :\n",w);
printf("\n");
}
else if(G==7){
int t= Depth(T);
printf("你建立的树的高度为%d :\n",t);
printf("\n");
}
else if(G==8){
DestroyBiTree(T);
printf("清除成功\n");
printf("\n");
}
else if(G==9){
printf("请输入您想输入的操作\n1 先序遍历\n2 中序遍历\n");
printf("3 后序遍历\n4 叶子节点数\n5 结点度数为1\n6 ");
printf("结点度数为2\n7 树的高度\n8 清空一棵树\n9 操作台\n");
printf("\n");
}
}while(G);
}