二叉树系统

传到网上以后自己备一个优盘,专门存自己的代码库

/************************

2015/6/25 21:06
二叉树的构造(层次输入),输出,结点数量,叶子结点数量,树的高度
************************/
#include <stdio.h>

struct tree
{
    int lchild;
    int data;
    int rchild;
}tree[1000005];
int ok;
int count;
void createtree(int root)
{//创建一颗二叉树 层次遍历输入树的结点,输入0时结束
    int r=root;
    for(int i=1;i<=r;i++)
    {
        int number;
        scanf("%d",&number);
        if(number==0)
        {
            break;
        }
        tree[i].data=number;
         tree[i].lchild=2*i;
    tree[2*i].data=0;
    tree[i].rchild=2*i+1;
    tree[2*i+1].data=0;
    r=2*i+1;
    }

}

void pre(int root)
{ //先序遍历 输出树
    if(tree[root].data==0) return ;
          printf("%d ",tree[root].data);
    pre(tree[root].lchild);

    pre(tree[root].rchild);
}
void in(int root)
{//中序遍历
     if(tree[root].data==0) return ;
    pre(tree[root].lchild);
      printf("%d ",tree[root].data);
    pre(tree[root].rchild);
}

void last(int root)
{//后序遍历
     if(tree[root].data==0) return ;
    pre(tree[root].lchild);
    pre(tree[root].rchild);
          printf("%d ",tree[root].data);
}

int treehigh(int root)
{//输出树的高度
    int lh,rh,h;
    if(tree[root].data==0) h=0;
    else
    {
        lh=treehigh(tree[root].lchild);
        rh=treehigh(tree[root].rchild);
        h=(lh>rh?lh:rh)+1;
    }
    return h;
}

void searchh(int root,int x)
{
    if(ok==1) return ;
    if(tree[root].data==x)
    {
        printf("有这个值哦,地址是:");
        ok=1;
        printf("%d\n",tree+root);
        return ;
    }
    else
    {
        searchh(tree[root].lchild,x);
        searchh(tree[root].rchild,x);
    }
}

void countr(int root)
{
    if(tree[root].data==0)
        return ;
   count++;
    countr(tree[root].lchild);
    countr(tree[root].rchild);
}

int countleaf(int root)
{
    if(tree[root].data==0)
        return 0;
    else if(tree[tree[root].lchild].data==0&&tree[tree[root].rchild].data==0)
       {
            return 1;
       }
    else
        return countleaf(tree[root].lchild)+countleaf(tree[root].rchild);
}


int main(void)
{
    ok=1;

   while(1)
   {
         printf("你要做什么:1(我要创建一棵树)\n2(我想要知道树高)\n3(我要查找一个值)\n4(有几个结点呢)\n5(有几个叶子结点呢)\n6(俺要看看树中都有什么)");
    int operation;
    printf("退出(-1)");
        scanf("%d",&operation);
        if(operation==-1)
            break;
    switch(operation)
    {
    case 1:
       {
            printf("请输入值:(我是层次遍历输入的哦!0位结束标志。)\n");
        createtree(1);

        break;
       }
    case 2:
       {
            printf("亲,你的树高是:");
         int high=treehigh(1);
        printf("%d\n",high);
        break;
       }
    case 3:
       {
            printf("你要查找什么值呢?");
         int x;
        scanf("%d",&x);
        searchh(1,x);
        break;
       }
    case 4:
        {
             count=0;
    printf("结点的个数为:");
     countr(1);
    printf("%d个\n",count);
        break;
        }
    case 5:
       {
            printf("结点的个数为:");
    int count=countleaf(1);
    printf("%d\n",count);
        break;
       }
    case 6:
        {
            printf("你想要怎么输出树中的数呢?\n1(先序遍历)\n2(中序遍历)\n3(后序遍历)");
            int number;
            scanf("%d",&number);
            switch(number)
            {
            case 1:
                printf("我是先序遍历哦!");
                pre(1);
                break;
            case 2:
                printf("我是中序遍历哦!");
                in(1);
                break;
            case 3:
                printf("我是后序遍历哦!");
                last(1);
                break;
            }
        }
    }

   }
    //pre(1);

    //printf("¸ß¶ÈΪ£º");

    return 0;
}
//数据结构 二叉树的输入,查找,输出


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值