数据结构:关于树的各种操作(含菜单)(C/C++)

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Aaron_Liu0730

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值