一:目的
用C++實現二叉樹的基本操作,建立和遍歷;
二:介紹
在計算機科學中,二叉樹是每個節點最多有兩個子樹的樹結構。通常子樹被稱作“左子樹”(left subtree)和“右子樹”(right subtree)。二叉樹常被用於實現二叉查找樹和二叉堆。
二叉樹的遍歷方式有三種,前序遍歷,中序遍歷,後序遍歷。
它的前序遍歷順序爲:ABDGHCEIF(規則是先是根結點,再前序遍歷左子樹,再前序遍歷右子樹)
它的中序遍歷順序爲:GDHBAEICF(規則是先中序遍歷左子樹,再是根結點,再是中序遍歷右子樹)
它的後序遍歷順序爲:GHDBIEFCA(規則是先後序遍歷左子樹,再是後序遍歷右子樹,再是根結點)
三:實現
1. 首先創建二叉樹結構,以及二叉樹的類,定義在BinaryTree.h中
2. 具體實現在BinaryTree.cpp中#include <iostream> using namespace std; typedef char ElemType; //定義樹的節點 typedef struct BiNode { ElemType data; //節點的數據類型 BiNode * left; //左子樹 BiNode * right; //右子樹 BiNode(ElemType val) { data = val; left = NULL; right = NULL; } }BiNode,*BiTree; //二叉樹類 class BinaryTree { public: void Create(); //遞歸的創建二叉樹的節點 int getSize(); //遞歸得到樹的節點數目 int getHeight(); //遞歸得到樹的高度 void preOrder(); //前序遍歷 void inOrder(); //中序遍歷 void postOrder(); //後序遍歷 void distroy(); //刪除二叉樹 private: BiTree create(); //遞歸的創建二叉樹的節點 void preOrder(BiTree root); //前序遍歷 void inOrder(BiTree root); //中序遍歷 void postOrder(BiTree root); //後序遍歷 void distroy(BiTree root); //摧毀樹 int getHeight(BiTree root); //遞歸得到樹的高度 void AddNode(const ElemType key,int direction, BiTree root); //添加節點 BiTree m_root; //根節點 int size; //節點總數 };
3. 簡單測試# include "BinaryTree.h" #pragma region 私有成員函數 //添加節點 //key爲要插入的值,direction是從左子樹插入還是右子樹插入,root爲從哪個節點插入 void BinaryTree::AddNode(const ElemType key,int direction, BiTree root) { if(direction == 0) { //從左子樹插入 if(root->left == NULL) root->left = new BiNode(key); else AddNode(key, direction,root->left); } else if(direction == 1) { //從右子樹插入 if(root->right == NULL) root->right = new BiNode(key); else AddNode(key, direction,root->right); } } ////二叉樹的建立,按前序遍歷的方式建立二叉樹 BiTree BinaryTree::create() { BiTree current = NULL; ElemType val; cin >> val;//輸入鍵值 if(val == '#')//標識當前子樹爲空,轉向下一節點 { return NULL; } else { //遞歸的創建左右子樹 size++; current = new BiNode(val); current->left = create(); current->right = create(); return current; } } //刪除二叉樹(private 函數) void BinaryTree ::distroy(BiTree root) { if(root) { distroy(root->left); distroy(root->right); delete root; root = NULL; size = 0; } } //遞歸得到樹的高度 int BinaryTree::getHeight(BiTree root) { if(root == NULL) return 0; int left_height = getHeight(root->left); int right_height = getHeight(root->right); return (left_height>right_height)? (left_height+1):(right_height+1); } //前序遍歷 void BinaryTree::preOrder(BiTree root) { if(root == NULL) return; else { cout << root->data << " --> "; //首先打印根節點 preOrder(root->left); //接着遍歷左子樹 preOrder(root->right); //接着遍歷右子樹 } } //中序遍歷 void BinaryTree::inOrder(BiTree root) { if(root == NULL) return; else { inOrder(root->left); //首先遍歷左子樹 cout << root->data << " --> "; //接着打印根節點 inOrder(root->right); //接着遍歷右子樹 } } //後序遍歷 void BinaryTree::postOrder(BiTree root) { if(root == NULL) return; else { postOrder(root->left); //首先遍歷左子樹 postOrder(root->right); //接着遍歷右子樹 cout << root->data << " --> "; //接着打印根節點 } } #pragma endregion #pragma region 公有成員函數 ////二叉樹的建立 void BinaryTree::Create() { size = 0; m_root = create(); } //刪除二叉樹 void BinaryTree ::distroy() { distroy(m_root); } //遞歸得到樹的高度 int BinaryTree::getHeight() { return getHeight(m_root); } //前序遍歷 void BinaryTree::preOrder() { cout << "前序遍歷的結果如下:"<<endl; preOrder(m_root); cout << endl; } //中序遍歷 void BinaryTree::inOrder() { cout << "中序遍歷的結果如下:"<<endl; inOrder(m_root); cout << endl; } //後序遍歷 void BinaryTree::postOrder() { cout << "後序遍歷的結果如下:"<<endl; postOrder(m_root); cout << endl; } //獲取大小 int BinaryTree::getSize() { //這裏是創建時候直接進行了計數 //也可以利用遍歷的方式獲取,當節點有值,就加1 return size; } #pragma endregion
前序遍歷的方式創建如下圖所示二叉樹,應當輸入:ABDG##H###CE#I##F##測試代碼爲:測試結果爲:BinaryTree tree; cout << "按前序遍歷方式創建樹" <<endl; //"ABDG##H###CE#I##F##"; tree.Create(); cout << "樹的高度爲:" << tree.getHeight() <<endl; cout << "樹的節點爲:" << tree.getSize() <<endl; tree.preOrder(); //前序遍歷 tree.inOrder(); //中序遍歷 tree.postOrder(); //後序遍歷 tree.distroy(); //摧毀樹 system("pause");
參考:https://www.cnblogs.com/SarahZhang0104/p/5827532.htmlhttps://www.cnblogs.com/liuamin/p/6269950.html