Original link: https: //blog.csdn.net/abel_liujinquan/article/details/89435686z
Author: https: //blog.csdn.net/Abel_Liujinquan
1, the concept of a binary tree
Binary tree: each node has at most two branches (the degree of branching less than 2) tree structure, the tree may be empty.
Root: a tree top node is called the root node.
Left and right sub-tree: a node in the left branch is called the left subtree, right branch is called the right subtree.
About children: a node in the left and right branches of the root node is called the left and right children.
Sibling node: The node with the same parent node of each other sibling.
Degree nodes: the node has a number of sub-tree.
Leaf node: The node does not have any children is called a leaf node.
Internal node: non-leaf nodes are called internal nodes.
Root level: defined starting from the root node, the root of the first layer, the second layer is a child root, so the count, until the node.
The depth and height of the tree: the maximum level of nodes in a binary tree called a binary tree of depth or height.
2, binary classification
1), complete binary tree
In a binary tree, except the last one, it is full, and is full or the last layer, or the lack of a continuous right of several nodes become complete binary tree. as the picture shows:
2), full binary tree
A depth of k, and there are 2k + 1-1 2 ^ {k + 1} -12
Binary k + 1-1 nodes become full binary tree. as the picture shows:
3), a binary search tree (Binary Search Tree)
Also known as binary search tree, binary tree sorting, can meet all the left subtree is empty tree nodes, or node <node with <right subtree of the node, the node is equal to the value does not exist, as shown:
4), balanced binary tree (Balanced Binary Tree)
Is a structurally balanced binary search tree, i.e., a leaf node does not exceed a depth of 1 can be inserted into the completed O (logn), to find and delete operations, as shown in FIG structure common balanced binary tree has AVl, red black trees.
5), AVL tree
Height balanced tree is also known, is a self-balancing binary search tree first invention, the maximum height difference between the two sons subtree of any node 1, additions and deletions may need to rotate or more to rebalance a tree the tree, the tree shown in Figure.
Former equilibrium (non-AVL tree):
After equilibration (AVL trees):
6), red-black trees (Red-black tree)
是一种自平衡二叉查找树,又称为“对称二叉B树”,除了满足所有二叉查找树的要求之外还需要满足以下要求:
(1)节点是红色或者是黑色的
(2)跟节点是黑色的
(3)每个叶子节点都是黑色的(叶子是NIL节点)
(4)每个红色节点必须有两个黑色节点(从叶子到根节点的所有简单路径上不可能有两个连续的红色节点)
(5)从任一节点到其每个叶子的所有简单路径都饱和相同数目的节点
注:
NIL节点就是空节点,二叉树中庸NIL节点代替NULL
简单路径:指顶点序列中顶点不重复出现的路径
3、二叉树的性质
性质1:在非空二叉树的第i层上最多有2^i−1 个节点。
性质2:深度为K的二叉树最多有2^k-1个节点。
性质3:对于任意一棵二叉树,如果度为0的节点个数为n0,度为2的节点个数为n2,则n0 = n2 + 1。
性质4:具有n个节点的完全二叉树的深度k=⌊log2n⌋+1。
性质5:对于含n个节点的完全二叉树中编号为i(1≤ \leq≤i≤ \leq≤n)的节点:
- 如果i=1,则i节点是这可完全二叉树的根,没有双亲,否则其双亲的编号为⌊i/2⌋。
- 如果2i>n,则i节点没有左孩子,否则其左孩子的编号为2i。
- 如果2i+1>n,则i节点没有右孩子,否则其有孩子的编号为2i+1。