二叉树的先序、中序、后序、层序遍历

二叉树是一种树形结构,它每个结点至多只有两棵子树(即二叉树中不存在度大于2的结点)。

所谓度是结点拥有的子树数。

对于二叉树,它具有以下的性质:

1、在二叉树的第i层上至多有2^(i-1)个结点(i>=1)。

2、深度为k的二叉树至多有2^k-1个结点。

3、对任何一棵二叉树,如果它的叶子结点个数为n0,度为2的结点为n2,那么m = n + 1;

eg.如果设一个二叉树中度为1的结点个数为n1

故总结点数   N = n0 + n1 + n2;   (1)

二叉树除了根结点外,其余结点都有一个分支,设M为分支总数,则 N = M + 1;由于这些分支是由度为1或2的结点射出的,则M = n1 + 2*n2;

则有    N = n1 + 2*n2 + 1   (2)

由(1)(2)得 n0 = n2 + 1;

4、具有n个结点的完全二叉树的深度为∟log 2 n」+1.(其中“∟x」 ”表示不大于x的最大整数)。

5、如果对一棵有n个结点的完全二叉树的结点按层序编号(每一层从左到右,直到∟log 2 n」+1),则对任意一结点i(1=<i<=n)有

(1)如果i=1,则结点i是二叉树的根,无双亲;如果i>1,则其双亲是结点∟i/2」.

(2)如果2i>n,则结点i无左右孩子(结点i为叶子结点)否则其左孩子是结点2i;

(3)如果2i+1>n,则结点i无左右孩子;否则其右孩子是结点2i+1;

#pragma once
#include<queue>
#include<iostream>
using namespace std;
template<class T>
struct BinaryTreeNode
{
	BinaryTreeNode<T> *_left;
	BinaryTreeNode<T> *_right;
	T _data;
public:
	BinaryTreeNode(const T& x)
		:_data(x)
		,_right(NULL)
		,_left(NULL)
	{}
};
template<class T>
class BinaryTree
{
	typedef BinaryTreeNode<T> Node;
public:
	BinaryTree()
		:_root(NULL)
	{}
	BinaryTree<T>(const T* a, size_t size, const T& invalid)
	{
		size_t index = 0;
		_root = _CreatTree(a, size, index, invalid);
	}
	BinaryTree<T>(const BinaryTree<T>& t)
	{
		_root=_Copy(t._root);
	}
	BinaryTree<T>& operator=( BinaryTree<T> t)
	{
		swap(_root, t._root);
		return *this;
	}
	~BinaryTree()
	{
		_Clear(_root);
	}
	void PrevOrder()
	{
		cout << "先序:" << endl;
		_PrevOrder(_root);
	}
	void InOrder()
	{
		cout << "中序:" << endl;
		_InOrder(_root);
	}
	void PostOrder()
	{
		cout << "后序:" << endl;
		_PostOrder(_root);
	}
    //层序
	//思想:队列
	//1.先判断根节点是否为NULL
	//2.如果根节点不为空,节点入队(不是入值)
	//3.判断队列是否为空,如果不为空,出队
	//4.判断左 右子树节点是否为空,
	//5.如果不为空,入队 左右节点,跳至2
	void LeveLorder()    //层序
	{
		cout << "层序:" << endl;
		queue<Node*> tmp;
		if (_root == NULL)
			return;
		else
		{
			tmp.push(_root);
			while (!tmp.empty())
			{
				Node* Front = tmp.front();
				cout << Front->_data << " ";
				tmp.pop();
				if (Front->_left)
				{
					tmp.push(Front->_left);
				}
				if (Front->_right)
				{
					tmp.push(Front->_right);
				}
			}
		}
	}
	size_t Size()
	{
		return _Size(_root);
	}
	size_t Depth()
	{
		_Depth(_root);
	}
	size_t LeafSize()
	{
		return _leafSize(_root);
	}

protected:
	Node* _CreatTree(const T*a, size_t size, size_t& index, const T& invalid)
	{
		Node* root = NULL;
		if (a[index] != invalid&&index < size)
		{
			root = new Node(a[index]);
			root->_left = _CreatTree(a, size, ++index, invalid);//++index 返回index  index++返回临时变量(在此编译不通过)
			root->_right = _CreatTree(a, size, ++index, invalid);
		}
		return root;
	}
	void _PrevOrder(Node* root)   //先序遍历
	{
		if (root == NULL)
			return;
		cout << root->_data << " ";
		_PrevOrder(root->_left);
		_PrevOrder(root->_right);
	}
	void _InOrder(Node* root)   //中序遍历
	{
		if (root == NULL)
			return;
		_InOrder(root->_left);
		cout << root->_data << " ";
		_InOrder(root->_right);
	}
	void _PostOrder(Node* root)  //后序遍历
	{
		if (root == NULL)
			return;
		_PostOrder(root->_left);
		_PostOrder(root->_right);
		cout << root->_data << " ";
	}
	void _Size(Node* root)
	{
		if (root == NULL)
			return 0;
		return _Size(root->_left) + _Size(root->_right) + 1;
	}
	size_t _Depth(Node* root)
	{
		if (root == NULL)
			return 0;
		int leftdepth = _Depth(root->_left);
		int rightdepth = _Depth(root->_right);
		return leftdepth > rightdepth ? leftdepth + 1 : rightdepth + 1;
	}
	size_t _leafSize(Node* root)
	{
		static size_t size = 0;
		if (root == NULL)
			return 0;
		if (root->_left == NULL&&root->_right == NULL)
		{
			++size;
			return size;
		}
		_leafSize(root->_left);
		_leafSize(root->_right);
		return size;
	}
	void _Clear(Node* root)
	{
		if (root)
		{
			_Clear(root->_left);
			_Clear(root->_right);
			delete root;
		}
	}
	Node* _Copy(Node* root)
	{
		if (root==NULL)
		{
			return NULL;
		}
		Node *tem = new Node(root->_data);
		tem->_left=_Copy(root->_left);
		tem->_right=_Copy(root->_right);
		return  tem;
	}
private:
	Node* _root;
};
时间: 2024-07-30 06:24:21

