The nature and types of binary trees

One, binary tree

Binary tree (logical structure): A binary tree is a finite set of n (n>=0) nodes

1. When n=0, the binary tree is empty

2. When n>0, it consists of the root node and two disjoint left and right subtrees called roots. The left subtree and the right subtree are also a binary tree respectively

Note: the binary tree is divided into left and right

Five basic forms of binary trees
Insert picture description here

1. Full binary tree

Definition of full binary tree: a binary tree with height h and (2^h)−1 nodes is a full binary tree.

Features: all leaf nodes are in the last layer

Nature: For the node numbered i, if it exists, the parent number is rounded down (i/2), the left child is 2i, and the right child is 2i+1
Insert picture description here

2. Complete binary tree

Definition of a complete binary tree: Suppose a binary tree with a height of h and n nodes, if and only if each node corresponds to the node numbered 1~n in the full binary tree with height h, it is called Complete binary tree

The properties of a complete binary tree:
1. If i<= round down (n/2), then node i is a branch node, otherwise it is a leaf node

2. Leaf nodes can only appear on the two largest levels. For the leaf nodes of the largest level, they are arranged in the leftmost position in turn

3. If a node with degree 1 exists, there may be one, and it is the branch node with the largest number, and the child node must be the left node
Insert picture description here

3. Binary sort tree

Binary sort tree, also known as binary search tree

A binary tree, if the tree is not empty, it has the following properties: if there is a left subtree or a right subtree for any node, the keys of all nodes on the left subtree are smaller than the node, and all nodes on the right subtree The key of the node is greater than the node

Features: it can be an empty tree, all nodes of the left subtree<root node<right subtree, there is no node with the same value

Binary sort tree is also defined in a recursive form
Insert picture description here

4. Balanced binary tree

Definition: the tree at any junction depth're just left subtree right subtree and not more than 1

Features: It is a binary search tree with balanced structure, that is, the depth difference of leaf nodes is not more than 1, and it can complete the insertion, search and deletion operations in O(logn). The structure is shown in the figure. Common balanced binary trees are AVl trees
Insert picture description here
AVL trees such as, red and black trees are
also known as highly balanced trees. They are the first self-balanced binary search tree invented. The maximum difference between the heights of the two sons of any node is 1, and the addition and deletion may need to pass once or Multiple tree rotations to rebalance the tree

5. Red and black trees

Features: The red-black tree is a self-balancing binary search tree, also known as a "symmetric binary B-tree". In addition to meeting the requirements of all binary search trees, the following requirements must also be met:
(1) The node is red or is black

(2) The follow node is black

(3) Each leaf node is black (the leaf is a NIL node)

(4) Each red node must have two black nodes (it is impossible to have two consecutive red nodes on all simple paths from the leaf to the root node)

(5) All simple paths from any node to each of its leaves are saturated with the same number of nodes.
Note:
NIL nodes are empty nodes, and NIL nodes in the binary tree are in the meantime instead of NULL
simple paths: refer to the paths where the vertices do not appear repeatedly
Insert picture description here

Second, the nature of the binary tree

1. The number of leaf nodes on a non-empty binary tree is equal to the number of nodes with degree 2 plus 1, that is, n 0 = n 2 + 1 ( n 0 refers to leaf nodes, n 2 refers to nodes with degree 2)

Explanation: n 0 = n 2 + 1 , which is derived from the two formulas n = n 0+ n 1 + n 2 , n = n 1 + 2 n 2 + 1 . n represents the number of summary points. The origin of formula 1 is that the sum of the nodes with degree 0 and degree 1, and the number of degree 2 is the sum of points; the origin of formula 2 is that the node with degree 1 is multiplied by 1 (because degree is The node of 1 has a child node, so multiply it by 1), add the node with degree 2 and multiply by 2 (because the node with degree 2 has two nodes, so multiply by 2), and finally add 1 , Because there is another root node not included

2. There are at most 2^(k-1) nodes on the Kth level of the non-empty binary tree (k>=1)

Explanation: It can be obtained by mathematical induction

Insert picture description here
3. A binary tree with height h has at most 2^h-1 nodes (h>=1)

Explanation: The third property can be obtained from the second property, which can be obtained by adding the number of nodes in each layer

4. The complete binary tree is numbered 1, 2, ..., n from top to bottom and from left to right. The relationship is as follows:

   1) When i>1, the label of the parent node of node i is rounded down (i/2), that is, when i is an even number, the number of the parent node is i/2, which is the parent node When i is an odd number, the number of the parent node is (i-1)/2, and he is the right child of the parent node.

   2) When 2i<=n, the left child number of node i is 2i, otherwise there is no left child

   3) When 2i+1<=n, the right child number of node i is 2i+1, otherwise there is no right child

   4) The level of node i is rounded down (log 2 i )+1

   5) The height of a complete binary tree with n (n>0) nodes is rounded down (log 2 n )+1 or rounded up log 2 (n+1)

This article is referenced from "The King's Way"

Everyone is welcome to read. I have limited knowledge, and there are inevitably mistakes or omissions in the blog I wrote. I hope you guys can give me some advice. Thank you.

Guess you like

Origin blog.csdn.net/qq_41936224/article/details/108116862