非递归遍历二叉树(前序,中序,后序,叶子节点的计算)
2016-03-10 20:29
441 查看
#pragma once #include<iostream> #include<queue> #include<stack> using namespace std; template<class T> struct BinaryTreeNode { T _data; BinaryTreeNode* _left; BinaryTreeNode* _right; BinaryTreeNode(const T& x) :_data(x) , _left(NULL) , _right(NULL) {} }; template<class T> class BinaryTree { protected: BinaryTreeNode<T>* _root; protected: //先序遍历产生二叉树 BinaryTreeNode<T>* _CreateBinaryTree(T* arr, size_t &index, size_t size) { BinaryTreeNode<T>* root = NULL; if (index < size&&arr[index] != '#') { root = new BinaryTreeNode<T>(arr[index]); root->_left = _CreateBinaryTree(arr, ++index, size); root->_right = _CreateBinaryTree(arr, ++index, size); } return root; } void _PreOrder(BinaryTreeNode<T>* root) { if (root != NULL) { cout << root->_data << " "; _PreOrder(root->_left); _PreOrder(root->_right); } return; } void _InOrder(BinaryTreeNode<T>* root) { if (root != NULL) { _InOrder(root->_left); cout << root->_data<<" "; _InOrder(root->_right); } return; }void _PostOrder(BinaryTreeNode<T>* root) { if (root != NULL) { _PostOrder(root->_left); _PostOrder(root->_right); cout << root->_data << " "; } return; } void _Clear(BinaryTreeNode<T>* root) { if (root) { _Clear(root->_left); _Clear(root->_right); delete root; } } int _Size(BinaryTreeNode<T>* root) { if (root==NULL) return 0; 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 leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1; } BinaryTreeNode<T>* _Copy(BinaryTreeNode<T>* root) { BinaryTreeNode<T>* tmp = NULL; if (root) { tmp = new BinaryTreeNode<T>(root->_data); tmp->_left=_Copy(root->_left); tmp->_right=_Copy(root->_right); } return tmp; } int _LeftNum(BinaryTreeNode<T>* root) { if (root == NULL) return 0; if (root->_left == NULL&&root->_right == NULL) return 1; return _LeftNum(root->_left) + _LeftNum(root->_right); } BinaryTreeNode<T>* _Find(BinaryTreeNode<T>* root, const T& x) { if (root == NULL) return NULL; if (root->_data == x) return root; BinaryTreeNode<T>* leftRet = _Find(root->_left, x); if (leftRet) return leftRet; return _Find(root->_right, x);//如果左子树没找到,就到右子树查找,直接返回即可 } public: BinaryTree() :_root(NULL) {} BinaryTree(T* a, size_t size) { size_t index = 0; _root = _CreateBinaryTree(a, index, size); } BinaryTree(const BinaryTree& b) { _root = _Copy(b._root); } BinaryTree& operator=(BinaryTree b) { swap(_root, b._root); } ~BinaryTree() { _Clear(_root); _root = NULL; } void PreOrder() { _PreOrder(_root); cout << endl; } void InOrder() { _InOrder(_root); cout << endl; } void PostOrder() { _PostOrder(_root); cout << endl; } int Size() { return _Size(_root); } int Depth() { return _Depth(_root); } int LeafNum() { return _LeftNum(_root); } void PreOrder_Non_R() { stack<BinaryTreeNode<T>*> s; if (_root) s.push(_root); while (!s.empty()) { BinaryTreeNode<T>* top = s.top(); cout << top->_data << " "; s.pop(); if (top->_right) s.push(top->_right); if (top->_left) s.push(top->_left); } cout << endl; } void InOrder_Non_R() { stack<BinaryTreeNode<T>*> s; BinaryTreeNode<T>* cur = _root; while (cur || !s.empty()) { while (cur) { s.push(cur); cur = cur->_left; } BinaryTreeNode<T>* top = s.top(); cout << top->_data << " "; s.pop(); cur = top->_right; } cout << endl; } void PostOrder_Non_R() { stack<BinaryTreeNode<T>*> s; BinaryTreeNode<T>* cur = _root; BinaryTreeNode<T>* prevVisited = NULL; while (cur || !s.empty()) { while (cur) { s.push(cur); cur = cur->_left; } BinaryTreeNode<T>* top = s.top(); if (top->_right == NULL || top->_right == prevVisited)//两种情况,才访问根节点 { cout << top->_data << " "; s.pop(); prevVisited = top; cur = NULL;//可要可不要 } else cur = top->_right; } cout << endl; } BinaryTreeNode<T>* Find(const T&x) { return _Find(_root, x); } }; #include"BinaryTree.h" void Test1() { int array[10] = { 1, 2, 3, '#', '#', 4, '#', '#', 5, 6 }; BinaryTree<int> b2(array, 10); b2.PreOrder(); b2.PreOrder_Non_R(); b2.InOrder(); b2.InOrder_Non_R(); b2.PostOrder(); b2.PostOrder_Non_R(); cout << b2.LeafNum() << endl; } int main() { Test1(); system("pause"); return 0; }
相关文章推荐
- AVL树-自平衡二叉查找树(Java实现)
- 文件遍历排序函数
- Lua 学习笔记之C API 遍历 Table实现代码
- C#遍历文件夹后上传文件夹中所有文件错误案例分析
- C#中遍历Hashtable的4种方法
- Erlang中遍历取出某个位置的最大值代码
- C++实现图的邻接矩阵存储和广度、深度优先遍历实例分析
- C++实现图的邻接表存储和广度优先遍历实例分析
- C语言二叉树的非递归遍历实例分析
- 使用C语言构建基本的二叉树数据结构
- 一波二叉树遍历问题的C++解答实例分享
- C++非递归队列实现二叉树的广度优先遍历
- php遍历目录方法小结
- 一个目录遍历函数
- php遍历删除整个目录及文件的方法
- PHP遍历文件夹与文件类及处理类用法实例
- PHP遍历XML文档所有节点的方法
- php中使用key,value,current,next和prev函数遍历数组的方法
- C#使用前序遍历、中序遍历和后序遍历打印二叉树的方法
- 平衡二叉树