二叉树常用操作

来源:互联网 发布:数控龙门铣编程 编辑:程序博客网 时间:2024/05/22 02:17
  1. #include <iostream>
  2. #include <string>
  3. //using namespace std;
  4. int n=0,dep1=0,dep2=0,m=0,n1=0,n2=0;
  5. template <class T>
  6. struct BiNode   //二叉树的结点结构
  7. {
  8.   T data;       
  9.   BiNode<T> *lchild, *rchild;
  10. };
  11. template <class T>
  12. class BiTree
  13. {
  14. public:
  15.     BiTree() {root=NULL;}
  16.     BiTree(T ch);             //构造函数,初始化一棵二叉树,其前序序列由键盘输入
  17.     ~BiTree(void);         //析构函数,释放二叉链表中各结点的存储空间
  18.     BiNode<T>* Getroot();  //获得指向根结点的指针
  19.     void PreOrder(BiNode<T> *root);     //前序遍历二叉树
  20.     void InOrder(BiNode<T> *root);      //中序遍历二叉树
  21.     void PostOrder(BiNode<T> *root);    //后序遍历二叉树
  22.     void LeverOrder(BiNode<T> *root);   //层序遍历二叉树
  23.     int  Count(BiNode<T> *root);//计算结点个数
  24.     int  Depth(BiNode<T> *root);//计算深度
  25.     int  leafnum(BiNode<T> *root);//叶子节点个数
  26.     int  full(BiNode<T> *root);
  27. private:
  28.     BiNode<T> *root;         //指向根结点的头指针
  29.     BiNode<T> *Creat(T ch);     //有参构造函数调用
  30.     void Release(BiNode<T> *root);   //析构函数调用 
  31. };
  32. template<class T>
  33. BiTree<T>::BiTree(T ch)
  34. {   cout<<"请输入创建一棵二叉树的结点数据"<<endl;
  35.     this->root = Creat(ch);
  36. }
  37. template <class T>
  38. BiNode<T>* BiTree<T>::Creat(T ch)
  39. {
  40.     BiNode<T>* root;
  41.     //T ch;
  42.     //cout<<"请输入创建一棵二叉树的结点数据"<<endl;
  43.     cin>>ch;
  44.     if (ch=="#") root = NULL;
  45.     else
  46.          root = new BiNode<T>;       //生成一个结点
  47.          root->data=ch;
  48.          root->lchild = Creat(ch);    //递归建立左子树
  49.          root->rchild = Creat(ch);    //递归建立右子树
  50.     } 
  51.     return root;
  52. }
  53. template<class T>
  54. BiTree<T>::~BiTree(void)
  55. {
  56.     Release(root);
  57. }
  58. template<class T>
  59. BiNode<T>* BiTree<T>::Getroot( )
  60. {
  61.     return root;
  62. }
  63. template<class T>
  64. void BiTree<T>::PreOrder(BiNode<T> *root)
  65. {
  66.     if(root==NULL)  return;
  67.     else{       
  68.         cout<<root->data<<" ";
  69.         PreOrder(root->lchild);
  70.         PreOrder(root->rchild);
  71.     }
  72. }
  73. template <class T>
  74. void BiTree<T>::InOrder (BiNode<T> *root)
  75. {
  76.     if (root==NULL)  return;      //递归调用的结束条件             
  77.     else{   
  78.         InOrder(root->lchild);    //中序递归遍历root的左子树
  79.         cout<<root->data<<" ";    //访问根结点的数据域
  80.         InOrder(root->rchild);    //中序递归遍历root的右子树
  81.     }
  82. }
  83. template <class T>
  84. void BiTree<T>::PostOrder(BiNode<T> *root)
  85.     if (root==NULL)   return;       //递归调用的结束条件
  86.     else{   
  87.         PostOrder(root->lchild);    //后序递归遍历root的左子树
  88.         PostOrder(root->rchild);    //后序递归遍历root的右子树
  89.         cout<<root->data<<" ";      //访问根结点的数据域
  90.     }
  91. }
  92. template <class T>
  93. void BiTree<T>::LeverOrder(BiNode<T> *root)
  94. {
  95.     const int MaxSize = 100;
  96.     int front = 0;
  97.     int rear = 0;  //采用顺序队列,并假定不会发生上溢
  98.     BiNode<T>* Q[MaxSize];
  99.     BiNode<T>* q;
  100.     if (root==NULL) return;
  101.     else{
  102.         Q[rear++] = root;
  103.         while (front != rear)
  104.         {
  105.             q = Q[front++];
  106.             cout<<q->data<<" ";         
  107.             if (q->lchild != NULL)    Q[rear++] = q->lchild;        
  108.             if (q->rchild != NULL)    Q[rear++] = q->rchild;
  109.         }
  110.     }
  111. }
  112. template<class T>
  113. void BiTree<T>::Release(BiNode<T>* root)
  114. {
  115.   if (root != NULL){                  
  116.       Release(root->lchild);   //释放左子树
  117.       Release(root->rchild);   //释放右子树
  118.       delete root;
  119.   }
  120. }
  121. template<class T>
  122. int BiTree<T>::Count(BiNode<T> *root)
  123.  if(root==NULL) return 0;
  124.  else
  125.  {
  126.   Count(root->lchild);
  127.   Count(root->rchild);
  128.   n++;
  129.  }
  130.  return n;
  131. }
  132. template <class T>
  133. int BiTree<T>::Depth(BiNode<T> *root)
  134. {
  135.  if (root==NULL) return 0;
  136.    else
  137.    {
  138.      dep1=Depth(root->lchild);//计算左子树深度
  139.      dep2=Depth(root->rchild);//计算右子树深度
  140.      if(dep1>dep2) return dep1+1;
  141.         else return dep2+1;
  142.    }
  143. }
  144. template <class T>
  145. int BiTree<T>::leafnum(BiNode<T> *root)//叶子节点个数
  146. {
  147.  if(root==NULL) return 0;
  148.  else
  149.  {
  150.   if(root->lchild==NULL && root->rchild==NULL)
  151.       m++;
  152.   leafnum(root->lchild);
  153.   leafnum(root->rchild);
  154.  }
  155.   return m;
  156. }
  157. template <class T>
  158. int BiTree<T>::full(BiNode<T> *root)
  159. {
  160.   if(root==NULL) return 0;
  161.     else 
  162.     {
  163.      if(root->lchild==NULL && root->rchild==NULL)
  164.          return 1;
  165.      else if (root->lchild==NULL || root->rchild==NULL)
  166.          return 0;
  167.         else return(full(root->lchild) && full(root->rchild));
  168.     }
  169. }
原创粉丝点击