#include "stdio.h"
#include "stdlib.h"
#include "mystack.h"
/*typedef struct BinTreeNode
{ char info;
struct BinTreeNoden *lchild;
struct BinTreeNoden *rchild;
}*BinTree;*/
//创建树
create_pre(BinTree *t)
{
char ch;
scanf("%c",&ch);
if (ch==' ') *t=NULL;
else
{ *t=(struct BinTreeNode *)malloc(sizeof(struct BinTreeNode));
if (t==NULL) { printf("NOT Malloc!!!");
exit(0);
}
else
{ (*t)->info=ch;
create_pre(&((*t)->lchild));
create_pre(&((*t)->rchild));
}
}
}
//以下为递归的三个周游方式
//递归先根次序周游
void preorder(BinTree t)
{
if (t==NULL) return;
printf("%c",t->info);
preorder(t->lchild);
preorder(t->rchild);
}
//递归中序周游
void inOrder(BinTree t)
{
if(t==NULL)
return;
inOrder(t->lchild);
printf("%c",t->info);
inOrder(t->rchild);
}
//递归后根次序周游
void postOrder(BinTree t)
{
if(t == NULL)
return;
postOrder(t->lchild);
postOrder(t->rchild);//递归后根次序周游
printf("%c",t->info);
}
//以下为非递归的三种周游方式
//非递归先根次序周游
void npreorder(BinTree t)
{
BinTree c;
create_seq(100);
push(t);
while (!(pastack->t==-1)) {
c = pop();
while (c != NULL) {
printf("%c",c->info);
if (c->rchild!=NULL) push(c->rchild);
c=c->lchild;
}
}
}
//非递归中序周游
void ninOrder(BinTree t)
{
BinTree c=t;
create_seq(100);
//push(t);
do{
while(c!=NULL)
{
push(c);
c=c->lchild;
}
c=pop();
printf("%c",c->info);
c=c->rchild;
}while (!(pastack->t==-1));
}
void nPostOrder(BinTree t)
{
BinTree c=t,q=NULL;
create_seq(100);
while(c!=NULL)
{
push(c);
c=c->lchild;
}
while (isEmetyStack_seq())
{
c=pop();
if(c->rchild == q||c->rchild==NULL)
{
printf("%c",c->info);
q=c;
}
else{
push(c);
c=c->rchild;
while(c!=NULL)
{
push(c);
c=c->lchild;
}
}
}
}
int leaf(BinTree t)
{
if(!t)
{return 0; } //空树,无叶子
else if(!t->lchild && !t->rchild)
{return 1;
}
else
{return (leaf(t->lchild) + leaf(t->rchild));
}
}
int leafnode(BinTree t)
{
if(t!=NULL)
{
if((t->lchild==NULL) && (t->rchild==NULL))
printf("%c",t->info);
leafnode(t->lchild);
leafnode(t->rchild);
}
}
int TreeDeep(BinTree T)
{
int deep = 0;
if (T != NULL)
{
int leftdeep = TreeDeep(T->lchild);//递归求深度
int rightdeep = TreeDeep(T->rchild);
deep = leftdeep >= rightdeep?leftdeep+1:rightdeep+1;
}
return deep;
}
main()
{
BinTree root;
int c;
while(1)
{printf("\n================================");
printf("\n MENU ");
printf("\n1 Create ");
printf("\n2 Preorder(recursion) ");
printf("\n3 Inorder(recursion) ");
printf("\n4 Pastorder(recursion) ");
printf("\n5 Preorder(non-recursion) ");
printf("\n6 Inorder(non-recursion) ");
printf("\n7 Pastorder(non-recursion) ");
printf("\n8 TreeDepth ");
printf("\n9 Leafcount ");
printf("\n10 Leafnode ");
printf("\n0 Exit ");
printf("\n================================");
printf("\nPlease choose menu(0-10):");
scanf("%d",&c);
switch(c)
{ case 1: printf("\nPreorder Create Tree:");
create_pre(&root);
printf("Success!");
break;
case 2: printf("\nPreorder:");
preorder(root);
break;
case 3:
printf("\ninorder:");
inOrder(root);
break;
case 4:
printf("\npastorder:");
postOrder(root);
break;
case 5: printf("\nNon-recursion Preorder:");
npreorder(root);
break;
case 6: printf("\nNon-recursion inorder:");
ninOrder(root);
break;
case 7: printf("\nNon-recursion PostOrder:");
nPostOrder(root);
break;
case 8: printf("\nThe number is:");
c=leaf(root);
printf("%d",c);
break;
case 9:
printf("\nThe leafnode is :");
leafnode(root);
break;
case 10:
printf("\nThe depth is:");
c = TreeDeep(root);
printf("%d",c-2);
break;
default:exit(0);
}
}
}
qq:1351006594