[data structure] traversal of binary tree

  Yan-yingjie's homepage

Awareness of the past, not remonstrance, knowing the future, can be pursued  

C++ programmer, 2024 electronic information graduate student


Table of contents

Preorder, inorder and postorder traversal

preorder traversal

Inorder traversal

post order traversal


Preorder, inorder and postorder traversal

        

        The easiest way to learn the binary tree structure is to traverse it. The so-called binary tree traversal (Traversal) is to perform corresponding operations on the nodes in the binary tree in turn according to certain specific rules, and each node is only operated once . The operations performed by the access nodes depend on the specific application problem. Traversal is one of the most important operations on the binary tree, and it is also the basis for other operations on the binary tree.

According to the rules, the traversal of the binary tree includes: preorder / inorder / postorder recursive structure traversal :
         1. Preorder traversal (Preorder Traversal, also known as preorder traversal) - the operation of visiting the root node occurs before traversing its left and right subtrees.
        2. Inorder Traversal (Inorder Traversal) - the operation of visiting the root node occurs in traversing its left and right subtrees (between).
        3. Postorder Traversal——The operation of visiting the root node occurs after traversing its left and right subtrees .

Since         the visited node must be the root of a certain subtree, N(Node ), L(Left subtree ) and R(Right subtree ) can be interpreted as the root, the left subtree of the root and the right subtree of the root . NLR , LNR , and LRN are also called first-root traversal, middle-root traversal, and back-root traversal, respectively.

        

// 二叉树前序遍历
void PreOrder(BTNode* root);
// 二叉树中序遍历
void InOrder(BTNode* root);
// 二叉树后序遍历
void PostOrder(BTNode* root);

 The following mainly analyzes the pre-order recursive traversal. The in-order and post-order diagrams are similar, and students can draw them by themselves.

preorder traversal

         Preorder traversal recursive diagram:

        

        Code analysis:

                

#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<assert.h>
#include<stdlib.h>

typedef int BTDataType;

typedef struct BinaryTreeNode
{
	BTDataType data;
	struct BinaryTreeNode* left;
	struct BinaryTreeNode* right;
}BTNode;

BTNode* BuyNode(BTDataType x)
{
	BTNode* node = (BTNode*)malloc(sizeof(BTNode));
	if (node == NULL)
	{
		perror("malloc fail");
		return;
	}
	node->data = x;
	node->left = NULL;
	node->right = NULL;

	return node;
}

BTNode* CreatTree()
{
	BTNode* node1 = BuyNode(1);
	BTNode* node2 = BuyNode(2);
	BTNode* node3 = BuyNode(3);
	BTNode* node4 = BuyNode(4);
	BTNode* node5 = BuyNode(5);
	BTNode* node6 = BuyNode(6);
	BTNode* node7 = BuyNode(7);

	node1->left = node2;
	node1->right = node4;
	node2->left = node3;
	node2->right = NULL;
	node4->left = node5;
	node4->right = node6;

	return node1;
}


//前序排列
void PreOrder(BTNode* root)
{
	if (root==NULL)
	{
		printf("NULL ");
		return;
	}

	printf("%d ",root->data);
	PreOrder(root->left);
	PreOrder(root->right);
}


int main()
{
	BTNode* root = CreatTree();
	PreOrder(root);
	printf("\n");
	return 0;
}

Inorder traversal

          Inorder traversal recursive diagram:

        Code analysis:

        

void InOrder(BTNode* root) {
	if (root == NULL) {
		printf("NULL ");
		return;
	}

	InOrder(root->left);
	printf("%d ", root->data);
	InOrder(root->right);
}

post order traversal

        Post-order traversal recursive diagram:

        Code analysis:

void PostOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("NULL ");
		return;
	}

	PostOrder(root->left);
	PostOrder(root->right);
	printf("%d ", root->data);
}

Guess you like

Origin blog.csdn.net/m0_73367097/article/details/130258973