国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++ 數據結構二叉樹(前序/中序/后序遞歸、非遞歸遍歷)

2020-01-26 13:59:45
字體:
來源:轉載
供稿:網友

C++ 數據結構二叉樹(前序/中序/后序遞歸、非遞歸遍歷)

二叉樹的性質:

二叉樹是一棵特殊的樹,二叉樹每個節點最多有兩個孩子結點,分別稱為左孩子和右孩子。

例:

實例代碼:

#include <iostream> #include <Windows.h> #include <stack> using namespace std;  template <class T> struct BinaryTreeNode {  int _data;  BinaryTreeNode<T>* _left; //左孩子  BinaryTreeNode<T>* _right;  //右孩子  BinaryTreeNode(const T& data)   :_data(data)   , _left(NULL)   , _right(NULL)  {} };  template <class T> class BinaryTree {  typedef BinaryTreeNode<T> Node; public:  BinaryTree()   :_root(NULL)  {}   BinaryTree(T* arr, size_t n, const T& invalid=T())  {   size_t index = 0;   _root = _CreatTree(arr, n, invalid, index);  }   void PreOrderR()  //前序遍歷 遞歸  {   _PreOrderR(_root);  }   void PreOrder()  //前序遍歷 非遞歸  {   _PreOrder(_root);  }   void InOrderR() //中序遍歷 遞歸  {   _InOrderR(_root);  }   void InOrder()   //中序遍歷  非遞歸  {   _InOrder(_root);   }   void PostOrderR()  //后序遍歷 左 右 根  遞歸  {   _PostOrderR(_root);   }   void PostOrder()  //后序遍歷 左 右 根  非遞歸  {   _PostOrder(_root);   }   ~BinaryTree()  {}  protected:  //建樹 arr:建樹使用的數組 n:數組大小 invalid:非法值 index:當前下標  Node* _CreatTree(T* arr, size_t n, const T& invalid, size_t& index)  {   Node* root = NULL;   if (index < n && arr[index] != invalid)   {    root = new Node(arr[index]); //根節點    root->_left = _CreatTree(arr, n, invalid, ++index);    root->_right = _CreatTree(arr, n, invalid, ++index);   }   return root;   }   void _PreOrderR(Node* root)  //前序遍歷 遞歸  {   if (root == NULL)   {    return;   }   cout << root->_data << " ";   _PreOrderR(root->_left);   _PreOrderR(root->_right);  }   void _PreOrder(Node* root)  //前序遍歷 非遞歸  {   stack<Node*> tty;   while (root != NULL || !tty.empty())   {    if (root)    {     cout << root->_data << " ";     tty.push(root);     root = root->_left;    }    else    {     Node* temp = tty.top();     tty.pop();     root = temp->_right;    }   }  }   void _InOrderR(Node* root) //中序遍歷 遞歸  {   if (root == NULL)   {    return;   }   _InOrderR(root->_left);   cout << root->_data << " ";   _InOrderR(root->_right);  }   void _InOrder(Node* root) //中序遍歷 非遞歸  {   if (root == NULL)   {    return;   }   stack<Node*> tty;   while (root != NULL || !tty.empty())   {    while (root)    {     tty.push(root);     root = root->_left;    }    //此時出了循環走到了最左葉子節點    Node* temp = tty.top();    tty.pop();    cout << temp->_data << " ";    root = temp->_right;   }  }   void _PostOrderR(Node* root)  //后序遍歷 左 右 根  遞歸  {   if (root == NULL)   {    return;   }   _PostOrderR(root->_left);   _PostOrderR(root->_right);   cout << root->_data << " ";  }   void _PostOrder(Node* root)  //后序遍歷 左 右 根  非遞歸  {   if (root == NULL)   {    return;   }   stack<Node*> tty;   Node* PreNode = NULL; //上一個訪問的結點   tty.push(root);   while (!tty.empty())   {    Node* cur = tty.top();    //訪問的當前節點左右孩子均為空或者當前節點左右子樹均已經訪問過    if ((cur->_left == NULL && cur->_right == NULL) || ((PreNode != NULL) && (PreNode == cur->_left || PreNode == cur->_right)))    {     cout << cur->_data << " ";     tty.pop();     PreNode = cur;    }    else    {     if (cur->_right != NULL)     {      tty.push(cur->_right);     }     if (cur->_left != NULL)     {      tty.push(cur->_left);     }    }   }  }  protected:  Node* _root; }; 
#include "源.h"  void Test() {  int array[10] = { 1, 2, 3, '#', '#', 4, '#', '#', 5, 6 };  BinaryTree<int> p(array, sizeof(array) / sizeof(array[0]), '#');  cout << "前序遞歸遍歷: " << "";  p.PreOrderR();  cout << endl;   cout << "前序非遞歸遍歷: " << "";  p.PreOrder();  cout << endl;   cout << "中序遞歸遍歷: " << "";  p.InOrderR();  cout << endl;   cout << "中序非遞歸遍歷: " << "";  p.InOrder();  cout << endl;   cout << "后序遞歸遍歷: " << "";  p.PostOrderR();  cout << endl;   cout << "后序非遞歸遍歷: " << "";  p.PostOrder();  cout << endl; }  int main() {  Test();  system("pause");  return 0; } 


實現效果:

以上就是數據結構二叉樹的詳解,如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 淄博市| 墨竹工卡县| 三穗县| 永德县| 丰台区| 雷波县| 临潭县| 苍溪县| 武平县| 亚东县| 新余市| 伊宁县| 阳山县| 阳城县| 太康县| 正镶白旗| 甘洛县| 揭阳市| 万盛区| 洪江市| 灵宝市| 宝清县| 剑川县| 南通市| 舟曲县| 普洱| 建瓯市| 潼关县| 建湖县| 桂阳县| 会宁县| 沂南县| 长白| 科尔| 朝阳区| 尖扎县| 仁怀市| 余姚市| 柳江县| 山丹县| 绵阳市|