二叉树的先序、中序、后序、层序遍历的相关文章

leetcode(144,94,145,102)中迭代版的二叉树的前、中、后、层级遍历

//前序遍历class Solution{ public: vector<int> preorderTraversal(TreeNode *root){ vector<int> res; stack<TreeNode*> s; TreeNode* p = root; if(!p) return res; s.push(p); while(!s.empty()){ p = s.top(); s.pop(); res.push_back(p->val); if(p-&

Java数据结构四之——二叉树的前、中、后序遍历

程序来自Program Creek 前 Preorder binary tree traversal is a classic interview problem about trees. The key to solve this problem is to understand the following: What is preorder? (parent node is processed before its children) Use Stack from Java Core lib

算法进化历程之“根据二叉树的先序和中序序列输出后序序列”

巧若拙(欢迎转载,但请注明出处:http://blog.csdn.net/qiaoruozhuo) 前不久在看到一个作业"根据二叉树的先序和中序序列输出后序序列",当时我参考<数据结构与算法(C语言)习题集>上的做法,先根据先中序序列确定一颗二叉树,然后后序遍历二叉树输出后序序列. 函数采用了递归算法,利用函数传入的先序和中序序列的左右边界,确定要处理的序列段,生成相应的二叉树. 基本思路是,把该段先序序列的第一个元素作为当前二叉树的根结点,然后在中序序列找到根结点.根结点

通过二叉树的中序序列和后序序列获取前序序列

二叉树的遍历方式常见的三种是:先序遍历(ABC).中序遍历(BAC).后序遍历(BCA) 先序遍历: 若二叉树为空,则空操作:否则: 访问根结点; 先序遍历左子树: 先序遍历右子树. 中序遍历: 若二叉树为空,则空操作:否则: 中序遍历左子树: 访问根结点: 中序遍历右子树. 后序遍历: 若二叉树为空,则空操作:否则: 后序遍历左子树: 后序遍历右子树: 访问根结点. 在学习到 根据遍历序列确定二叉树 时,知道了:可以通过二叉树的先中或者中后遍历序列唯一确定一棵二叉树. 根据算法描述 使用jav

先序序列和后序序列并不能唯一确定二叉树

数据结构的基础知识中重要的一点就是能否根据两种不同遍历序列的组合(有三种:先序+中序,先序+后序,中序+后序),唯一的确定一棵二叉树.然后就是根据二叉树的不同遍历序列(先序.中序.后序),重构二叉树.显然,这三种组合并不是都能唯一确定二叉树的,其中先序+后序就不能唯一确定一棵二叉树,其他两种组合可以唯一的确定一颗二叉树. 由先序序列和后序序列不能唯一确定一棵二叉树,因无法确定左右子树两部分. 反例:任何结点只有左子树的二叉树和任何结点只有右子树的二叉树,其前序序列相同,后序序列相同,但却是两棵不

中序表达式转后序表式式

中序表达式转后序表式式: 将中序表达式所有括号补全,然后将所有运算符向右移出无匹配的第一个右括号,去掉括号即为后序表式式 举例: 原式:a+b*(c+d/e) 补全括号:(a+(b*(c+(d/e)))) 操作符右移:(a(b(c(de)/)+)*)+ 去掉括号:abcde/+*+ 中序表达式转前序表式式: 将中序表达式所有括号补全,然后将所有运算符向左移出无匹配的第一个左括号,去掉括号即为前序表式式 举例: 原式:a+b*(c+d/e) 补全括号:(a+(b*(c+(d/e)))) 操作符右移

hdu1710-Binary Tree Traversals (由二叉树的先序序列和中序序列求后序序列)

http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4210    Accepted Submission(s): 1908 Problem Description A binary tree is a

华南理工数据结构大作业第二题 二叉树各种操作深度结点个数后序前序中序层次求祖先

/*#include<iostream> #include<windows.h> using namespace std ; struct BTNode { char data ; BTNode *left ; BTNode *right ; BTNode () { left = NULL ; right = NULL ; } } ; int main () { cout <<"题目所给的二叉树用括号表示法后表示为:A(B(D,E(H(J,K(L,M(,N))

二叉树遍历算法——包含递归前、中、后序和层次,非递归前、中、后序和层次遍历共八种

首先,要感谢网上的参考资料. http://mengliao.blog.51cto.com/876134/1178079(作者:BlackAlpha) http://blog.csdn.net/fzh1900/article/details/14056735(作者:_云淡风轻) http://blog.csdn.net/stpeace/article/details/8138458(作者:stpeace) 二叉树是使用的比较广泛的一种数据结构,这里我写了二叉树的相关操作,包括初始化.新建.以及遍

日常学习随笔-用链表的形式实现普通二叉树的新增、查找、遍历(前、中、后序)等基础功能(侧重源码+说明)

一.二叉树 1.二叉树的概念 二叉树是每个节点最多有两个子树的树结构.通常子树被称作"左子树"(left subtree)和"右子树"(right subtree),其次序不能任意颠倒. 2.性质 (1)若二叉树的层次从0开始,则在二叉树的第i层至多有2^i个结点(i>=0): (2)高度为k的二叉树最多有2^(k+1) - 1个结点(k>=-1). (空树的高度为-1): (3)对任何一棵二叉树,如果其叶子结点(度为0)数为m, 度为2的结点数为n,