二叉树三种遍历(递归及非递归)的实现
来源:互联网 发布:姆潘巴现象知乎 编辑:程序博客网 时间: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
- 二叉树三种遍历(递归及非递归)的实现
- 二叉树三种遍历递归及非递归实现
- 二叉树的遍历(递归实现+非递归实现)
- 二叉树三种遍历方式的递归与非递归实现
- 二叉树三种遍历方法的递归与非递归实现
- 二叉树的遍历(递归,非递归)实现
- 二叉树的遍历(非递归和递归实现)
- 二叉树三种遍历(递归以及非递归实现)
- C++ 实现求二叉树的深度及遍历(递归与非递归算法)
- Tree(3)--二叉树的实现及四种遍历(递归/非递归)
- 二叉树的建立及层次遍历及前中后序遍历递归与非递归的实现
- 二叉树的深度与广度遍历及前序遍历递归非递归实现
- 非递归实现二叉树的遍历
- 非递归实现二叉树的遍历
- 二叉树遍历的非递归实现
- 二叉树遍历的非递归实现
- 二叉树遍历的非递归实现
- 二叉树的非递归遍历实现
- MySQL慢日志查询用法
- 深入理解Java内存模型(六)——final
- Python学习手册 - 11
- 16 复杂链表的复制
- 从僵尸网络追踪到入侵检测 第8章 Honeyd配置文件使用
- 二叉树三种遍历(递归及非递归)的实现
- 修改mysql数据库的用户名和密码
- 安装完 MySQL 后必须调整的 10 项配置
- Linux下设置环境变量
- MYSQL 备份与恢复
- oracle 的自连接(self join)操作(转载的)
- MySQL处理数据库和表的常用命令
- android中的四大组件
- tableview的两个获得重用cell方法的区别?