Data Structure Notes - Definition and Properties of Binary Trees

In some TV programs, commodity prices will be guessed, and some people will accumulate the numbers bit by bit. This strategy is too inefficient. In fact, there is a classic halving search algorithm, which is similar to the binary tree we are going to talk about today.

Definition of binary tree

Binary tree: is a finite set of n (n>=0) nodes, which is either an empty set (called an empty binary tree), or consists of a root node and two disjoint trees, respectively called root nodes A binary tree consisting of a left subtree and a right subtree.

The following figure is a binary tree:

write picture description here

Binary tree features

The characteristics of a binary tree are:

  • Each node has at most two subtrees, so there is no node with degree greater than 2 in the binary tree. Note that there are not only two subtrees, but at most. It is okay to have no subtree or to have a subtree.
  • The left subtree and the right subtree are in order, and the order is arbitrarily reversed.
  • Even if a node in the tree has only one subtree, it is necessary to distinguish whether it is a left subtree or a right subtree. As shown in the figure: tree 1 and tree 2 are the same tree, but they are different binary trees.

write picture description here

A binary tree has five basic forms:

  1. empty binary tree;
  2. only one root node;
  3. The root node has only left subtree;
  4. The root node has only the right subtree;
  5. The root node has both left and right subtrees.

special binary tree

Let's introduce some special binary trees.

1. Oblique tree

As the name suggests, a sloping tree must be sloping, but there is still a particular emphasis on sloping there.

A binary tree whose nodes have only left subtrees is called a left-sloping tree, and a binary tree whose nodes have only right subtrees is called a right-sloping tree. Both are collectively referred to as oblique trees.

The following two diagrams are the left-sloping tree and the right-sloping tree:

write picture description here

write picture description here

2. Full binary tree

Su Dongpo has a poem saying: People have joys and sorrows, and the moon is cloudy and sunny, and this is difficult to complete . It means that perfection is ideal and imperfection is life.

The examples we usually see are all binary trees with high left and right low, and uneven binary trees. Is there a perfect binary tree?

In a binary tree, if all branch nodes have left subtree and right subtree, and all leaf nodes are on the same level, such a binary tree is called a full binary tree . As shown

write picture description here

Just because each node has left and right subtrees, it cannot be regarded as a full binary tree, and all leaf nodes must be at the same level, thus achieving the balance of the binary tree. Therefore, the characteristics of a full binary tree are:

  1. Leaves can only appear on the bottom layer, and can not achieve balance when they appear on other layers;
  2. The degree of a non-leaf node must be 2;
  3. In a binary tree of the same depth, a full binary tree has the largest number of nodes and the largest number of leaf trees.

3. Complete binary tree

A binary tree with n nodes is numbered in layer order, if the node numbered i (1<=i<=n) is in the exact same position as the node numbered i in the full binary tree of the same depth , the binary tree is called a complete binary tree. As shown in the figure:

write picture description here

First of all, we must distinguish literally, the difference between "complete" and "full", a full binary tree must be a complete binary tree, and a complete binary tree is not necessarily full.

Note that the numbers in the complete binary tree are the same as those in the full binary tree, and the numbers are all consecutive. If there is a disconnection, it is not a complete binary tree. The three trees in the following figure are not complete binary trees.

write picture description here

Features of a complete binary tree:

  1. Leaf nodes can only appear in the bottom two layers;
  2. The lowermost leaves must be concentrated in the left continuous position;
  3. On the penultimate layer, if there are leaf nodes, they must be in a continuous position on the right;
  4. If the degree of a node is 1, then the node has only left child, that is, there is no situation where there is only right subtree;
  5. A binary tree with the same number of nodes has the smallest depth of a complete binary tree.

We also come up with a method to judge whether a binary tree is a complete binary tree, that is, look at the schematic diagram of the tree, and silently number each node layer by layer according to the structure of the full binary tree. If there is a gap in the number, it means that it is not a complete binary tree. , otherwise it is.

Properties of Binary Trees

The binary tree has some properties that need to be understood and remembered in order to make better use of it.

Binary tree properties 1

There are at most 2 i-1 nodes (i>=1) on the ith level of a binary tree .

write picture description here

Above:
Layer 1: 1: 2 1-1 = 2 0 = 1
Layer 2: 1: 2 2-1 = 2 1 = 2
Layer 3: 1: 2 3-1 = 2 2 =4
Tier 4: 8: 2 4-1 =2 3 =8

By data induction, it is easy to conclude that there are at most 2 i-1 nodes on the ith level of a binary tree .

Binary tree properties 2

A binary tree of depth k has at most 2k -1 nodes (k>=1).

Note here that the k-th power of 2 is subtracted by 1.

If there is one layer, at most 1=2 1 -1 nodes
If there are two layers, at most 1+2=2 2 -1 nodes
If there are three layers, at most 1+2+4=2 3 -1 nodes
If There are four layers, at most 1+2+4+8=2 4 -1 nodes

Through the demonstration of data induction, it can be concluded that if there are k layers, the number of nodes is at most 2 k -1.

Binary tree properties 3

For any binary tree T, if the number of terminal nodes is n 0 and the number of nodes with degree 2 is n 2 , then n 0 =n 2 +1 .

The terminal node is the leaf node, and in a binary tree, except for the leaf node, the rest are nodes of degree 1 and 2, let n 1 be the number of nodes of degree 1. Then the total number of nodes in tree T is n=n 0 +n 1 +n 2 .

write picture description here

Let's change the angle and count the connecting lines again. Since the root node only branches out and no branches enter, the total number of branch lines is the total number of nodes minus 1, n-1=n 1 +2n 2 , and because n=n 0 +n 1 +n 2 , resulting in n 0 =n 2 +1 .

Binary tree properties 4

The depth of a complete binary tree with n nodes is the largest integer not greater than log 2 n +1.

No more detailed derivation here.

Binary tree properties 5

If the nodes of a complete binary tree with n nodes are numbered in level order (from the first level to the last level, each level from left to right), for any node i (1<=i<= n) have:

  1. If i=1, node i is the root of the binary tree and has no parents; if i>1, its parents are nodes ⌊ i/2 ⌋ .
  2. If 2i>n, node i has no left child (node ​​i is a leaf node); otherwise, its left child is node 2i.
  3. If 2i+1>n, node i has no right child; otherwise, its right child is node 2i+1.

The next article will talk about the storage structure of the binary tree and traversing the binary tree. I hope you will continue to pay attention.

For more exciting content, please pay attention to my WeChat public account - Android Motor Vehicles.
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325557045&siteId=291194637