zoukankan      html  css  js  c++  java
  • [资料]二叉树各种遍历方法

    二叉树中序、后序和先序的递归遍历:
    inorder中序遍历:
    template<typename NT>
    void inorder(NT* root, void (*visit)(NT*))
    {
       if(root != 0)
       {
          inorder(root->lc_, visit); //中序遍历左子树;         
          visit(root); //访问根节点; 
          inorder(root->rc_, visit); //中序遍历右子树;
       }
    }

    postorder后续遍历:
    template<typename NT>
    void postorder(NT* root, void (*visit)(NT*))
    {
       if(root != 0)
       {
          postorder(root->lc_, visit); //后序遍历左子树; 
          postorder(root->rc_, visit); //后续遍历右子树;
          visit(root); //访问根节点;
       }
    }

    preorder先序遍历:
    template<typename NT>
    void preorder(NT* root, void (*visit)(NT*))
    {
       if(root != 0)
       {
          visit(root); //访问根节点;
          preorder(root->lc_, visit); //先序遍历左子树;
          preorder(root->rc_, visit); //先序遍历右子树;
       }
    }
    /*************************************************************************/

    /***********************************************************************/
    levelorder按层遍历方式(levelorder)

    template<typename NT>
    void levelorder(NT* root, void (*visit)(NT*))
    {
       if(root == 0)
          return;
       std::queue<NT*> q;
       q.push(root);
       while(!q.empty())
       {
          NT* t = q.front();
          q.pop();
          if(t->haslc())
             q.push(t->lc_);
          if(t->hasrc())
            q.push(t->rc_);
          visit(t);
       }
    } //~void levelorder(...)
    /****************************************************************/

    /********************************************************/
    inorder,preorder,postorder,levelorder函数的应用例子:

    #include <iostream>
    template<typename NT>
    void print(NT* p)
    {
       std::cout<< p->value()<< " ";
    }
    int main(void)
    {
       typedef Btnode<char> NT;
       NT i('I', 0, 0); NT h('H', 0, 0);
       NT f('F', &h, &i); NT g('G', 0, 0);
       NT e('E', &g, 0); NT d('D', 0, 0);
       NT c('C', 0, &f); NT b('B', &d, &e);
       NT a('A', &b, &c); NT* root = &a;
       inorder(root,&print<NT>); cout<< endl;
       preorder(root,&print<NT>); cout<< endl;
       postorder(root,&print<NT>); cout<< endl;
       levelorder(root,&print<NT>); cout<< endl;
       cout<<" height= "<<height(root)<<endl;
       cout<<"number of nodes= "<<size(root)<<endl;
       return 0;
    }
    /****************************************************/

    后续递归遍历应用:求二叉树的高度和节点个数;
    template<typename NT>
    int height(NT const* root)
    {
       if(root == 0)
          return 0;
       int ld = height(root->lc_);
       int rd = height(root->rc_);
          return 1 + std::max(ld, rd);
    }

    template<typename NT>
    int size(NT const* root)
    {
       if(root == 0)
          return 0;
       return 1 + size(root->lc_)+ size(root->rc_);
    }
  • 相关阅读:
    JavaScript按纯数字排序
    用jQuery监听浏览器窗口的变化
    jquery-jtemplates.js模板应用
    art-template模板应用
    JavaScript判断当前手机是Android还是iOS系统
    JavaScript数组转字符串,字符串转数组
    JavaScript数字转字符串,字符串转数字
    Play framework 安装
    JQuery判断数组中是否包含某个字符串
    js获取页面宽度高度及屏幕分辨率
  • 原文地址:https://www.cnblogs.com/hust_wsh/p/2217642.html
Copyright © 2011-2022 走看看