Types of binary trees
In the process of solving the questions, we mainly focus on two main forms: full binary tree and complete binary tree.
full binary tree
If a binary tree only has nodes with degree 0 and nodes with degree 2, and the nodes with degree 0 are on the same level, then the binary tree is a full binary tree. As shown in the figure below:
This binary tree is a full binary tree, which can also be said to have a depth of k, with 2 k − 1 2^k-1 2k−Binary tree with 1 nodes.
complete binary tree
Complete definition: In a complete binary tree, except for the bottom node, which may not be filled, the number of nodes in each layer reaches the maximum, and the nodes in the bottom layer are concentrated in the leftmost positions of the layer. If the lowest layer is the h-th layer (h starts from 1), then this layer contains 1~ 2^(h-1) nodes.
binary search tree
The trees introduced earlier do not have numerical values, but the binary search tree does have numerical values. The binary search tree is an ordered tree.
- If its left subtree is not empty, then the values of all nodes on the left subtree are less than the value of its root node;
- If its right subtree is not empty, then the values of all nodes on the right subtree are greater than the value of its root node;
- Its left and right subtrees are also binary sorting trees respectively.
The following two trees are search trees
Balanced binary search tree
Balanced binary search tree: also known as AVL (Adelson-Velsky and Landis) tree, and has the following properties: it is an empty tree or the absolute value of the height difference between its left and right subtrees does not exceed 1, and the left and right Both subtrees are a balanced binary tree. As shown below:
How binary trees are stored
Binary trees can be stored in a chain or sequentially. Then the chain storage method uses pointers, and the sequential storage method uses arrays. As the name implies, the elements stored sequentially are continuously distributed in the memory, while chain storage uses pointers to connect nodes distributed at various addresses in series. Chain storage is shown in the figure:
The most common one is chain storage. The sequential storage method is to use an array to store, specifically:
Then if the array subscript of the parent node is i, the subscript of its left child is 2 ∗ i + 1 2*i+1 2∗i+1, right side 2 ∗ i + 2 2*i+2 2∗i+2。
How to traverse a binary tree
There are two main traversal methods:
- Depth-first traversal: first go in the direction of the leaf node, that is, the depth direction, and then go back when you encounter the leaf node.
- Breadth-first traversal: traversal layer by layer
Then these two methods can be further subdivided into:
- Depth first traversal:
- Preorder traversal
- inorder traversal
- Postorder traversal
- breadth-first traversal
- Level traversal
In the pre-order, mid-order, and post-order traversal, the order here actually refers toTraversal order of intermediate nodes, that is:
- Preorder traversal: middle left and right
- In-order traversal: left, middle, right
- Postorder traversal: left, right, center
This can be better understood from the image below:
Binary tree definition
The main focus is on how a binary tree stored in a chain is defined:
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {
}
};
Recursive traversal of binary tree
A very important traversal implementation method in binary trees is through recursion.
Then the three most important elements in the recursive algorithm are:
- Determine the parameters and return values of the recursive function: Determine which parameters need to be passed for processing during the recursive process, and which parameters need to be returned after the recursion is completed a>
- Determine the termination condition: In the recursive algorithm, the termination condition needs to be clear to prevent stack overflow caused by failure to terminate
- Determine the logic of single-level recursion: Determine what we need to do at each level of recursion