In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to build a first-order binary tree in C++". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn how to build a first-order binary tree in C++.
First, define the BinaryTreeNode class
# include # include # include using namespace std; templateclass BinaryTree;template class BinaryTreeNode {public: friend class BinaryTree; BinaryTreeNode () {data = NULL; lChild = rChild = NULL;} BinaryTreeNode (T newdata) {this- > data = newdata; lChild = rChild = NULL;} T getData () {return data;} BinaryTreeNode* getLeftNode () {return lChild;} BinaryTreeNode* getRightNode () {return rChild;} T data; BinaryTreeNode* lChild; BinaryTreeNode* rChild Private:}
View Code
Second, define the BinaryTree class
Template class BinaryTree {public: BinaryTreeNode* root; char* p; BinaryTree () {root = NULL;} BinaryTree (T data) {root = new BinaryTreeNode (data); root- > lChild = NULL; root- > rChild = NULL;} ~ BinaryTree () {delete root;} / / build a binary tree and return BinaryTreeNode* CreateTree () {BinaryTreeNode* bt = NULL; char t; cin > > t If (t = ='#') {return NULL;} else {int num = t-'0trees; bt = new BinaryTreeNode (num); bt- > lChild = CreateTree (); bt- > rChild = CreateTree ();} return bt;} / / pre-build binary tree BinaryTreeNode* PreCreateTree () {BinaryTreeNode* bt = NULL; if (this- > root = NULL) {cout t If (t = ='#') {return NULL;} else {int num = t-0mm; bt = new BinaryTreeNode (num); if (this- > root = = NULL) {this- > root = bt;} cout data lChild = PreCreateTree (); cout data rChild = PreCreateTree ();} return bt;} void preOderTraversal (BinaryTreeNode * bt) / / first order traversal void inOrderTraversal (BinaryTreeNode * bt); / / medium order traversal void postOrderTraversal (BinaryTreeNode * bt); / / Post order traversal void levelTraversal (BinaryTreeNode * bt); / / layer-by-layer traversal private:}; template void BinaryTree::preOderTraversal (BinaryTreeNode * bt) {if (bt) {cout data; BinaryTree::preOderTraversal (bt- > getLeftNode ()); BinaryTree::preOderTraversal (bt- > getRightNode ()) } template void BinaryTree::inOrderTraversal (BinaryTreeNode * bt) {if (bt) {BinaryTree::inOrderTraversal (bt- > getLeftNode ()); cout data; BinaryTree::inOrderTraversal (bt- > getRightNode ());} template void BinaryTree::postOrderTraversal (BinaryTreeNode * bt) {if (bt) {BinaryTree::postOrderTraversal (bt- > getLeftNode ()); BinaryTree::postOrderTraversal (bt- > getRightNode ()); cout data;}} template void BinaryTree::levelTraversal (BinaryTreeNode * bt) {queue que Que.push (bt); while (! que.empty ()) {BinaryTreeNode* proot = que.front (); que.pop (); cout data; if (proot- > lChild! = NULL) {que.push (proot- > lChild); / / left child join the team} if (proot- > rChild! = NULL) {que.push (proot- > rChild); / / right child join the team}
View Code
Third, the main program runs
# include "pch.h" # include # include "BinaryTree.h" int main () {/ / scenario test 2 BinaryTree btree; btree.PreCreateTree (); / / build binary tree cout first
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.