二叉树的相关操作

  



二叉树:二叉树是一棵特殊的树,二叉树每个节点最多有两个孩子结点,分别称为左孩子和右孩子。

# define _CRT_SECURE_NO_WARNINGS 1

# include   # include   # include   # include using  namespace 性传播疾病;//节点结构   template   class  BinaryTreeNode//节点   {   公众:   BinaryTreeNode (const  T&,数据)   :_data(数据)   _left(空)   _right(空)   {}   T  _data;//值   BinaryTreeNode *, _left;//左子树   BinaryTreeNode *, _right;//右子树   };   template   class  BinaryTree   {   typedef  BinaryTreeNode< T>,节点;   公众:   BinaryTree()//无参构造函数   :_root(空)   {}   BinaryTree (const  T *,,, size_t 大小,const  T&,无效)//构造函数   {   assert ();   size_t  index =, 0;   _root =, _CreateTree(一个,大小,无效的,,指数);   }   BinaryTree (const  BinaryTree,, t)//拷贝构造   {   时间=_root  _Copy (t._root);   }   BinaryTree,,操作符=(const  BinaryTree< T>,, t)//赋值函数   {   if (却;能够!=,,t)   {   BinaryTreeNode *, tmp =, _Copy (t._root);   _Destroy (_root);   时间=_root 温度;   }   return  *;   }   ~ BinaryTree()//析构   {   _Destroy (_root);   时间=_root 零;   }   公众:   void  PrevOrder()//先根遍历   {   cout  & lt; & lt;,“先根遍历:”;   _PrevOrder (_root);   cout  & lt; & lt;, endl;   }   void 为了()/手册/中根遍历   {   cout  & lt; & lt;,“中根遍历:”;   _InOrder (_root);   cout  & lt; & lt;, endl;   }   void 后缀次序()//后根遍历   {   cout  & lt; & lt;,“后根遍历:”;   _PostOrder (_root);   cout  & lt; & lt;, endl;   }   void  LevelOrder()//层次遍历   {   cout  & lt; & lt;,“层次遍历:”;   _LevelOrder (_root);   cout  & lt; & lt;, endl;   }   size_t 大小()//求二叉树的节点的个数   {   return  _Size (_root);   }   size_t 深度()//求二叉树的深度   {   return  _Depth (_root);   }   size_t  LeafSize()//叶子节点个数   {   return  _LeafSize (_root);   }   保护:   节点*,_CreateTree (const  T *,,, size_t 大小,const  T&,无效的,,size_t&,,指数)//索引要传引用,需要更改指数的值   {   节点*,root =,空;//判断数组是否越界和输入的值是否合法   if  (index  & lt;, size&和一个(指数),!=,无效)   {   时间=root  new 节点((指数));//创建根节点   根→_left =, _CreateTree(体积,,,无效的,,+ +指数);//递归创建左子树   根→_right =, _CreateTree(体积,,,无效的,,+ +指数);//递归创建右子树   }   return 根源;//返回根节点   }//void  _PrevOrder(节点*,根)//{////如果节点为空则直接返回//if  (root ==, NULL)//{//返回;//}//cout  & lt; & lt;,根→_data  & lt; & lt;,“,”;//访问根节点//_PrevOrder(根→_left);//递归访问左子树//_PrevOrder(根→_right);//递归访问右子树//}   void  _PrevOrder(节点*,根)   {   stack<节点*祝辞,年代;   if  (root==NULL)   {   返回;   }   s.push(根);   while  (! s.empty ())   {   时间=root  s.top ();   cout  & lt; & lt;,根→_data  & lt; & lt;,“,”;   s.pop ();   if (根→_right)   {   s.push(根→_right);   }   if (根→_left)   {   s.push(根→_left);   }   }   }//void  _InOrder(节点*,根)//{////如果节点为空则直接返回//if  (root ==, NULL)//{//返回;//}//_InOrder(根→_left);//递归访问左子树//cout  & lt; & lt;,根→_data  & lt; & lt;,“,”;//递归访问根节点//_InOrder(根→_right);//递归访问右子树//}   void  _InOrder(节点*,根)   {   if  (root ==, NULL)   {   返回;   }   stack<节点*祝辞,年代;   时间=节点*,cur 根源;   while  (cur  | |, ! s.empty ())   {   while (坏蛋)   {   s.push(坏蛋);   时间=cur  cur→_left;   }   时间=cur  s.top();//将栈顶元素保存,以便后面判断它是否有有孩子   cout  & lt; & lt;, s.top ()→_data  & lt; & lt;,“,”;   s.pop ();   if  (cur→_right ==, NULL)   {   时间=cur 零;   }   其他的   {   时间=cur  cur→_right;   }   }   }//void  _PostOrder(节点*,根)//{//如果(root ==, NULL)//{//返回;//}//_PostOrder(根→_left);//递归访问左子树//_PostOrder(根→_right);//递归访问右子树//cout  & lt; & lt;,根→_data  & lt; & lt;,“,”;//递归访问根节点//}   void  _PostOrder(节点*,根)   {   if  (root ==, NULL)   {   返回;   }   时间=节点*,cur 根源;   节点*,prev =,空;   stack

二叉树的相关操作