千家信息网

二叉树的操作有哪些呢

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,本篇文章为大家展示了二叉树的操作有哪些呢,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。之前实现过二叉树的创建,非递归遍历和递归遍历。现在添加一些其他的操作,包
千家信息网最后更新 2026年11月01日二叉树的操作有哪些呢

本篇文章为大家展示了二叉树的操作有哪些呢,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

之前实现过二叉树的创建,非递归遍历和递归遍历。现在添加一些其他的操作,包括:

  1. 销毁一棵树

  2. 计算树的深度(高度)

  3. .计算叶子节点的个数

  4. 计算所有节点的个数

  5. 复制二叉树

具体见代码:

#include #include typedef struct Node{    int data;    struct Node* lchild;    struct Node* rchild;}Node;//创建树Node* create_tree(){    int _data;    scanf("%d",&_data);    if(_data==100)    {        return NULL;    }    Node* root=(Node*)malloc(1*sizeof(Node));    if(root==NULL)    {        return ;    }    root->data=_data;    root->lchild=create_tree();    root->rchild=create_tree();    return root;}// 前序遍历void pre_print(Node* root){    if(root==NULL)    {        return ;    }    printf("%d\t",root->data);    pre_print(root->lchild);    pre_print(root->rchild);}/****************************************************///销毁一棵树void free_tree(Node* root){    if(root==NULL)        return ;    free_tree(root->lchild);    free_tree(root->rchild);    free(root);    root=NULL;}//计算树的深度(高度)int depth(Node* root){    if(root==NULL)        return 0;    int l=depth(root->lchild);    int r=depth(root->rchild);    if(l>r)    {        return l+1;    }    return r+1;}//计算叶子节点的个数int count_leaf(Node* root){    if(root==NULL)    {        return 0;    }    if(root->lchild==NULL && root->rchild==NULL)    {        return 1;    }    else    {        return count_leaf(root->lchild)+count_leaf(root->rchild);    }}//计算所有节点的个数int count_all_node(Node* root){    if(root==NULL)    {        return 0;    }    return count_all_node(root->lchild)+count_all_node(root->rchild)+1;}//复制二叉树Node* copy_tree(Node* root){    if(root==NULL)    {        return NULL;    }    Node* l_tree,*r_tree,*root_tree;    l_tree=copy_tree(root->lchild);    r_tree=copy_tree(root->rchild);    root_tree=malloc(1*sizeof(Node));    root_tree->data=root->data;    root_tree->lchild=l_tree;    root_tree->rchild=r_tree;    return root_tree;}int main(int argc, char const *argv[]){    Node* root=create_tree();    pre_print(root);    printf("\n");    printf("Depth:%d\n",depth(root));    printf("count_leaf:%d\n",count_leaf(root));    printf("count_all_node:%d\n",count_all_node(root));    Node* copy_root=copy_tree(root);    pre_print(root);    printf("\n");    return 0;}

上述内容就是二叉树的操作有哪些呢,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。

个数 节点 内容 叶子 技能 深度 知识 高度 一棵树 递归 简明 简明扼要 代码 就是 文章 更多 篇文章 行业 资讯 资讯频道 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 天津市摩尔互联网科技 重生之互联网科技帝国 致聘互联网科技 奔汉互联网科技 四川狮子座互联网科技 天扬科技互联网广告 山东吉时兔互联网科技有限公司 互联网 科技馆 创业天下互联网科技有限公司 成都互联网科技公司100强 知域互联网科技有限公司大骗子 杭州壁岐互联网科技有限公司 呼和浩特新华互联网科技学院 996高科技互联网公司 互联网在线教育科技创业 众安科技工业互联网合作 小米互联网科技园入驻 互联网科技股为什么大涨 互联网科技手机排行榜 互联网科技收购价值 精品咖啡豆 广东万易互联网科技有限公司惠州分公司 互联网科技工作委员会副主任毛军 互联网最新的科技有限公司 科技互联网创业计划书 互联网科技类展会 互联网科技有哪些公司 恒生互联网科技指数跳水 艺电能源互联网科技有限公司招聘 深圳互联网科技园地址
0