The time complexity of the red/black tree is: O (lgn)
SeeMathematical induction"Proves the time complexity of the red and black trees.
Theorem:The height of a red/Black Tree Containing N nodes is mostly 2log (n + 1).
Proof:
"The height of a red/Black Tree Containing N nodes is mostly 2log (n + 1 )"Inverse negative propositionIt is a red-black tree with a height of H. It contains at least 2 H/2-1 nodes ".
We only need to prove the inverse negative proposition to prove that the original proposition is true. That is, we only need to prove that "a red-black tree with a height of H contains at least 2 H/2-1 inner nodes ".
Starting from a node X (excluding this node) to any path of a leaf node, the number of black nodes is called the Black height of the node (X's black height), which is recordedBH (X). There are two notes about BH (x:
: According to"Features (5), That isAll paths from a node to its child nodes contain the same number of black nodes"We can see that all leaf nodes arriving from node X have the same number of black nodes.This means that the value of BH (X) is unique.!
: According to the red/black features (4), that isIf a node is red, its child nodes must be black."We can see that starting from node X, we can reach the leaf node" number of experienced black nodes "> =" number of experienced Red nodes ". Assuming X is the root node, we can draw a conclusion"BH (x)> = H/2". Then, we only need to prove that "a red-black tree with H height contains at least 2bh (x)-1.
Here, we will explain the theorem that needs to be proved
"The height of a red/Black Tree Containing N nodes is mostly 2log (n + 1 )"
The conversion only needs to prove
"A red-black tree with a height of H. It contains at least 2bh (x)-1 inner nodes ".
The following uses mathematical induction to demonstrate the red and black trees with H height. The number of inner nodes is at least 2bh (x)-1 ".
(01) when the height of the tree is h = 0,
The number of inner nodes is 0, BH (X) is 0, and 2bh (x)-1 is also 0. Obviously, the original proposition is true.
(02) when h> 0 and the height of the tree is h-1, it contains at least 2bh (x)-1-1 nodes. This is inferred from (01!
Below, known conditions from the tree's height to the H-1 roll out "when the tree's height is H, it contains the node tree 2bh (x)-1 ".
When the height of the tree is H,
For node x (x is the root node), its black height is BH (X ).
For left and right subtree of node X, their black height is BH (x) or BH (x)-1.
According to (02) known conditions, we know that "X's left and right subtree, that is, a node with a height of h-1, contains at least 2bh (x) nodes) -1-1 ";
Therefore, node X contains at least (2bh (x)-1-1) + (2bh (x)-1-1) + 1 = 2 ^ BH (X) -1. That is, node X contains at least 2bh (x)-1 nodes.
Therefore, the original proposition is true.
According to (01) and (02), "a red-black tree with H height contains at least 2 ^ BH (x)-1 nodes ".
Therefore, the height of a red-black Tree Containing N nodes is mostly 2log (n + 1 )".
The time complexity of the red/black tree is: O (lgn)