二叉树的创建,前序,中序,后序,层序遍历
2017-04-27 23:49
459 查看
二叉树(binary tree)是另一种树形结构,它的特点是每个节点至多有两颗子树,并且其子树有左右之分,并且顺序不能颠倒。
主要用递归的思想完成创建,遍历等操作。
主要用递归的思想完成创建,遍历等操作。
#include <iostream> using namespace std; #include <string> #include <queue> template <typename T> struct TreeNode{ TreeNode(const T& value) :_value(value) ,_pLeft(NULL) ,_pRight(NULL) {} TreeNode<T> *_pLeft; TreeNode<T> *_pRight; T _value; }; template <typename T> class BinaryTree{ typedef TreeNode<T> Node; public: BinaryTree() :_pRoot(NULL) {} BinaryTree(const T* arr, const T& invalied) //传入一个顺序为前序的数组,invalied意味节点为NULL { int index = 0; createTree(arr, _pRoot, strlen(arr), invalied, index); } BinaryTree(const BinaryTree<T>& t) { _pRoot = _CopyTree(t._pRoot); } BinaryTree<T>& operator=(const BinaryTree<T>& t) { _DestroyTree(_pRoot); _pRoot = _CopyTree(t._pRoot); return *this; } void PreOrder() //前序访问 { _PreOrder(_pRoot); cout<<"end"<<endl; } void InOrder() //中序访问 { _InOrder(_pRoot); cout<<"end"<<endl; } void PostOrder() //后序访问 { _PostOrder(_pRoot); cout<<"end"<<endl; } void LeverOrder() //层序访问 { queue<Node*> q; if (_pRoot) { q.push(_pRoot); _LeverOrder(_pRoot, q); } } ~BinaryTree() { _DestroyTree(_pRoot); } private: void createTree(const T* arr,Node* &proot ,size_t size, const T& invalied, int& index) { if (index < size && arr[index] != invalied) { proot = new Node(arr[index]); createTree(arr, proot->_pLeft,size, invalied, ++index); createTree(arr, proot->_pRight,size, invalied, ++index); } } Node *_CopyTree(Node* proot) { if (proot) { Node *temp = new Node(proot->_value); temp ->_pLeft = _CopyTree(proot->_pLeft); temp ->_pRight = _CopyTree(proot->_pRight); return temp; } return NULL; } void _DestroyTree(Node *&proot) { if (proot) { _DestroyTree(proot->_pLeft); _DestroyTree(proot->_pRight); delete proot; proot = NULL; } } void _PreOrder( Node* proot) { if (proot) { cout<<proot->_value<<"->"; _PreOrder(proot->_pLeft); _PreOrder(proot->_pRight); } } void _InOrder(Node *proot) { if (proot) { _InOrder(proot->_pLeft); cout<<proot->_value<<"->"; _InOrder(proot->_pRight); } } void _PostOrder(Node *proot) { if (proot) { _PostOrder(proot->_pLeft); _PostOrder(proot->_pRight); cout<<proot->_value<<"->"; } } void _LeverOrder(Node *proot, queue<Node*>& q) { while (!q.empty()) { Node* temp = q.front(); cout<<temp->_value<<"->"; q.pop(); if (temp->_pLeft) q.push(temp->_pLeft); if (temp->_pRight) q.push(temp->_pRight); } cout<<"end"<<endl; } private: Node *_pRoot; }; int main() { char* str= "124##57##8##3#6"; BinaryTree<char> tree(str,'#'); tree.PreOrder(); tree.InOrder(); tree.PostOrder(); tree.LeverOrder(); return 0; }
相关文章推荐
- 我强大的二叉树(先序、层序创建,先序、中序、后序、层序遍历)
- 二叉树的创建及前序、中序、后序、层序遍历
- 二叉树的创建 、前序、中序、后序、层序(层序二层从左遍历下一层从右遍历)遍历
- 二叉树(创建、前序、中序、后序、层序遍历理论及java实现)
- 二叉树创建及其遍历:前序、中序、后序、层序遍历等
- 二叉树的创建,先序、中序、后序遍历的递归实现以及层序遍历
- 遍历二叉树(前序、中序、后序、层序)
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现
- 二叉树的创建以及利用迭代实现中序、先序、后序遍历、清空
- 二叉树的遍历(前序、中序、后序、层序),递归和非递归实现
- 二叉树(先序创建,递归先序,中序,后序遍历)-----1
- 二叉树的遍历:前序、中序、后序、层序的非递归实现
- 二叉树的创建以及利用迭代实现中序、先序、后序遍历、清空
- 二叉树的创建、先序、中序以及后序遍历
- 创建二叉树,并对其进行 先序、中序、后序遍历
- 由二叉树的先序和中序遍历生成二叉树的层序和后序遍历结构
- 非递归遍历二叉树的四种策略-先序、中序、后序和层序
- JAVA下实现二叉树的先序、中序、后序、层序遍历(递归和循环)