Hello everyone, today continue to learn the tree data structure. In the previous chapter, we spoke about the tree's child brother notation when referring to the two-fork tree. Today, we are going to learn about the related properties of binary trees.
One or two fork tree definition:
Binary tree: A finite set of n (n≥0) nodes, either an empty set (null, called an empty tree), or a root node, two disjoint, a Zuozi of the root node, and a two-ary tree of the right subtree. (Note: We are aware that when we give a two-fork tree definition, we take advantage of the recursive concept.) is to give the concept of two fork tree, and then use the concept of binary tree to explain the binary tree. Recursion is an important method, which is important in the data structure, which we will refer to in later chapters. )
Let's look at the picture to understand the binary tree:
Two or two fork tree features:
1, the node in the binary tree of the degree, must be less than or equal to 2. (according to the binary tree definition, consists of a root node, two disjoint, called the root node of the left sub-tree, the right subtree of two-tree composition.) Note: The degree is not only 2, is at most 2)
2, Zuozi and right subtree are in order, the order can not be reversed.
3, even if there is only one subtree in the tree, but also to distinguish between Zuozi, right sub-tree.
There are 5 basic types of three or two-fork trees:
1. Empty binary tree
2. Only the root node
3. Only the Zuozi two-fork Tree
4. Only the two-tree of the right subtree
5. Both Zuozi and right sub-tree
Four, special two-fork tree:
1. Oblique binary tree:
Since it is a slanting binary tree, it must have a slope.
Above these two graphs, is the oblique binary tree. (Note: It's not quite like a linear watch.) In fact, the linear table is the special manifestation of the binary tree.
2. Full two fork tree:
As the name implies, since is full of two fork tree, that is, each node has two sub-trees. :
If the binary tree, each node has the Zuozi, the right subtree, and all the leaf nodes are in the same layer, such a two-fork tree is called full two fork tree.
Full two-fork tree features:
A. Leaf nodes can only appear on the last layer, and if not, they cannot reach the two fork tree.
B. The number of non-leaf nodes can only be 2, if not, there will be "lack of arms and fewer legs" phenomenon.
C. In the same depth of the two-fork tree, the nodes full of two forks, the most nodes of the leaf node.
3. Complete binary tree:
For a two-fork tree with n nodes, which is numbered as the node position numbered I (1≤i≤n) in the same depth as the number I nodes in the two-ary tree, the two-tree is called a complete binary tree. Completely binary tree is not good to understand, we look at one:
Above the left, is a completely binary tree, the right is a tree full of two forks. We are in the heart to the right picture full of two tree by sequence number. By comparing the left and right graphs, we find that any node in the left image has a corresponding node in the right image.
Here are a few pictures that are not completely binary trees:
Full binary Tree features:
A. If the node has a degree of 1, it can only be a left subtree. (No only right sub-tree nodes exist.)
B. Leaf nodes can only be present on the bottom two floors.
C. The lowest leaf node, must be concentrated in the left side of the continuous position.
D. The bottom two layer, if there are leaf nodes, must be concentrated in the right continuous position.
E. The tree with the same number of nodes, the depth of the complete binary tree is minimal.
Five or two fork tree nature:
Next, let's learn about the nature of the binary tree. In practice, it is operated by the nature of the binary tree. The nature of the binary tree is a very important concept.
A. In a binary tree, the total number of nodes in layer I is 2^ (i-1)
First floor: 1=2^ (1-1) =2^0=1
Second floor: 2=2^ (2-1) =2^1=2
Layer Three: 4=2^ (3-1) =2^2=4
......
......
B. Two-fork tree with a depth of k, the total number of nodes equals 2^k-1
Depth is 1:1=2^1-1=1
Depth is 2:3=2^2-1=3
Depth is 3:7=2^3-1=7
......
......
C. If N0 is a leaf node, N2 is a node with a degree of 2. The n0=n2+1.
In the binary tree, except for the leaf node is a node with a degree of 1 and a degree of 2. We assume that the node with a degree of 1 is N1, the node with a degree of 2 is N2, and the leaf node is n0. The total number of nodes in the tree is n=n0+n1+n2. We look at the connection lines between nodes. Because the root node only goes out of line, there is no entry line. So bus +1 is the total number of nodes, N1+2n2+1=n0+n1+n2 n0=n2+1.
D. The depth of a complete binary tree with n nodes is [log2n]+1 ([x] = maximum integer not greater than X).
E. If a complete binary tree with n nodes (whose depth is [log2n]) is numbered by sequence (from the first layer to the [log2n]+1 layer, each layer from left to right), any node I (1≤i≤n) is:
1. If i=1, then node i is the root of the two fork tree, no parents, and if i>1, the parents are [I/2].
2. If 2i>n, then node I has no left child (node i is the leaf node); otherwise its left child is node 2i.
3. If 2i+1>n, then node I has no right child, otherwise the right child is a junction 2i+1
We will explain the storage structure of the binary tree in the next chapter
Data structure and algorithm----tree (bottom)