• 二叉树的递归创建和遍历


       1:  /*
       2:  author:justinzhang
       3:  email:uestczhangchao@gmail.com
       4:  time:2012-8-20 15:52:27
       5:  des.:using non recursive method to traverse binary tree
       6:  */
       7:  #include <iostream>
       8:  using namespace std;
       9:   
      10:  template<typename T> class btree
      11:  {
      12:  public:
      13:      T data;
      14:      btree *lchild, *rchild;
      15:  };
      16:   
      17:  /*    create binary tree, with one parameter.
      18:      the binary tree is inputed through pre order,
      19:      1
      20:     2 3, is input as 1 2 -1 -1 3 -1 -1,(-1 represent nil node);
      21:  */
      22:  btree<double> * create_btr(btree<double> **root)
      23:  {
      24:      double data ;
      25:      cin>>data;
      26:      /* -1.0 represent the nil node */
      27:      if(data == -1.0) 
      28:      {
      29:          *root=NULL;
      30:      }
      31:      else
      32:      {
      33:          *root = new btree<double>();
      34:          (*root)->data = data;
      35:          (*root)->lchild = create_btr(&(*root)->lchild);
      36:          (*root)->rchild = create_btr(&(*root)->rchild);        
      37:      }
      38:      return *root;
      39:  }
      40:   
      41:  /*create binary tree without parameter!*/
      42:  btree<double>* create_btr()
      43:  {
      44:      double data;
      45:      cin >> data;
      46:      if(data == -1)
      47:          return NULL; //return nill node
      48:      else
      49:      {
      50:          btree<double> *T = new btree<double>;
      51:          T->data = data;
      52:          T->lchild = create_btr();
      53:          T->rchild = create_btr();
      54:          return T;
      55:      }
      56:  }
      57:   
      58:  void preorder(btree<double> *r)
      59:  {
      60:      if(r==NULL)
      61:          return;
      62:      cout << r->data << endl;
      63:      preorder(r->lchild);
      64:      preorder(r->rchild);
      65:  }
      66:   
      67:  void inorder(btree<double> *r)
      68:  {
      69:      if(NULL==r)
      70:          return;
      71:      inorder(r->lchild);
      72:      cout << r->data<<endl;
      73:      inorder(r->rchild);
      74:  }
      75:  void postorder(btree<double> *r)
      76:  {
      77:      if(NULL==r)
      78:          return ;
      79:      postorder(r->lchild);
      80:      postorder(r->rchild);
      81:      cout << r->data<<endl;
      82:  }
      83:   
      84:   
      85:  int main()
      86:  {
      87:      btree<double> *root = NULL;
      88:      create_btr(&root);
      89:      //btree<double> *T = create_btr();
      90:      preorder(root);
      91:      inorder(root);
      92:      postorder(root);
      93:      //inorder(T);
      94:      return 0;
      95:  }
  • 相关阅读:
    随笔和文章的区别
    02. 什么是 SOA?
    GOF 23种设计模式(Patterns in Java)
    ASP.NET MVC: how to include <span> in link from Html.ActionLink?
    Head First 02.观察者模式(Observer)
    ActionResult types in MVC2
    探索推荐引擎内部的秘密,第 1 部分: 推荐引擎初探
    Head First 01.策略模式(Strategy Pattern)
    SOA实施:不能忽略商业价值
    nmon analyser——生成 AIX 性能报告的免费工具
  • 原文地址:https://www.cnblogs.com/justinzhang/p/2667187.html
Copyright © 2020-2023  润新知