二叉树三种遍历(递归及非递归)的实现

来源:互联网 发布:姆潘巴现象知乎 编辑:程序博客网 时间:2024/05/21 21:50
#include <iostream>#include <queue>#include <stack>using namespace std;template<class T>struct TreeNode{T _value;TreeNode<T>* _firstChild;TreeNode<T>* _nextBorther; };template<class T>struct BinaryTreeNode{T _value;BinaryTreeNode<T>* _left;BinaryTreeNode<T>* _right;BinaryTreeNode(const T& value):_value(value),_left(NULL),_right(NULL){}};template<class T>class Binarytree{public:Binarytree():_root(NULL){}Binarytree(char* str){_CreatTree(_root, str);}Binarytree(BinaryTreeNode<T>& t);Binarytree& operator=(BinaryTreeNode<T>& t);~Binarytree(){}void _CreatTree(BinaryTreeNode<T>*& root, char*& str){if (*str != '#'&& *str != '\0'){root = new BinaryTreeNode<T>(*str);_CreatTree(root->_left, ++str);if (*str == '\0'){return;}_CreatTree(root->_right, ++str);}}void PrevOrder(){_PrevOrder(_root);cout << endl;}void PrevOrder_NonR(){stack<BinaryTreeNode<T>*> Stack;if (_root){Stack.push(_root);}while (!Stack.empty()){BinaryTreeNode<T>* ret = Stack.top();cout << ret->_value << " ";Stack.pop();if (ret->_right)Stack.push(ret->_right);if (ret->_left)Stack.push(ret->_left);}cout << endl;}void InOrder_NonR(){stack<BinaryTreeNode<T>*> Stack;BinaryTreeNode<T>* ret = _root;while (ret || !Stack.empty()){while (ret){Stack.push(ret);ret = ret->_left;}if (!Stack.empty()){BinaryTreeNode<T>* Top = Stack.top();cout << Top->_value << " ";Stack.pop();if (Top->_right){ret = Top->_right;}}}cout << endl;}void PostOrder_NonR(){stack<BinaryTreeNode<T>*> Stack;BinaryTreeNode<T>* ret = _root;BinaryTreeNode<T>* visNode = NULL;while (ret || !Stack.empty()){while (ret){Stack.push(ret);ret = ret->_left;}BinaryTreeNode<T>* Top = Stack.top();if (Top->_right == NULL||Top->_right==visNode){Stack.pop();cout << Top->_value << " ";visNode = Top;}else{ret = Top->_right;}}cout << endl;}void InOrder(){_InOrder(_root);cout << endl;}void PostOrder(){_PostOrder(_root);cout << endl;}void LevelOrder(){_LevelOrder(_root);cout << endl;}int Size(){return _Size(_root);}int Depth(){return _Depth(_root);}protected:int _Size(BinaryTreeNode<T>* root){if (root == NULL){return 0;}if (root->_left == NULL && root->_right == NULL){return 1;}return 1 + (_Size(root->_left) + _Size(root->_right));}int _Depth(BinaryTreeNode<T>* root){if (root == NULL){return 0;}int leftDepth = _Depth(root->_left);int rightDepth = _Depth(root->_right);return 1 + (leftDepth > rightDepth ? leftDepth : rightDepth);}void _PrevOrder(BinaryTreeNode<T>* root){if (root != NULL){cout << root->_value << " ";_PrevOrder(root->_left);_PrevOrder(root->_right);}}void _InOrder(BinaryTreeNode<T>* root){if (root != NULL){_InOrder(root->_left);cout << root->_value << " ";_InOrder(root->_right);}}void _PostOrder(BinaryTreeNode<T>* root){if (root){_PostOrder(root->_left);_PostOrder(root->_right);cout << root->_value << " ";}}void _LevelOrder(BinaryTreeNode<T>* root){queue<BinaryTreeNode<T>*> q;if (root){q.push(root);}while (!q.empty()){BinaryTreeNode<T>* front = q.front();cout << front->_value << " ";q.pop();if (front->_left)q.push(front->_left);if (front->_right)q.push(front->_right);}}private:BinaryTreeNode<T>* _root;};void Test1(){char* str = "123##4##56";Binarytree<char> bt1(str);bt1.PrevOrder();bt1.LevelOrder();bt1.PostOrder();bt1.InOrder();bt1.PrevOrder_NonR();bt1.InOrder_NonR();bt1.PostOrder_NonR();cout << "bt1.Size:" << bt1.Size() << endl;cout << "bt1.Depth:" << bt1.Depth() << endl;}int main(){Test1();system("pause");return 0;}

0 0
原创粉丝点击