btree-C二叉树

  /*2Tree的基本操作*/
#include <stdio.h>
struct tree
{
    int data;
    struct tree *left;
    struct tree *right;
};
typedef struct tree treenode;
typedef treenode *b_tree;

b_tree creat()
{
    char ch;
    b_tree newnode;
    ch=getchar();
    if (ch==' ') return(NULL);
    else
    {   newnode=(b_tree)malloc(sizeof(treenode));
        newnode->data=ch;
        newnode->left=creat(newnode);
        newnode->right=creat(newnode);
     }
     return newnode;
}

void front_print(b_tree root)
{
    if(root!=NULL)
    {
        printf("[%c]",root->data);
        front_print(root->left);
        front_print(root->right);
     }
}

void middle_print(b_tree root)
{
    if(root!=NULL)
    {
        middle_print(root->left);
        printf("[%c]",root->data);
        middle_print(root->right);
     }
}

void back_print(b_tree root)
{
    if(root!=NULL)
    {
        back_print(root->left);
        back_print(root->right);
        printf("[%c]",root->data);
     }
}

int countleaf(b_tree root,int *i)
{
    if(root==NULL)
        return 0;
    else
    {
        if((root->left==NULL)&&(root->right==NULL))
             (*i)++;
        countleaf(root->left,i);
        countleaf(root->right,i);
        return *i;
     }
}

int locate(b_tree root,char x)
{
    if(root==NULL)
        return 0;
    else
    {
        if(root->data==x)
            printf("/nSuccess!You research data is %c",x);
        locate(root->left,x);
        locate(root->right,x);
    }
}

int t_depth(b_tree root)
{
    int dep1,dep2;
    if(root==NULL)
        return 0;
    else
    {
        dep1=t_depth(root->left);
        dep2=t_depth(root->right);
        if(dep1>dep2)
            return(dep1+1);
        else
            return(dep2+1);
     }
}

void main()
{
    b_tree root=NULL;
    char x;
    int select;
    int depth=0;
    int *i=0;
    int j;
    printf("Please set up a tree./n");
    printf("Notice:if no have crunode please input blank!!!/n");
    root=creat();
    do
    {
        printf("/n(1) Show the tree in a front-root order.");
        printf("/n(2) Show the tree in a middle-root order.");
        printf("/n(3) Show the tree in a back-root order.");
        printf("/n(4) Show the the leafage number of tree.");
        printf("/n(5) Locate a data in the tree.");
        printf("/n(6) Show the depth of the tree.");
        printf("/n(7) Exit");
        printf("/nPlease select one:");
        scanf("%d",&select);
        switch(select)
        {
            case 1: printf("/nThe tree is :");
                    front_print(root);
                    break;
            case 2: printf("/nThe tree is :");
                    middle_print(root);
                    break;
            case 3: printf("/nThe tree is :");
                    back_print(root);
                    break;
           
            case 4: *i=countleaf(root,i);
                    printf("/nThe tree have %d leafage./n",*i);
                    *i=0;
                    break;
            case 5: getchar();
                    printf("Please input the data you want to research: ");
                    x=getchar();
                    locate(root,x);
                    break;
            case 6: depth=t_depth(root);
                    printf("The depth of the tree is %d./n",depth);
                    break;
 
            case 7:
                    break;
        }
    }
    while(select<7);
    printf("/n Press any key to quit...");
    getch();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值