当前位置: 首页 > news >正文

二叉树的操作

对于二叉树的操作,做一个简单的总结;
Tips:针对于不论什么树的操作,首先须要推断是不是空树
树的结构体
int index = 0;

typedef struct BiTree
{
    int data;
    BiTree* lchild;
    BiTree* rchild;
}*Tree;

创建树的操作:

void createBT(Tree &root,int data[])
{
    int value = data[index++];
    if(value == -1)
    {
        root = NULL;
    }
    else
    {
        root = new BiTree;
        root->data = value;
        createBT(root->lchild,data);
        createBT(root->rchild,data);
    }
}

计算结点个数:

int Count_Leaf(Tree &root)
{
    int count = 0;
    if(!root)
    {
        return count;
    }
    count = Count_Leaf(root->lchild)+Count_Leaf(root->rchild)+1;
    return count;
}

计算树中叶子结点个数

int Count(Tree &root)
{
    if(!root)
        return 0;
    if(root->lchild == NULL && root->rchild == NULL)
        return 1;
    int numLeft = Count(root->lchild);
    int numRight = Count(root->rchild);
    return (numLeft+numRight);
}

二叉树的深度:

int BT_depth(Tree &root)
{
    if(!root)
        return 0;
    int depth_left = BT_depth(root->lchild)+1;
    int depth_right = BT_depth(root->rchild)+1;
    return depth_left>depth_right?

depth_left:depth_right; }

二叉树的bfs遍历:

void Level(Tree &root)
{
    cout << "二叉树的层次遍历" << endl;
    queue<BiTree*> que;
    if(!root)
        return;
    que.push(root);
    while(!que.empty())
    {
        root = que.front();
        que.pop();
        cout << root->data << " ";
        if(root->lchild)
            que.push(root->lchild);
        if(root->rchild)
            que.push(root->rchild);
    }
}

二叉树的dfs遍历:

void dfs(Tree &root)
{
    stack<BiTree*> s;
    if(!root)
        return;
    s.push(root);
    while(!s.empty())
    {
        root = s.top();
        cout << root->data << " ";
        s.pop();
        if(root->rchild)
            s.push(root->rchild);
        if(root->lchild)
            s.push(root->lchild);
    }
}

二叉树第K层结点数:

int K_level(Tree &root,int k)
{
    if(!root || k<1)
        return 0;
    if(k == 1)
        return 1;
    int numLeft = K_level(root->lchild,k-1);
    int numright = K_level(root->rchild,k-1);
    return (numLeft+numright);
}

推断俩个二叉树是否是一样的?

bool Same_BT(Tree &root1,Tree &root2)
{
    if(!root1 && !root2)
        return true;
    if(!root1 || !root2)
        return false;
    bool leftSame = Same_BT(root1->lchild,root2->lchild);
    bool rightSame = Same_BT(root1->rchild,root2->rchild);
    return (leftSame&&rightSame);
}

二叉树的镜像:

BiTree* Mirror(Tree &root)
{
    if(!root)
        return NULL;
    BiTree* left = Mirror(root->lchild);
    BiTree* right = Mirror(root->rchild);
    root->lchild = right;
    root->rchild = left;
    return root;
}

是否是平衡二叉树:

bool isAVL(Tree &root,int &height)
{
    if(!root)
    {
        height = 0;
        return true;
    }
    int heightLeft;
    bool left = isAVL(root->lchild,heightLeft);
    int heightRight;
    bool right = isAVL(root->rchild,heightRight);
    if(left && right && abs(heightLeft-heightRight)<=1)
    {
        height = max(heightLeft,heightRight)+1;
        return true;
    }
    else
    {
         height = max(heightLeft,heightRight)+1;
         return false;
    }
}

操作的main()函数:

int main()
{
    int data[] = {8,6,4,-1,-1,7,-1,-1,10,9,-1,-1,11,-1,-1};
    int data1[] = {8,6,4,-1,-1,7,-1,-1,10,9,-1,-1,11,-1,-1};
    Tree tree;
    Tree tree1;
    createBT(tree,data);
    /*
    //int height;
    if(isAVL(tree,height))
    {
        cout << "isAVL" << endl;
    }*/
    createBT(tree1,data1);

    if(Same_BT(tree,tree1))
    {
        cout<< "这俩二叉树是一样的" << endl;
    }
    else
    {
        cout << "这俩二叉树是不一样的" << endl;
    }
    //cout << "二叉树的叶子节点个数" << endl;
    //cout<<Count_Leaf(tree);
    //cout<<Count(tree)<<endl;
    //cout<<BT_depth(tree);*/
    //Level(tree);

    //cout << endl;
    //Mirror(tree);
    //Level(tree);
    //cout<< endl;
   // dfs(tree);
   //cout<< K_level(tree,3)<<endl;
    return 0;
}

相关文章:

  • jquery 的队列queue
  • CentOS下载
  • 开始学习第一天
  • 电梯演讲
  • Linux kernel Makefile for ctags
  • SVN之 trunk, branches and tags意义
  • Android Java执行Shell命令
  • 简介SQL数据库
  • KeyMob移动广告聚合平台服务_广告聚合平台_工具
  • IDEA的快捷键
  • 仿新浪微盾客户端项目简介三
  • IIS提示“异常详细信息: System.Runtime.InteropServices.ExternalException: 无法执行程序”...
  • linux 公网主机被***了
  • 【Mysql 学习】 MERGE表方面的问题(二)
  • win8 开发之旅(7) --五子棋游戏开发
  • 分享的文章《人生如棋》
  • 《网管员必读——网络组建》(第2版)电子课件下载
  • 〔开发系列〕一次关于小程序开发的深度总结
  • Akka系列(七):Actor持久化之Akka persistence
  • IOS评论框不贴底(ios12新bug)
  • JavaScript创建对象的四种方式
  • JavaScript类型识别
  • NLPIR语义挖掘平台推动行业大数据应用服务
  • Node 版本管理
  • Node.js 新计划:使用 V8 snapshot 将启动速度提升 8 倍
  • React系列之 Redux 架构模式
  • Spark RDD学习: aggregate函数
  • vue从入门到进阶:计算属性computed与侦听器watch(三)
  • 来,膜拜下android roadmap,强大的执行力
  • 盘点那些不知名却常用的 Git 操作
  • 如何使用 OAuth 2.0 将 LinkedIn 集成入 iOS 应用
  • 深度解析利用ES6进行Promise封装总结
  • 什么是Javascript函数节流?
  • 使用 Node.js 的 nodemailer 模块发送邮件(支持 QQ、163 等、支持附件)
  • 手机app有了短信验证码还有没必要有图片验证码?
  • 详解NodeJs流之一
  • kubernetes资源对象--ingress
  • ​​​​​​​​​​​​​​Γ函数
  • ​比特币大跌的 2 个原因
  • #Js篇:单线程模式同步任务异步任务任务队列事件循环setTimeout() setInterval()
  • #pragma 指令
  • $.proxy和$.extend
  • ${ }的特别功能
  • (1)Android开发优化---------UI优化
  • (10)Linux冯诺依曼结构操作系统的再次理解
  • (八)五种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (附源码)springboot码头作业管理系统 毕业设计 341654
  • (一)为什么要选择C++
  • (转)c++ std::pair 与 std::make
  • (转)Linux下编译安装log4cxx
  • (转)memcache、redis缓存
  • ..回顾17,展望18
  • .Net Core 中间件验签
  • .Net 访问电子邮箱-LumiSoft.Net,好用
  • .Net 中Partitioner static与dynamic的性能对比