[Data structure] [Binary tree] Traversal of three and binary trees (pre-order, middle-order, post-order, layer order)

Traversal of binary tree

This article will gradually add up the code of the four traversal methods (preorder, middle order, postorder, and layer sequence). The first three will list recursion and iteration (implemented by stack); the layer sequence is only Iterative (implemented by queue) is written.

For the pre-order, middle-order, and post-order, the recursive writing method is basically the same and relatively simple; the iterative writing method of the first two is relatively simple, and the template is similar; the iterative writing method of the latter traversal is slightly more complicated and involves The problem of secondary stacking requires careful study.

1. Preorder traversal

Put the code first:

void PreOrderTraverse(BiTNode *T)
{
    
    
	if(T == nullptr)
		return;
	printf("%c", T->data);
	PreOrderTraverse(T->leftChild);
	PreOrderTraverse(T->rightChild);
}

The achieved traversal effect is shown in the following figure:
Insert picture description here
The effect of pre-order traversal:
starting from the root node, the left children are traversed one by one, that is, in the order of ①②③ in the figure, each node is read;
until a node has no left Children come to the sibling node of this node (in the case of sibling nodes. If not, return to the sibling node of its parent node. If not, return to the sibling node of the parent node's parent node... ), and then regard the sibling node as the root of a new binary tree and traverse in the same way;
after traversing the sibling node, return to the sibling node of its parent node (in the case of its parent node having sibling nodes. If not , Then return to the sibling node of the parent node of the parent node...), recursively, until all nodes are traversed.
In short, in the preorder traversal, the sibling nodes of the parent node are very important and are often the first choice for return.

2. In-order traversal:

Code:
Recursive way:

void MidOrderTraverse(BiTNode *T)
{
    
    
	if(T == nullptr)
		return;
	MidOrderTraverse(T->leftChild);
	printf("%c",T->data);
	MidOrderTraverse(T->rightChild);	
}

Iterative method: (from Zhejiang University "Data Structure" P114)
Explanation: When traversing a binary tree in middle order, when a node is encountered, it is pushed onto the stack and its left subtree is traversed; when the left subtree traversal is over , Pop this node from the top of the stack and visit it, and then traverse the right subtree of the node according to its right pointer and go to the middle order.

void InorderTraverse(BinTree BT)
{
    
    
	BinTree T = BT;
	stack<ElemType> stk;
	
	while(!stk.empty() || T) //这个判断条件老折磨人了,|| T是不太容易想到的
	{
    
    
		while(T)
		{
    
    
			stk.push(T);
			T = T->left;
		}
		
		T = stk.top();
		stk.pop();
		printf("%d\n", T->val);
		
		T = T->right;
	}
}

Insert picture description here

Three, post-order traversal

Code:

void BackOrderTraverse(BiTNode *T)
{
    
    
	if(T == nullptr)
		return;
	MidOrderTraverse(T->leftChild);
	MidOrderTraverse(T->rightChild);
	printf("%c",T->data);	
}

Insert picture description here
The effect of post-order traversal:
As can be seen from the code, post-order traversal is to first traverse the left and right children of the current node, and then display itself.
In short, the basic steps of post-order traversal can be considered as the order of the left subtree first, then the right subtree , and the children first, then the father .

Fourth, the sequence traversal

Layer sequence traversal is achieved by relying on the stack. First, push the root node on the stack, then pop the root node, and push the left and right children of the root node on the stack;
then pop the left and rear children of the root node, and pop each node at the same time , Push its children onto the stack... so that the layer sequence traversal is realized.
As shown below:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_39642978/article/details/111188498