您好,登錄后才能下訂單哦!
這篇文章主要講解了“c++中如何構建一個先序二叉樹”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“c++中如何構建一個先序二叉樹”吧!
第一、定義BinaryTreeNode 類
#include <iostream> #include <string> #include <queue> using namespace std; template<typename T >class BinaryTree; template <typename T> class BinaryTreeNode { public: friend class BinaryTree<T>; BinaryTreeNode() { data = NULL; lChild = rChild = NULL; } BinaryTreeNode(T newdata) { this->data = newdata; lChild = rChild = NULL; } T getData() { return data; } BinaryTreeNode<T> * getLeftNode() { return lChild; } BinaryTreeNode<T> * getRightNode() { return rChild; } T data; BinaryTreeNode<T>* lChild; BinaryTreeNode<T>* rChild; private: };
View Code
第二、定義BinaryTree 類
template <typename T> class BinaryTree { public: BinaryTreeNode<T> *root; char* p; BinaryTree() { root = NULL; } BinaryTree(T data) { root = new BinaryTreeNode<T>(data); root->lChild = NULL; root->rChild = NULL; } ~BinaryTree() { delete root; } //構建二叉樹并返回 BinaryTreeNode<T>* CreateTree() { BinaryTreeNode<int>* bt = NULL; char t; cin >> t; if (t == '#') { return NULL; } else { int num = t - '0'; bt = new BinaryTreeNode<T>(num); bt->lChild = CreateTree(); bt->rChild = CreateTree(); } return bt; } //先序構建二叉樹 BinaryTreeNode<T>* PreCreateTree() { BinaryTreeNode<int>* bt = NULL; if (this->root == NULL) { cout << "請輸入根節點(#代表空樹):"; } else { cout << "請輸入節點(#代表空樹):"; } char t; cin >> t; if (t == '#') { return NULL; } else { int num = t - '0'; bt = new BinaryTreeNode<T>(num); if (this->root == NULL) { this->root = bt; } cout << bt->data << "的左孩子"; bt->lChild = PreCreateTree(); cout << bt->data << "的右邊孩子"; bt->rChild = PreCreateTree(); } return bt; } void preOderTraversal(BinaryTreeNode<T> *bt); //先序遍歷 void inOrderTraversal(BinaryTreeNode<T> *bt); //中序遍歷 void postOrderTraversal(BinaryTreeNode<T> *bt);//后序遍歷 void levelTraversal(BinaryTreeNode<T> *bt); //逐層遍歷 private: }; template <typename T> void BinaryTree<T>::preOderTraversal(BinaryTreeNode<T> *bt) { if (bt) { cout << bt->data; BinaryTree<T>::preOderTraversal(bt->getLeftNode()); BinaryTree<T>::preOderTraversal(bt->getRightNode()); } } template <typename T> void BinaryTree<T>::inOrderTraversal(BinaryTreeNode<T> *bt) { if (bt) { BinaryTree<T>::inOrderTraversal(bt->getLeftNode()); cout << bt->data; BinaryTree<T>::inOrderTraversal(bt->getRightNode()); } } template <typename T> void BinaryTree<T>::postOrderTraversal(BinaryTreeNode<T> *bt) { if (bt) { BinaryTree<T>::postOrderTraversal(bt->getLeftNode()); BinaryTree<T>::postOrderTraversal(bt->getRightNode()); cout << bt->data; } } template <typename T> void BinaryTree<T>::levelTraversal(BinaryTreeNode<T> *bt) { queue<BinaryTreeNode<T>*> que; que.push(bt); while (!que.empty()) { BinaryTreeNode<T>* proot = que.front(); que.pop(); cout << proot->data; if (proot->lChild != NULL) { que.push(proot->lChild);//左孩子入隊 } if (proot->rChild != NULL) { que.push(proot->rChild);//右孩子入隊 } } }
View Code
第三、主程序運行
#include "pch.h" #include <iostream> #include "BinaryTree.h" int main() { //場景測試2 BinaryTree<int> btree; btree.PreCreateTree();//先序構建二叉樹 cout << "先序遍歷:"; btree.preOderTraversal(btree.root); cout << endl;//先序遍歷 cout << "中序遍歷:"; btree.inOrderTraversal(btree.root); cout << endl;//中序遍歷 cout << "后序遍歷:"; btree.postOrderTraversal(btree.root); cout << endl;//后序遍歷 cout << "逐層序遍歷:"; btree.levelTraversal(btree.root); }
View Code
最終測試運行截圖
感謝各位的閱讀,以上就是“c++中如何構建一個先序二叉樹”的內容了,經過本文的學習后,相信大家對c++中如何構建一個先序二叉樹這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。