C++ 數(shù)據(jù)結(jié)構(gòu)二叉樹(前序/中序/后序遞歸、非遞歸遍歷)
C++ 數(shù)據(jù)結(jié)構(gòu)二叉樹(前序/中序/后序遞歸、非遞歸遍歷)
二叉樹的性質(zhì):
二叉樹是一棵特殊的樹,二叉樹每個節(jié)點最多有兩個孩子結(jié)點,分別稱為左孩子和右孩子。
例:
實例代碼:
#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:建樹使用的數(shù)組 n:數(shù)組大小 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]); //根節(jié)點 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; } //此時出了循環(huán)走到了最左葉子節(jié)點 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; //上一個訪問的結(jié)點 tty.push(root); while (!tty.empty()) { Node* cur = tty.top(); //訪問的當前節(jié)點左右孩子均為空或者當前節(jié)點左右子樹均已經(jīng)訪問過 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; }
實現(xiàn)效果:
以上就是數(shù)據(jù)結(jié)構(gòu)二叉樹的詳解,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
上一篇:C語言實現(xiàn)C++繼承和多態(tài)的代碼分享
欄 目:C語言
下一篇:C語言模式實現(xiàn)C++繼承和多態(tài)的實例代碼
本文標題:C++ 數(shù)據(jù)結(jié)構(gòu)二叉樹(前序/中序/后序遞歸、非遞歸遍歷)
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1309.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10深入二叉樹兩個結(jié)點的最低共同父結(jié)點的詳解
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設計- 解析最少換車次數(shù)的問題詳解
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設計-用棧實現(xiàn)表達式求值的方法詳解
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10深入理解二叉樹的非遞歸遍歷
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10C#中split用法實例總結(jié)
- 08-05DEDE織夢data目錄下的sessions文件夾有什