演算法與資料結構基礎4:C++二叉樹實現及遍曆方法大全,二叉樹遍曆

來源:互聯網
上載者:User

演算法與資料結構基礎4:C++二叉樹實現及遍曆方法大全,二叉樹遍曆

binary search tree,中文翻譯為二叉搜尋樹、二叉尋找樹或者二叉排序樹。簡稱為BST。

本文集齊了二叉樹的五大遍曆演算法:先序遍曆、中序遍曆、後序遍曆、深度優先遍曆和廣度優先遍曆(同層遍曆也就是深度優先遍曆)。


// BSTree.h

#include <cstdio>#include <iostream>#include <stack>#include <queue>using namespace std;// binary search tree,中文翻譯為二叉搜尋樹、二叉尋找樹或者二叉排序樹。簡稱為BSTclass BSTree{struct Node{Node(int x = 0):data(x), lchild(NULL), rchild(NULL){}struct Node* lchild;struct Node* rchild;int data;};public:// **************************************************************************// 類的四大函數:建構函式、拷貝建構函式、重載賦值運算子、解構函式// **************************************************************************BSTree();~BSTree();// **************************************************************************// 增刪改查// **************************************************************************void Insert(int x);// 返回二叉樹的個數unsigned short Size();unsigned short Deep();unsigned short Leaf();bool IsEmpty();// 遍曆void PreorderTraversal();// 先序遍曆void InorderTraversal();// 中序遍曆void PostorderTraversal();// 後序遍曆void DepthFirstSearch();// 深度優先遍曆void BreadthFirstSearch();// 廣度優先遍曆private:// 遞迴計算二叉樹個數unsigned short CountSize(Node* n);unsigned short CountDeep(Node* n);unsigned short CountLeaf(Node* n);// 遞迴遍曆void PreorderTraversal(Node* n);void InorderTraversal(Node* n);void PostorderTraversal(Node* n);void DepthFirstSearch(Node* n);void BreadthFirstSearch(Node* n);void Free(Node* node);private:Node* m_root;};// **************************************************************************// 私人方法// **************************************************************************unsigned short BSTree::CountSize(Node* n){if(!n){return 0;}return CountSize(n->lchild) + CountSize(n->rchild) + 1;}unsigned short BSTree::CountDeep(Node* n){if (!n) {return 0;}int ldeep = CountDeep(n->lchild);int rdeep = CountDeep(n->rchild);return ( ldeep > rdeep ) ? (ldeep + 1) : (rdeep + 1);}unsigned short BSTree::CountLeaf(Node* n){if (!n){return 0;}if (!n->lchild&& !n->rchild){return 1;}return CountLeaf(n->lchild) + CountLeaf(n->rchild);}void  BSTree::PreorderTraversal(Node* n){if (n) {cout << n->data << ",";PreorderTraversal(n->lchild);PreorderTraversal(n->rchild);}}void  BSTree::InorderTraversal(Node* n){if (n) {InorderTraversal(n->lchild);cout << n->data << ",";InorderTraversal(n->rchild);}}void  BSTree::PostorderTraversal(Node* n){if (n) {PostorderTraversal(n->lchild);PostorderTraversal(n->rchild);cout << n->data << ",";}}void BSTree::DepthFirstSearch(Node* root){stack<Node *> nodeStack;nodeStack.push(root);Node* node = NULL;while(!nodeStack.empty()){node = nodeStack.top();cout << node->data << ",";nodeStack.pop();if (node->rchild) {nodeStack.push(node->rchild);}if (node->lchild) {nodeStack.push(node->lchild);}}}void BSTree::BreadthFirstSearch(Node* root){queue<Node *> nodeQueue;nodeQueue.push(root);Node* node = NULL;while(!nodeQueue.empty()){node = nodeQueue.front();nodeQueue.pop();cout << node->data << ",";if (node->lchild) {nodeQueue.push(node->lchild);}if (node->rchild) {nodeQueue.push(node->rchild);}}}void BSTree::Free(Node* n){if (n) {Free(n->lchild);Free(n->rchild);delete n;n = NULL;}}// **************************************************************************// 類的四大函數:建構函式、拷貝建構函式、重載賦值運算子、解構函式// **************************************************************************BSTree::BSTree(){m_root = NULL;}BSTree::~BSTree(){Free(m_root);}// **************************************************************************// 增刪改查// **************************************************************************void BSTree::Insert(int x){Node* tmp = new Node(x);if (!m_root){m_root = tmp;}else{Node* pre = m_root;Node* cur = m_root;while (cur){pre = cur;cur = (x < cur->data) ? (cur->lchild) : (cur->rchild);}(x < pre->data) ? (pre->lchild = tmp) : (pre->rchild = tmp);}}unsigned short BSTree::Size(){return CountSize(m_root);}unsigned short BSTree::Deep(){return CountDeep(m_root);}unsigned short BSTree::Leaf(){return CountLeaf(m_root);}bool BSTree::IsEmpty(){return m_root == NULL;}void BSTree::PreorderTraversal(){PreorderTraversal(m_root);cout << endl;}void BSTree::InorderTraversal(){InorderTraversal(m_root);cout << endl;}void BSTree::PostorderTraversal(){PostorderTraversal(m_root);cout << endl;}void BSTree::DepthFirstSearch(){DepthFirstSearch(m_root);cout << endl;}void BSTree::BreadthFirstSearch(){BreadthFirstSearch(m_root);cout << endl;}

// mian.cpp

// test for BSTree#include "BSTree.h"#include <cstdlib>#include <iostream>using namespace std;int main(){BSTree tree;int arr[6] = {5, 4, 8, 1, 7, 10};for (int i = 0; i < 6; ++i){tree.Insert(arr[i]);}tree.PreorderTraversal();tree.InorderTraversal();tree.PostorderTraversal();tree.DepthFirstSearch();tree.BreadthFirstSearch();cout << "size:" << tree.Size() << endl;cout << "deep:" << tree.Deep() << endl;cout << "leaf:" << tree.Leaf() << endl;system("pause");return 0;}


// 輸出




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.