資料結構 — 二叉樹的基本操作(遞迴實現)
阿新 • • 發佈:2019-02-13
#include<iostream> #include<Windows.h> #include<queue> using namespace std; template<class T> struct BinaryTreeNode { BinaryTreeNode<T>* _letf; BinaryTreeNode<T>* _right; T _data; BinaryTreeNode(const T& x) :_letf(NULL) , _right(NULL) , _data(x) {} }; template<class T> class BinaryTree { typedef BinaryTreeNode<T> Node; public: BinaryTree() :_root(NULL) {} BinaryTree(T* a, size_t n, const T& invalid) { size_t index = 0; _root = CreateTree(a, n, invalid, index); } BinaryTree(const BinaryTree<T>& t) { _root = _Copy(t._root); } BinaryTree<T>& operator = (const BinaryTree<T> t) { if (this != &t) { swap(t._root, _root); } } //前序遍歷 void PrevOrder() { return _prevOrder(_root); } //中序遍歷 void InOrder() { return _InOrder(_root); } //後序遍歷 void PosOrder() { return _PosOrder(_root); } //層序遍歷 void LevelOrder() { return _LevelOrder(_root); } //尺寸大小 size_t Size() { return _Size(_root); } //葉子結點 size_t LeafSize() { return _LeafSize(_root); } //第K個結點的節點個數 size_t GetKLevel(size_t k) { return _GetKLevel(_root, k); } //樹深度 size_t Depth() { return _Depth(_root); } //查詢 Node* find(const T& x) { return _find(_root,x); } protected: //第K層的節點數 size_t _GetKLevel(Node* root, size_t k) { if (root == NULL) { return 0; } if (k == 1) { return 1; } return _GetKLevel(root->_letf, k - 1) + _GetKLevel(root->_right, k - 1); } //查詢 Node* _find(Node* root,const T& x) { if (root == NULL) { return NULL; } if (root->_data == x) { return root; } Node* ret = _find(root->_letf, x); if (ret) { return ret; } return _find(root->_right, x); } //深度 size_t _Depth(Node* root) { if (root == NULL) { return 0; } else { size_t i = _Depth(root->_letf); size_t j = _Depth(root->_right); if (i > j) { return i + 1; } else { return j + 1; } } } //葉子結點求解 size_t _LeafSize(Node* root) { if (root == NULL) { return 0; } if (root->_letf == NULL &&root->_right == NULL) { return 1; } size_t i = _LeafSize(root->_letf); size_t j = _LeafSize(root->_right); return i + j; } //層序遍歷 void _LevelOrder(Node* root) { queue<Node*> q; if (root != NULL) { q.push(root); while (!q.empty()) { Node* front = q.front(); cout << front->_data << " "; if (front->_letf) { q.push(front->_letf); } if (front->_right) { q.push(front->_right); } q.pop(); } } } //總成員數 size_t _Size(Node* root) { if (root == NULL) { return 0; } size_t i = _Size(root->_letf); size_t j = _Size(root->_right); return i + j + 1; } //後序遍歷 void _PosOrder(Node* root) { if (root == NULL) { return; } _PosOrder(root->_letf); _PosOrder(root->_right); cout << root->_data <<" "; } //中序遍歷 void _InOrder(Node* root) { if (root == NULL) { return; } _InOrder(root->_letf); cout << root->_data << " "; _InOrder(root->_right); } //前序遍歷 void _prevOrder(Node* root) { if (root == NULL) { return ; } cout << root->_data << " "; _prevOrder(root->_letf); _prevOrder(root->_right); } //注意這裡的index 要使用引用, 若是沒有使用 上一層遞迴裡面的++index 對上一層沒有任何影響. Node* _Copy(Node* root) { if (NULL == root) { return NULL; } Node* newroot = new Node(root->_data); newroot->_letf = _Copy(root->_letf); newroot->_right = _Copy(root->_right); return newroot; } Node* CreateTree(T* a, size_t n, const T& invalid, size_t& index) { Node* root = NULL; if (index < n && a[index] != invalid) { root = new Node(a[index]); root->_letf = CreateTree(a, n, invalid, ++index); root->_right = CreateTree(a, n, invalid, ++index); } return root; } protected: Node* _root; size_t index; size_t invalid; }; void Test1() { int array[10] = { 1, 2, 3, '#', '#', 4, '#', '#', 5, 6 }; BinaryTree<int> t1(array, sizeof(array) / sizeof(array[0]), '#'); BinaryTree<int> t2(t1); //t1.PrevOrder(); //t1.InOrder(); //t1.PosOrder(); //cout<<t1.Size()<<" "; //t1.LevelOrder(); //cout<<t1.LeafSize(); //cout<<t1.Depth(); //cout<<(t1.find(3)); //cout<<t1.GetKLevel(3); }