When you switch to a red-black tree, is there any reason to choose one shape over another?

I have a library of linked lists / binary tree methods for use when standard containers are not suitable - for example. when there are different types of nodes, or when I need to convert from a binary tree to a list and vice versa. It includes red-black wood processing.

One of the methods is converted from a double list to a perfectly balanced simple binary tree in O(n)time (given that the number of elements is known in advance). The algorithm is known as "folding" - this is the second half of the binary tree balancing algorithm that was once published by Dr. Dobbs. The steps are basically ...

  • Given the size of the tree, determine the size of the left and right subtrees

  • Count left subtree

  • Put a node from the list to be used as root

  • Recurse for the correct subtree

  • Link the subtrees to the root

I also have a similar method that creates a red-black tree. The principle is the same, but recursion tracks node heights - zero vertices are created in red, all the rest are black. The initial height calculation is based on the highest bit set in tree size and is split so that a perfectly balanced tree of size (2^n)-1has only black nodes (recursion only drops to a height of one).

The point here is that I only have red nodes at the sheet level, and at most half the nodes are red.

, , - , . , . . , , ( ), , .

, - ?

- , , - , ?

+3
2

- pysicist , , , , . .

, , node .


, , . , - , , .

, - - 2-4 , , 2, 3 4 . node 2-4 - , .

- , i- 2 i , i- 0, 1, 2. , . 4 dec 100 20 12.

, ; 3, 1 .. , 0 1.

2-4, . , , . , node, . , , node .

, 1 2 . Xall , k , k-1, O (1).

, , O (1) : 2 k - 1. k 1 . & Theta; (k). , & Theta; (k) .

, 1 , . 2-4 , 3- , .

- node 3 node 2-4. , - , .

, , . , , .

+1

: .

, . - , , .

. , , . , , .

, .

+2

Source: https://habr.com/ru/post/1721937/


All Articles