数据结构-二叉树(包含递归和非递归版本)

来源:互联网 发布:无尽的传说2 mac 破解 编辑:程序博客网 时间:2024/05/20 10:11
#pragma once#include <iostream>#include <assert.h>#include <stack>using namespace std;template <typename T>struct BinaryTreeNode{T _value;BinaryTreeNode* _leftchild;BinaryTreeNode* _rightchild;BinaryTreeNode(const T& x): _value(x), _leftchild(NULL), _rightchild(NULL){}};template <typename T>class BinaryTree{typedef BinaryTreeNode<T> Node;public:BinaryTree():_root(NULL){}BinaryTree(const T* arr, size_t size, const T& invalid=T()){assert(arr);size_t index = 0;_root = _CreatTree(arr, size, index, invalid);}BinaryTree(const BinaryTree<T>& bt){_root = _Copy(bt._root);}BinaryTree<T>& operator=(BinaryTree bt){swap(_root, bt._root);return *this;}~BinaryTree(){_Destroy(_root);}void PrevOrder()  //先序遍历{_PrevOrder(_root);cout << endl;}void PrevOrderNonR() //先序非递归{stack<Node*> s;Node* cur = _root;while (!s.empty() || cur){while (cur)  //吧该数的左字数入栈{cout << cur->_value << " ";  //访问根节点s.push(cur);cur = cur->_leftchild;}Node* top = s.top(); //到这,top的左子树和自己已经访问s.pop();cur = top->_rightchild; //转化为子问题来解决访问以top节点的右子树}cout << endl;}void InOrder() //中序遍历{_InOrder(_root);cout << endl;}void InOrderNonR() //中序非递归{stack<Node*> s;Node* cur = _root;while (!s.empty() || cur){while (cur){s.push(cur);cur = cur->_leftchild;}Node* top = s.top();cout << top->_value << " ";s.pop();cur = top->_rightchild;}cout << endl;}void PostOrder() //后序遍历{_PostOrder(_root);cout << endl;}void PostOrderNonR() //后序非递归{stack<Node*> s;Node* cur = _root;Node* prev = NULL;while (!s.empty() || cur){while (cur)   {s.push(cur);cur = cur->_leftchild;}Node* top = s.top();if (top->_rightchild == NULL || top->_rightchild == prev){cout << top->_value << " ";s.pop();prev = top;}else{cur = top->_rightchild;}}cout << endl;}void LevelOrder() //层序遍历{queue<Node*> q;Node* tmp = _root;if (tmp == NULL){return;}q.push(tmp);while (!q.empty()){tmp = q.front();cout << tmp->_value << " ";q.pop();if (tmp->_leftchild != NULL){q.push(tmp->_leftchild);}if (tmp->_rightchild != NULL){q.push(tmp->_rightchild);}}cout << endl;}size_t Size(){return _Size(_root);}size_t Depth(){return _Depth(_root);}size_t GetKLevelSize(size_t k)  //求第k层节点个数(根节点为第1层){return _GetKLevelSize(_root,k);}size_t GetLeafSize()  //求叶子节点的个数{return _GetLeafSize(_root);}Node* Find(const T& x){return _Find(_root, x);}protected:Node* _CreatTree(const T* arr, size_t size, size_t& index, const T& invalid = T()){Node* tmp = NULL;if (index<size && arr[index] != invalid){tmp = new Node(arr[index]);  //int arr[15] = { 1, 2, '#', 3, '#', '#', 4, 5, '#', 6, '#', 7, '#', '#', 8 };tmp->_leftchild = _CreatTree(arr, size, ++index, invalid);tmp->_rightchild = _CreatTree(arr, size, ++index, invalid);}return tmp;}Node* _Copy(Node* root){if (root == NULL){return NULL;}Node* newroot = new Node(root->_value);newroot->_leftchild = _Copy(root->_leftchild);newroot->_rightchild = _Copy(root->_rightchild);return newroot;}void _Destroy(Node* root){if (root != NULL){_Destroy(root->_leftchild);_Destroy(root->_rightchild);delete root;root = NULL;}}void _PrevOrder(Node* root){if (root == NULL){return;}cout << root->_value<<" ";_PrevOrder(root->_leftchild);_PrevOrder(root->_rightchild);}void _InOrder(Node* root){if (root == NULL){return;}_InOrder(root->_leftchild);cout << root->_value << " ";_InOrder(root->_rightchild);}void _PostOrder(Node* root){if (root == NULL){return;}_PostOrder(root->_leftchild);_PostOrder(root->_rightchild);cout << root->_value << " ";}size_t _Size(Node* root){if (root == NULL){return 0;}return _Size(root->_leftchild) + _Size(root->_rightchild) + 1;}size_t _Depth(Node* root){if (root == NULL){return 0;}size_t left = _Depth(root->_leftchild);size_t right = _Depth(root->_rightchild);return (left > right ? left : right)+1;}size_t _GetKLevelSize(Node* root, size_t k){if (root == NULL){return 0;}if (k == 1){return 1;}return _GetKLevelSize(root->_leftchild,k-1) + _GetKLevelSize(root->_rightchild,k-1);}size_t _GetLeafSize(Node* root){if (root == NULL){return 0;}if ((root->_leftchild == root->_rightchild) && root->_leftchild == NULL){return 1;}return _GetLeafSize(root->_leftchild) + _GetLeafSize(root->_rightchild);}Node* _Find(Node* root, const T& x){if (root == NULL){return NULL;}if (root->_value == x){return root;}Node* tmp = _Find(root->_leftchild, x);if (tmp == NULL){tmp = _Find(root->_rightchild, x);}return tmp;}protected:Node* _root; //根};

0 0
原创粉丝点击