What is a binary tree


Preface

Why should we focus on a tree with at most two "forks" per node?

  • The structure of the binary tree is the simplest and the regularity is the strongest.
  • It can be proved that all trees can be transformed into a unique corresponding binary tree without loss of generality.

If the ordinary tree (multiple tree) is not converted into a binary tree, the operation is difficult to implement. This section will introduce the binary tree to everyone.


1. What is a binary tree

Simply understand, a tree that meets the following two conditions is a binary tree:

  1. It is an ordered tree;
  2. The degree of each node contained in the tree cannot exceed 2, that is, it can only be 0, 1 or 2;
    Insert picture description here
    Figure 1 Schematic diagram of binary tree

Second, the nature of the binary tree

Binary tree has the following properties:

  1. In a binary tree, the i-th level has at most 2(i)-1 nodes. Note: 2(i) means 2 to the power of i.
  2. If the depth of the binary tree is K, then the binary tree has at most 2(K)-1 nodes.
  3. In a binary tree, the number of terminal nodes (the number of leaf nodes) is n0, and the number of nodes with degree 2 is n2, then n0=n2+1.

The calculation method of property 3 is: for a binary tree, except for the leaf nodes with degree 0 and the nodes with degree 2, the rest is the node with degree 1 (set as n1), then the summary point n= n0+n1+n2.

At the same time, for each node, it is represented by the branch of its parent node. Assuming that the number of branches in the tree is B, then the number of summary points n=B+1. The number of branches can be expressed by n1 and n2, that is, B=n1+2 n2. Therefore, n is expressed in another way as n=n1+2 n2+1.

The n values ​​obtained in the two ways form an equation set, and then n0=n2+1 can be obtained.

Binary trees can also continue to be classified to derive full binary trees and complete binary trees.

Three, full binary tree

If the degree of each node in the binary tree is 2, except for the leaf nodes, then the binary tree is called a full binary tree.

Insert picture description here
Figure 2 Schematic diagram of full binary tree

In addition to satisfying the properties of ordinary binary trees, full binary trees also have the following properties:

  1. The number of nodes at the i-th level in a full binary tree is 2(n)-1. Note: 2(n) means 2 to the nth power.
  2. A full binary tree of depth k must have 2(k)-1 nodes and the number of leaves is 2(k)-1.
  3. There is no node with degree 1 in a full binary tree. There are two subtrees with the same depth in each branch point, and the leaf nodes are all at the bottom.
  4. The depth of a full binary tree with n nodes is log2(n+1).

Fourth, the complete binary tree

If the binary tree is a full binary tree except the last layer of nodes, and the nodes of the last layer are distributed from left to right, then this binary tree is called a complete binary tree.
Insert picture description here
Figure 3 Schematic diagram of a complete binary tree

As shown in Figure 3a), it is a complete binary tree. As the nodes of the last layer are not distributed from left to right, it can only be regarded as an ordinary binary tree.

In addition to the properties of an ordinary binary tree, a complete binary tree also has some unique properties. For example, the depth of a complete binary tree with n nodes is ⌊log2n⌋+1.

⌊Log2n⌋ means to take the largest integer less than log2n. For example, ⌊log24⌋ = 2, and ⌊log25⌋ results in 2.

For any complete binary tree, if the contained nodes are numbered from left to right in order (as shown in Figure 3a)), for any node i, the complete binary tree has the following conclusions:

  1. When i>1, the father node is the node [i/2]. (When i=1, it means the root node, no parent node)
  2. If 2*i>n (the number of summary points), then node i must have no left child (leaf node); otherwise, its left child is node 2*i.
  3. If 2*i+1>n, then node i must have no right child; otherwise, the right child is node 2*i+1.

to sum up

This section introduces what a binary tree is and the properties of a binary tree. It also introduces a full binary tree and a complete binary tree and their unique properties. Most of it abstracts the C language Chinese website . Please feel free to correct any errors or omissions.

Guess you like

Origin blog.csdn.net/qq_52208569/article/details/115361166