Data structure C language version, four traversal methods of binary tree (preorder, inorder, postorder, level traversal)

Abstract: When we study data structures, the tree chapter is definitely the key point, and the binary tree is the key point of the tree. The traversal of the binary tree has to be well grasped. Today I will take you to take a look. The four commonly used Traversal of a binary tree.

Regarding the traversal method of the binary tree, as mentioned in the title, how are they realized? What is the preorder, what is the middle order, and the postorder? What is the level traversal? For example, there is a binary tree below, let's take a look.

         The so-called preorder is to traverse the root node first, and then traverse the left and right child nodes.

As shown above, if we use pre-order traversal, it is ABCDEF GHK.

        In-order traversal is to traverse the left child node first, then traverse the root node, and finally traverse the right child node.

As shown above, if we use pre-order traversal, it is B DC AE HGK F.

        Post-order traversal is to traverse the left child node first, then traverse the right child node, and finally traverse the root node.

As shown above, if we use post-order traversal, it is DC B HKG FE A.

        The final hierarchical traversal is actually layered traversal, according to the rules from top to bottom and from left to right, as shown in the figure above,

If we use level traversal it is A BE CF DG HK.

        The code implementation is as follows;

Header file (function declaration): terr.h

#ifndef _TREE_
#define _TREE_
#define N 64
typedef char data_t;
typedef struct tree_node{
	data_t data;
	struct tree_node *l;//左孩子指针
	struct tree_node *r;//右孩子指针
}Tree;


Tree *Create_tree();//创建二叉树

void Tlr(Tree *T);//先序遍历

void lTr(Tree *T);//左序遍历

void lrT(Tree *T);//后序遍历

void C_bianli(Tree *T);//层次遍历

#endif

Implementation function: terr.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h>
#include "tree.h"

Tree *Create_tree()
{
	char ch;
	scanf("%c",&ch);
	Tree *T;
	if(ch == '#')
		{
		return NULL;
		}
	else
		{
		T =(Tree *)malloc(sizeof(Tree));
		if(NULL==T)
			{
			printf("T is NULL\n");
			return NULL;
			}
		T->data= ch;	
		T->l=Create_tree();//递归创建左孩子
		T->r=Create_tree();//递归创建右孩子
		
		return T;
		}
}

void Tlr(Tree *T)//先序遍历
{
	
	if(T!=NULL)
	{
		printf("%c",T->data);
		
		Tlr(T->l);//递归遍历左孩子
		Tlr(T->r);//递归遍历右孩子
	}
	return;
	
}

void lTr(Tree *T)//中序遍历
{
	
	if(T!=NULL)
	{
		
		
		lTr(T->l);
		printf("%c",T->data);
		lTr(T->r);
	}
	return;
	
}
void lrT(Tree *T)//后序遍历
{
	
	if(T!=NULL)
	{
				
		lrT(T->l);		
		lrT(T->r);
		printf("%c",T->data);
	}
	return;
	
}

void C_bianli(Tree *T)//层次遍历
{
	int front,rear;
	Tree *Q[N];
	if(T==NULL)//若为空树 则直接返回
	{
		return;
	}
	for (rear=1;rear<N;rear++)//把指针数组里的内容都赋值为空
	{
		Q[rear]=NULL;
	}
	front=rear=1;//对头指针和对尾指针
	Q[rear]=T;
	while(Q[front]!=NULL)//判断是否遍历结束
	{
		printf("%c",Q[front]->data);//打印出队内容
		if( T->l != NULL)//判断是否有左孩子
		{
			rear++;//对尾指针向后偏移
			Q[rear]=T->l;//左孩子入队
		}
		if(T->r != NULL)//判断是否有右孩子
		{
			rear++;//对尾指针向后偏移
			Q[rear]=T->r;//右孩子入队
		}
		front++;//出队一个对头指针向后偏移一个
		T=Q[front];	//把下一个元素作为新的根节点进行出入队	
	}
	return;
}

Main function: main.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h>
#include "tree.h"

int main()
{
	Tree *T=Create_tree();//创建二叉树

	Tlr(T);//先序遍历
	puts(" ");
	lTr(T);//左序遍历
	puts(" ");
        lrT(T);//后序遍历
	puts(" ");	
	C_bianli(T);//层次遍历
	puts(" ");
	return 0;
}

good! Today I will share here, the first time to share, where the writing is wrong, welcome to correct me, and don’t hesitate to enlighten me.

Guess you like

Origin blog.csdn.net/weixin_56187542/article/details/126128998