Talk about the binary tree in the algorithm

origin


If there is no accident, the algorithm article has come to an end for the time being! Algorithms are more about conveying an idea, which is far less difficult than everyone thinks! The idea of ​​​​algorithms is common. Knowing is difficult and doing is easy. Only by learning can we make progress! What kind of topics do you want to see later, you can chat privately, the editor will update the official account from time to time according to your preferences! No matter how good other people's articles are, they are only auxiliary. The important thing is to try it yourself!

Binary Tree Overview


Binary tree is an important type of tree structure. The data structure abstracted from many practical problems is often in the form of a binary tree. Even a general tree can be easily converted into a binary tree, and the storage structure and algorithm of the binary tree are relatively simple, so the binary tree is particularly important. The characteristic of a binary tree is that each node can only have at most two subtrees, and there are left and right points.

A quick recap of the above passage shows that a binary tree is actually a type of data structure with a wide range of applications. A binary tree is composed of countless nodes, each node is a tree, and it is divided into left and right! Red-black trees can also be seen as a variant of binary trees!

Learning and testing of binary trees


The most basic learning of algorithm problems is to be able to solve the problems, and then to be able to apply them flexibly, and then to be able to abstract ideas and achieve commonality, and finally to be able to create new algorithms by oneself and become a patriarch!

This article focuses on the topic. The introductory questions of the binary tree are nothing more than telling you that the two in the preorder, inorder, and postorder infer the other, or restore the binary tree. Generally, there will be no layer order traversal, after all, limited popularity!

  • Preorder traversal: root => left => right, according to the result of preorder traversal, we can know that the first access must be the root node.
  • In-order traversal: left => root => right, according to the result of in-order traversal, combined with the root node of the pre-order traversal to divide the left and right subtrees of the root node.
  • Post-order traversal: left => right => root, according to the result of post-order traversal, it can be seen that the last node visited must be the root node.
  • Layer order traversal: from left to right, from top to bottom, the first access must be root

Insert the picture description here, and the following topics are all based on the picture. It is
insert image description here
known that the pre-order traversal of the binary tree is ABDHIEJCFKG, the in-order traversal is HDIBEJAFKCG, the post-order traversal is HIDJEBKFGCA, and the layer-order traversal is ABCDEFGHIJK

Note that there is no code in this chapter, only problem-solving ideas. If you are interested, you can convert the code into problem-solving ideas

topic one


Let’s start with the most classic, known pre-order and in-order, find the tree and post-order, or the above picture

Solution ideas: We know that the preorder is ABDHIEJCFKG, and the inorder is HDIBEJAFKCG, then we can determine the following points

  • According to the preorder traversal, the root node of the tree is A
  • According to the in-order traversal, the left HDIBEJ and the right FKCG of the whole tree
  • The preorder of the left subtree HDIBEJ is BDHIEJ (because the length of the preorder must be equal to the inorder, so it is easy to obtain the preorder of the corresponding subtree), the inorder is HDIBEJ (the subtree separated from the original inorder itself), we can know that the root node of the tree is B, the left side of its subtree is HDI, and the right side is EJ.
  • The cycle repeats until the last one.

Deformation: Knowing the post-order and in-order, finding the tree and the pre-order, in fact, the same principle, nothing more than the last one in the post-order is the root node, and the rest is nothing but the reverse!

Topic 2


Given the preorder and postorder, find the tree and inorder!

In fact, the answer to this question is not unique, because you can only determine the root node, you cannot determine the number of nodes in the left and right subtrees, any letter (as long as it is not the largest tree root node, it may be the left node or right node), then you need to exhaust All possible, anyway, just write a method to traverse, it's that easy!

Summarize


Understand the concept, have a clear idea, write code to show the idea, and run the code. According to the above process, any deformation problem can be solved according to the above solution. Is the binary tree not so difficult!

end


Good times are always short-lived. How have you improved today? See you next time!

Guess you like

Origin blog.csdn.net/zjscy666/article/details/121317311