[Data structure] The only way to determine a binary tree

The only way to determine a binary tree

​ Before knowing how to uniquely determine a binary tree , you need to know how to traverse the tree.

tree traversal

  1. preorder traversal
  2. post order traversal
  3. sequence traversal

How to traverse a binary tree

  1. preorder traversal
  2. Inorder traversal
  3. post order traversal
  4. sequence traversal

sure way

So how to uniquely determine a binary tree? This needs to be carried out in combination with the traversal method of the binary tree, and there are mainly the following two methods:

  1. Preorder traversal + inorder traversal of binary tree
  2. Post-order traversal + in-order traversal of binary tree

When memorizing these two methods, in order to avoid confusion, you can remember this way: Since in-order traversal is a unique traversal method for binary trees, one of the only ways to determine a binary tree must be in-order traversal, and the rest The one is either pre-order traversal or post-order traversal.

Now suppose there is a binary tree, the results of its pre-order, in-order and post-order traversal are as follows:

  1. Preorder traversal: ABDEGCFH
  2. Inorder traversal: DBGEACFH
  3. Post-order traversal: DGEBHFCA

Before introducing how to use the traversal method to uniquely determine a binary tree, it is important to emphasize:

No matter what method is used to uniquely determine a binary tree, its essence is to continuously and recursively determine the root node and divide the left and right subtrees through two traversal results!

Preorder traversal + inorder traversal

The essence of preorder traversal + inorder traversal is: use preorder traversal to determine the root node, and then use inorder traversal to divide the left and right subtrees

Step 1: Analyze the entire binary tree

For preorder traversal, the traversal method is that a node needs to visit itself first, then visit its left subtree, and then its right subtree, so the first element in the result of preorder traversal must be the root node, according to The above preorder traversal can know that it is A.

​ Then use the obtained A to find the position of A in the in-order traversal result, and then all the elements on the left side of A can be assigned to the left subtree of the root node, and all the elements on the right side of A can be assigned to the root node The reason for doing this is that for in-order traversal, the traversal method is that a node visits its left subtree first, then visits itself, and then its right subtree, so the elements in front of A must be From the left subtree of A, what follows A must come from the right subtree of A.

According to the above analysis, the following picture can be drawn first:

insert image description here

Step 2: Analyze the left subtree of A, that is, the binary tree containing DBGE.

At this time, the pre-order traversal part and the in-order traversal part containing the four elements of DBGE can be extracted separately:

  1. Partial preorder traversal: BDEG
  2. Partial inorder traversal: DBGE

Now observe the tree containing DBGE and its corresponding partial pre-order traversal and partial in-order traversal separately. You can see that the following analysis method is exactly the same as step 1, that is, first determine the tree (only containing DBGE) The root node of this tree), and then determine its two subtrees. Now the root node is the first element of the partial preorder traversal, that is, B. According to B, divide the left and right subtrees in the partial inorder traversal, namely The left subtree contains D, and the right subtree contains GE. The resulting picture is as follows:

insert image description here

It can be seen that the left subtree of B has only one node at this time, so it can be considered that it has been determined, then it is only necessary to perform the same analysis on the right subtree of B according to the method of step 1.

Step 3: Analyze the right subtree of A, that is, the binary tree containing CFH.

At this time, the pre-order traversal part and the in-order traversal part containing the three elements of CFH can be extracted separately:

  1. Partial preorder traversal: CFH
  2. Partial inorder traversal: CFH

Now observe the tree containing CFH and its corresponding partial pre-order traversal and partial in-order traversal separately. You can see that the following analysis method is exactly the same as step 1, that is, first determine the tree (only the one containing CFH The root node of this tree), and then determine its two subtrees. Now the root node is the first element of the partial preorder traversal, that is, C. According to C, divide the left and right subtrees in the partial inorder traversal, namely The left subtree is empty, and the right subtree contains FH. The resulting picture is as follows:

insert image description here

It can be seen that the left subtree of B does not have a node at this time, so it can be considered that it has been determined, then it is only necessary to perform the same analysis on the right subtree of C according to the method of step 1.

Step 4: Obtain a complete binary tree.

According to the above steps, go through the process completely, and you can get the following binary tree:

insert image description here

post-order traversal + in-order traversal

The essence of post-order traversal + in-order traversal is: use post-order traversal to determine the root node, and then use in-order traversal to divide the left and right subtrees

In fact, the method of post-order traversal + in-order traversal to uniquely determine a binary tree is essentially the same as the method of pre-order traversal + in-order traversal to uniquely determine a binary tree. The only difference is that using post-order traversal + in-order traversal to uniquely determine The method of a binary tree needs to look at the results of the post-order traversal from the back to the front every time the root node is determined . For example, for the post-order traversal in the above example: DGEBHFCA, the first step to determine the root node needs to The last element, because post-order traversal is the last to visit the root node, so the position of the root node is the last one at this time, and the other steps are the same as the method of pre-order traversal + in-order traversal to uniquely determine a binary tree up.

Guess you like

Origin blog.csdn.net/zzy_NIC/article/details/119550296