二叉树的基本操作

#include<stdio.h>
#include<malloc.h>

struct node{
    struct node *lc,*rc;
    int data;

};

//创建二叉排序树
struct node *create_sort_tree(struct node *root,int x){

    if(root==NULL){
        printf("直接插入\n");
        root=(struct node *)malloc(sizeof(struct node));
        root->data=x;
        root->lc=NULL;
        root->rc=NULL;
        printf("root->data %d\n",root->data);
        return root;
    }else{
        if(root->data>x){
            printf("向左插入\n");
            root->lc=create_sort_tree(root->lc,x);
        }else if(root->data<x){
            printf("向右插入\n");
            root->rc=create_sort_tree(root->rc,x);
        }else{
            printf("The node has been created last time!");
        }
        printf("ROOT->DATA %d\n",root->data);
        return root;
    }
}

//先序遍历二叉树
void First_put(struct node *root){
    if(root!=NULL){
        printf("%d ",root->data);
        First_put(root->lc);
        First_put(root->rc);
    }

}

//中序遍历二叉树
void Mid_put(struct node *root){
    if(root!=NULL){
        Mid_put(root->lc);
        printf("%d ",root->data);
        Mid_put(root->rc);
    }
}


//后序遍历二叉树
void Last_put(struct node *root){
    if(root!=NULL){
        Last_put(root->lc);
        Last_put(root->rc);
    }
}

//层次遍历二叉树
void Level_put(struct node *root){
    struct node *temp[100];//用一个指针类型的结构体数组存节点
    int i,j;
    i=j=-1;
    int num=0;//记录叶子节点个数
    if(root!=NULL){
        temp[++i]=root;
    }
    while(j<i){//边界条件   当j>=i时表示树中所有节点均遍历完成
        ++j;
        //左右孩子不为空分别入队
        if(temp[j]->lc!=NULL){
            temp[++i]=temp[j]->lc;
        }
        if(temp[j]->rc!=NULL){
            temp[++i]=temp[j]->rc;
        }
        //左右孩子节点为空则为叶子节点
        if(temp[j]->lc==NULL&&temp[j]->rc==NULL){
            printf("叶子:%d\n",temp[j]->data);
            num++;
        }
    }
    printf("叶子节点的个数是 %d\n",num);
    for(int x=0;x<=i;x++){
        printf("%d ",temp[x]->data);
    }
}

//求二叉树深度
int Tree_deepth(struct node *root){
    int lh,rh;
    if(root==NULL){
        return 0;
    }else{
        lh=Tree_deepth(root->lc);
        rh=Tree_deepth(root->rc);
        if(lh>rh){
            return lh+1;
        }else{
            return rh+1;
        }
    }

}

//入口函数
int main(){
    int a[100];
    struct node *root;
    root=NULL;
    for(int i=0;i<8;i++){
        scanf("%d",&a[i]);
        root=create_sort_tree(root,a[i]);
    }
    int high=Tree_deepth(root);
    printf("树的高度是:%d\n",high);
    return 0;

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉树是一种非常重要的数据结构,它的基本操作包括创建、销毁、遍历、查找等。下面是二叉树基本操作的实现方法: 1. 创建二叉树:通过前序遍历的数组构建二叉树,其中 '#' 表示空节点。具体实现方法可以参考引用中的 BinaryTreeCreate 函数。 2. 销毁二叉树:遍历二叉树,依次释放每个节点的内存空间。具体实现方法可以参考引用中的 BinaryTreeDestory 函数。 3. 遍历二叉树二叉树的遍历包括前序遍历、中序遍历、后序遍历和层序遍历。具体实现方法可以参考引用中的 BinaryTreePrevOrder、BinaryTreeInOrder、BinaryTreePostOrder 和 BinaryTreeLevelOrder 函数。 4. 查找二叉树节点:在二叉树中查找值为 x 的节点,具体实现方法可以参考引用中的 BinaryTreeFind 函数。 5. 计算二叉树节点个数:计算二叉树中节点的个数,具体实现方法可以参考引用[2]中的 BinaryTreeSize 函数。 6. 计算二叉树叶子节点个数:计算二叉树中叶子节点的个数,具体实现方法可以参考引用中的 BinaryTreeLeafSize 函数。 7. 计算二叉树第 k 层节点个数:计算二叉树中第 k 层节点的个数,具体实现方法可以参考引用中的 BinaryTreeLevelKSize 函数。 8. 判断二叉树是否是完全二叉树:判断二叉树是否是完全二叉树,具体实现方法可以参考引用中的 BinaryTreeComplete 函数。 9. 计算二叉树的深度:计算二叉树的深度,具体实现方法可以参考引用中的 BinaryTreeDeep 函数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值