Definition of binary tree traversal, traversal algorithm, traverse the level

Tree is widely used in the computer field, especially in the binary tree is most commonly used. As the operating system, a tree to represent the organizational structure of the file directory, the compiler system, a tree to represent the grammatical structure of the source code, in the database system, the number of organizational structure is also an important form of information. This article is the only record I learn binary tree traversal, hoping to have a deep understanding of the tree structure.

Note:Four or five in two partsUsedStacks and queuesOperating see:

The basic operation of the cyclic queue

Sequence of stack related operations

Binary tree traversal three categories:Preorder binary tree (about root), the binary tree in preorder (root left and right), after traversing Binary Tree (about root).

Operation defining a three traversal of

1, the operation preorder binary tree defined:
If the binary tree is empty, the empty; otherwise
1) to access the root node;
2)PreorderLeft subtree;
3)PreorderRight subtree;Here Insert Picture Description

2, the binary tree in preorder operation definition:
if the binary tree is empty, the empty; otherwise
1)PreorderLeft subtree;
2) access to the root node;
3)PreorderRight subtree.
Here Insert Picture Description
3, the operation of the binary tree traversal order is defined:
If the binary tree is empty, the empty; otherwise
1)PostorderLeft subtree;
2)PostorderRight subtree;
3) access to the root node.
Here Insert Picture Description
It is represented by binary arithmetic expressions:
Here Insert Picture Description
first order sequence: - A + B-CD / EF (prefix (Polish))
preorder: A + B
C-de / F (infix shown)
postorder traversal: ABCD- * + ef / - (postfix notation (reverse Polish Notation))

前缀表达式就是先进行符号运算,也叫波兰表示
中缀表达式就是我们平时所用的标准四则运算表达式
后缀表达式是一种不需要括号的后缀表达法,也称为逆波兰表示
利用二叉树可以将中缀表示转化为后缀表示。

二、根据遍历序列确定二叉树

  • 若二叉树中各结点的值均不相同,则二叉树结点的先序序列、中序序列和后序序列都是唯一的
  • 有二叉树的先序序列和中序序列,或由二叉树的后序序列和中序序列可以确定唯一 一颗二叉树
    Here Insert Picture Description后序遍历,根结点必在后序序列尾部,所以先找到根:A,然后判断哪些在左子树上,哪些在右子树上,然后找到左子树的根:B,以此类推找根结点
    Here Insert Picture Description

但根据先序序列和后序序列无法确定二叉树

三、遍历的算法实现

1)先序遍历(根左右)

Status PreOrderTraverse(BiTree T){
if(T==NULL)
	return OK;//空二叉树
else{
	visit(T);//访问根结点,例如输出根节点cout<<T->data;
	PreOrderTraverse(T->lchild);//递归遍历左子树
	PreOrderTraverse(T->rchild);//递归遍历右子树

执行过程:设主程序为Pre(T)

void Pre(BiTree *T){
if(T!=NULL){
	printf("%d\t",T->data);
	pre(T->lchild);
	pre(T->rchild);
	}
}	

2)中序遍历(左根右)

void InOrderTraverse(BiTree T)
{
	if(T)	//二叉树非空
	{
		InOrderTraverse(T->lchild);//递归遍历左子树
		visit(T);//访问根结点,或写做cout<<T->data;
		InOrderTraverse(T->rchild);//递归遍历右子树
	}
}

3)后序遍历(左右根)

Status PostOrderTraverse(BITree T)
{
	if(T){
		PostOrderTraverse(T->lchild);
		PostOrderTraverse(T->rchild);
		cout<<T->data;
	}
}

从虚线的出发点到终点的路径上,每个结点经过3次。
第一次经过时访问=先序遍历
第二次经过时访问=中序遍历
第三次经过时访问=后序遍历
时间复杂度O(n)//每个结点只访问一次
空间复杂度S(n)//栈占用的最大辅助空间。

四、中序遍历非递归算法(用栈实现)

二叉树中序遍历的非递归算法的关键:在中序遍历过某结点的整个左子树后,如何找到该结点根以及右子树。
基本思想:
(1)建立一个栈
(2)根结点进栈,遍历左子树,为空则根结点出栈。
(3)根结点出栈,输出根结点,遍历右子树。

Status InOrderTraverse(BiTree T)
{
	BiTree p;
	InitStack(S);
	p=T;
	while(p||!StackEmpty(S))
	{
	if(p)
	{
		Push(S,p);
		p=p->lchild;
	}
	else
	{
		Pop(S,q);
		cout<<q->data;
		p=p->rchild;
		
	}
	return OK;
  }
}

五、二叉树的层次遍历(队列实现)

对于一棵二叉树,从根结点开始,按从上到下、从左到右的顺序访问每一个结点,每个结点仅访问一次。

Algorithm design: using a queue
1, the root node into the team;
2, cycle team is not empty: a team from the queue node * p, access to it;

  • If it has a left child node, the left child node into the team
  • If it has the right child node and the right child node into the team

That is one element after the team, it's about the children into the team.

Using the queue type is defined as follows:

typedef struct{
	BTNode data[MaxSize];//存放队中元素
	int front,rear;   //队头和队尾指针
	}SqQueue;  //顺序循环队列类型 

Binary tree traversal algorithm level

void LevelOrder(BTNode *b)
{
	BTNode *p;
	SqQueue *qu;
	InitQueue(qu);	//初始化队列
	enQueue(qu,b);	//根结点指针进入队列
	while(!QueueEmpty(qu))	//队不为空,则循环
	{
		deQueue(qu,p);	//出对结点p
		cout<<p->data;	//访问结点p
		if(p->lchild!=NULL)
			enQueue(qu,p->lchild);//有左孩子时将其进队
		if(p->rchild!=NULL)
			enQueue(qu,p->rchild);//有右孩子时将其进队
				
	}
}
//所有元素出队后,程序结束

Content Reference: "data structure" Yan Wei Min

Published 34 original articles · won praise 85 · views 4612

Guess you like

Origin blog.csdn.net/weixin_45895026/article/details/104047357