Binary tree from entry to AC (1) construction and traversal in front, middle and back

Noun interpretation and nature

First quote Baidu Encyclopedia:
Insert picture description here
here is a picture to understand the example that will be used immediately:
Insert picture description here

1. How to build a binary tree

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

typedef struct BTNode
{
    
    
    int data;
    struct BTNode *Left;
    struct BTNode *Right;
}Node;


//创建二叉树,按前序输入
Node* createBTree()
{
    
    
    Node *p;
    int ch;
	//printf("输入data");
    scanf("%d",&ch);
    if(ch == 0)     //如果到了叶子节点,接下来的左、右子树分别赋值为0
    {
    
    
        p = NULL;
    }
    else
    {
    
    
        p = (Node*)malloc(sizeof(Node));
        p->data = ch;
        p->Left  = createBTree();  //递归创建左子树
        p->Right = createBTree();  //递归创建右子树
    }
    return p;
}
int main()
{
    
    
	int i;
    Node *root = NULL;
    root = createBTree();
 return 0;
 }

This is a binary tree constructed with a double-pointer linked list according to the pre-order input. We can find that the linked list is only a storage method, and the order of traversal of the binary tree (and the order of input) is the real focus.
Previous sequence: 1245367 as an example
, when input is 1 2 4 (leaf node) 0 0 5 (leaf node) 0 0 3 6 (leaf node) 00 7 (leaf node) 00

Second, the first, middle and last traversal of the binary tree

Order of traversal

Traversal is to traverse each node of the binary tree in a certain
order. There are three kinds of sequences as follows:

(1) Front (first) order traversal (left and right root)

(2) In-order traversal (left root right)

(3) Post-order traversal (left and right root)

So when facing a binary tree, we can recursively write the first, middle, and last traversal according to the above rule.
Give a chestnut:Insert picture description here

Preamble : ABDECFG
Since it is about recursive root, to write the root node A and its left child and right child nodes :
the ABC;
Insert picture description here

Then start recursion. When B is the root, add left and right child nodes D and E after B :
AB DE C;
Insert picture description here

When C is the root, add the left and right sub-nodes FG after C :
AB DE C FG;
because after DFEG , there are no sub-nodes and the recursive end is the result of traversal as above.
Insert picture description here

The middle order method is as above: write BAC first;
add B according to the left root and right:
DBE AC;
add the final solution for C:
DBEAFCG;

The following sequence is the same as above:
BCA;
DEBCA;
DEBFGCA; get
the solution.
Any binary tree can be recursively written as above, and this recursive method will continue to be used in subsequent algorithms.

Advanced and sample questions

How to use code to find the order of a binary tree?
We can confirm a binary tree by preorder and middle order , and find the postorder in sequence; we
can also confirm by midorder and postorder , and find the preorder
but only It is impossible to build a binary tree if it is pre-order and post-order ;
use an example to illustrate:
Input:
Two lines input pre-order and middle-order respectively
Output:
output post-order

Idea: We don't need to rush to build a binary tree. With pre-order and middle-order, the tree itself has been established according to the above rules, although it only exists in the array. In other words, as a data structure, the real connotation of a binary tree lies in its order, rather than soullessly using a pointer linked list to store it.
Then the preorder is the left and right roots, the middle order is the left root and the right, we first find the root through the preorder (the first one in the preorder), the left of this root in the middle order is the left subtree and the right is the right subtree, then Perform the above recursion and output the node.
Explain the misunderstanding in layman's terms: recursively and then output, then the first output is the deepest result of DFS, and the last output is the current result.
Take the pre-order ABC as an example, the middle-order is BAC, in order to find the post-order, first find the root A, in the middle-order A to the left and B to the right C, recursive to B, no subtree output, recursive to C without subtree output, recursive Complete output A.
Code:

#include<stdio.h>
#include<string.h>
char a[28],b[28];
void dfs(int w,int x,int y,int z)//深搜
{
    
    int i=0,j=0;
    if(w>x||y>z)//无法继续递归时
return;
for(i=w;i<=x;i++)
    if(a[i]==b[y])//在中序中找到根
{
    
    dfs(w,i-1,y+1,y+i-w);//递归左子树
dfs(i+1,x,y+i-w+1,z);//递归右子树
        printf("%c",a[i]);//递归完后输出,注意,是递归完
}
}
int main()
{
    
    
int i=0,j=0,k=0;
scanf("%s",b);//输入前序
scanf("%s",a);//输入中序
j=strlen(a);
k=strlen(b);
dfs(0,j-1,0,k-1);//进入递归
    return 0;
}

Regarding dfs and binary trees, it was mentioned in DFS and permutation and combination
, but at that time the binary tree was more like a logical judgment structure than a storage structure.
Update again when there is time.

Guess you like

Origin blog.csdn.net/weixin_43736127/article/details/113797972