Binary tree-definition / characteristic / storage / traversal

Definition and characteristics of binary tree

Tree related concepts

The data structure of a tree mimics the concept of a tree in nature. A tree in nature has roots, leaves, and branches. The same is true of a tree in a data structure, but only the other way around:

each element in it is called a node. The vertices of the tree (nodes without parent elements) are called root nodes, such as E; the end nodes of each branch (nodes without child elements) are called leaf nodes, such as G, H, I, J, K, L; used to connect The relationship between adjacent nodes is called the parent-child relationship. For example, E is the parent node of A and F, and A and F are the child nodes of E; multiple child nodes with the same parent node are called sibling nodes. For example, A and F are sibling nodes. .

The number of child nodes owned by a node is called the degree of the node. Obviously, the degree of the leaf node is 0, and the degree of the tree is the maximum value of the degree of each node in the tree.

In addition, the tree has the concepts of height, depth and layer:

Note: In fact, the linear table can also be regarded as a special tree, except that all the nodes are on a branch, the first element is the root node, and the last element is the child node, there is no sibling node. The number of layers is the length of the linear table.

In addition, there are some places where the depth of the binary tree is defined as the maximum number of nodes. For example, this is the definition of "big talk data structure". This problem is not big, it is just a definition:

multiple disjoint trees can constitute a forest. .

Definition of binary tree

The binary tree is the most common tree structure we usually encounter. It is a special tree. As the name implies, each node has at most two "forks", that is, two child nodes, which are the left child node and the right child node. However, the binary tree does not require each node to have two child nodes, some nodes have only left child nodes, and some nodes have only right child nodes. For example, the following are all binary trees:

According to the saturation of the left and right child nodes, we extract two special binary trees from the binary tree— full binary tree and complete binary tree . A full binary tree means that all branch nodes have left and right child nodes, and all leaf nodes are on the same layer, as shown in Figure 2 above is a full binary tree. A complete binary tree is more complicated. A binary tree with a depth of k and n nodes, if and only if each node in it, can correspond to a full binary tree of the same depth k, with nodes from 1 to n in one-to-one correspondence, said For a complete binary tree, for example, Figure 3 above is a complete binary tree.

Binary tree features

Before discussing the creation and storage of a binary tree, we first summarize some of the characteristics of the binary tree for later use (here the depth of the number of binary trees defines the maximum number of layers used, if you start from 0, you can deduce it yourself)

  • Property 1:

There are at most 2 ^ (i-1) nodes in the i-th layer.

  • Nature 2:

A binary tree of depth k has at most 2 ^ k-1 nodes.

  • Nature 3:

For any binary tree, the number of leaf nodes is n0, and the number of nodes with degree 2 is n2, then n0 = n2 + 1.

  • Nature 4:
  • Nature 5:

Guess you like

Origin www.cnblogs.com/stringarray/p/12749293.html