Red-black tree - sort binary - balanced binary

https://www.cnblogs.com/guweiwei/p/7080971.html

1. Sort binary tree

   

Binary tree is a special sort binary tree structure, can easily all the nodes of the tree sorting and retrieval.

Sorting binary tree is either an empty binary tree is either a binary tree with the following properties:

  • If its left subtree is not empty, then the value of the left sub-tree, all the nodes are less than the value of the root node;
  • If it is not empty right subtree, the right subtree are greater than values ​​of all the nodes of the root node and its value;
  • Its left and right subtrees are also sort binary tree .

Figure 1 shows an ordered binary tree:

 

1. FIG binary ordering

To binary sort, if by inorder traversal can be ordered in ascending sequence obtained. Binary tree shown in FIG. 1, preorder obtained:

 

1.1 creates a sorted binary tree

To create a sorted binary tree, which is the process of continually add nodes to a binary tree sorting step of adding nodes to sort binary tree as follows:

  1. To the root of the current node to start the search.
  2. Take the value of the current node and the new node comparison.
  3. If a larger value of the new node, places the right child of the current node as the new current node; if the value of the new node is smaller, places the left child node of the current node as the new current node.
  4. Repeat steps 2 and 3 two, until the search up to the appropriate leaf node.
  5. Add the new node is a leaf node in step 4 to find the child node; if more new node is added to the right child; otherwise, added as a left child node .
 
1.2 Delete sort binary tree node
 

When the program remove a node from a binary tree sort, in order to let it remain as a sort of binary tree, the program must be maintained in this sort of binary tree. Maintenance can be divided into the following situations:

(1) removed node is a leaf node, you can simply remove it from its parent node.

(2) is deleted only the left sub-tree node p, p add left subtree of the parent node p pL into the left subtree can; p nodes are removed and only the right subtree, add p pR into the right subtree to the right subtree of the parent node p.

(3) if they are deleted node p left and right subtrees are not empty, there are two approaches:

  • The left or right child of the parent node pL q is p (depending on the parent node q p is the left and right child node), the node is p pR right child node s before the chemotaxis sequence ( s is the lower right node pL, pL is the largest subtree of nodes).
  • In order to pre chemotaxis or a successor node p p alternatively referred node, prior or subsequent node in order to hasten and then deleted from the original binary tree sorting. (I.e. nodes with the lowest node is greater than the maximum or smaller than p p p nodes can be replaced).

 

2. balanced binary tree

 

Balanced binary tree: it is the absolute value of the difference in height empty tree or its two left and right subtrees of not more than 1, and the left and right sub-trees are a balanced binary tree. Common algorithm structure and method of adjusting the red and black balanced binary tree, AVL, Treap like. Formula smallest node binary tree balanced as F (n) = F (n -1) + F (n-2) +1 This is similar to a recursive series , can refer to the Fibonacci sequence, the root node is 1, F ( n-1) is the number of nodes left subtree, F (n-2) is the number of nodes right subtree.

3. red-black tree

3.1 Properties of red-black tree

In the original red-black tree sorted binary tree adds several requirements as follows:

 

  • Property 1: Each node is either red or black.
  • Property 2: the root is always black.
  • Property 3: All leaf nodes are empty nodes (ie null), and black.
  • Property 4: two children each red node are black. (Not on the path from the root to each leaf has two consecutive red nodes)
  • Nature 5: a path from any node to each of its sub-tree leaf node contains the same number of black nodes.

 

Java  implemented red-black tree may have the structure shown in FIG. 6:


6. Java schematic view of the red-black tree

Note: This article is all about schematic diagram of red-black tree with white for red. Node or black with a black representation.

Depending on the nature 5: black tree from the root node to each leaf node of the path contains the same number of black nodes, black nodes from the root node to the leaf nodes included in the path tree is referred to as "black height (black -height) ".

4 nature to ensure the longest path from the root node of the leaf node does not exceed twice the length of any other path. If there is a dark red-black tree height 3: The shortest path length from the root node to the leaf node is 2, the node path all black (Black Node - Node Black - black nodes). The longest path may be only 4, is inserted between each node of a red node black (black Node - Node Red - Black Node - Node red - black nodes), 4 properties ensure no more red nodes may be inserted. Thus, the red-black tree longest path is an alternate path to red and black.

Red-black trees and balanced binary tree

 

Red-black tree is not really a balanced binary tree, but in practice, the statistical properties of red-black tree is higher than the balanced binary tree, but extreme performance slightly worse.

From this we can conclude that: For a given height of the black red-black tree is N, the length of the shortest path from the root to the leaf node is N-1, the longest path length is 2 * (N-1).

Tip: depth ordering a direct impact on the binary tree retrieval performance, as noted earlier, when the insertion node itself is small to large order, a sorted list will become a binary tree, a binary tree search of the minimum performance of this sort: N nodes binary Tree depth is N-1.

Red-black tree above this limit to ensure that it is roughly balanced - not because of the high red-black tree of infinite increase, thus ensuring that the red-black tree in the worst case is efficient, does not appear ordinary sort of binary tree Happening.

Since the red-black tree is only a special sort binary tree, thus read-only operations on the read-only operations are identical to the red-black ordering ordinary binary tree, but the red-black tree to maintain a substantially balanced, so retrieval performance much better than the binary tree sorting .

However, insert and delete operations results in a tree no longer meets the red-black tree features red and black tree, so the insert and delete operations need to be a certain degree of maintenance to ensure that the insert node, the node tree after delete is still red black tree

 

The answer is: ABCD

Guess you like

Origin www.cnblogs.com/woainixxx/p/11163948.html