I've got a perfect binary tree that's enumerated the post-order way. An example of such tree would be
15
7 14
3 6 10 13
1 2 4 5 8 9 11 12
The size of the tree is known to me. I'm looking for a formula or a simple algorithm that would take a single number as an input (the ID of the vertex I'm interested in) and return also a single number - the ID of the parent. It's quite easy to traverse the tree from the top and get the result in O(log n)
. Is there a faster solution? I'm mostly interested in the leaves, so if there's a solution for the special case, bring it on too.
Parent index could be found in O(log* n) time and O(1) space.
Here log* n means iterated logarithm: the number of times the logarithm function must be iteratively applied before the result is less than or equal to 1.
Actually that may be done even faster - in O(1) time if we could afford O(n) space for a large lookup table (storing parent index for each node in the tree).
Below I'll sketch several algorithms that do not need any extra space and give result in O(log n) worst case time, O(log log n) expected time, O(log log n) worst case time, and O(log* n) worst case time. They are based on following properties of post-order indexes for perfect binary tree:
i
.Properties #1 and #2 give simple algorithms to get parent node for some nodes of the tree: for indexes of the form 2i-1, multiply by
2
and add1
; for indexes of the form 2i-2, just add1
. For other nodes we could repeatedly use property #4 to come to the node satisfying property #1 or #2 (by subtracting sizes of several left sub-trees), then find some parent node located on the leftmost path, then add back all previously subtracted values. And property #3 allows to quickly find size of which sub-trees should be subtracted. So we have following algorithm:((x+1) & x) != 0
and((x+2) & (x+1)) != 0
repeat step 2.1
. Accumulate the difference.((x+1) & x) == 0
, multiply by2
and add1
; otherwise if((x+2) & (x+1)) == 0
, add1
.For example,
12
(in binary form0b1100
) is transformed on step #2 to0b0101
, then to0b0010
(or2
in decimal). Accumulated difference is10
. Step #3 adds1
and step #4 adds back10
, so the result is13
.Other example:
10
(in binary form0b1010
) is transformed on step #2 to0b0011
(or3
in decimal). Step #3 doubles it (6
), then adds1
(7
). Step #4 adds back accumulated difference (7
), so the result is14
.Time complexity is O(log n) - but only when all elementary operations are executed in O(1) time.
To improve time complexity we could perform several iterations of step #2 in parallel. We could get
n/2
high-order bits of the index and compute population count on them. If after adding the result to the remaining low-order bits the sum does not overflow to these high-order bits, we could apply this approach recursively, with O(log log n) complexity. If we have an overflow, we could roll back to the original algorithm (bit-by-bit). Note that all set low-order bits should be also treated as overflow. So the resulting complexity is O(log log n) expected time.Instead of rolling back to bit-by-bit we could handle overflow using binary search. We could determine how many high-order bits (less than
n/2
) are to be selected so that we either have no overflow or (as for index0b00101111111111
) the number of selected non-zero high-order bits is exactly1
. Note that we do not need to apply this binary search procedure more than once because second overflow would not happen while number of bits in the number is greater than O(log log n). So the resulting complexity is O(log log n) worst case time. All elementary operations are assumed to be executed in O(1) time. If some operations (population count, leading zero count) are implemented in O(log log n) time, then our time complexity increases to O(log2 log n).Instead of dividing bits of the index into two equal sets we could use different strategy:
0
to1
determines separation point for high-order/low-order bits.((x+1) & x) != 0
and((x+2) & (x+1)) != 0
, continue from step #1.((x+1) & x) != 0
and((x+2) & (x+1)) != 0
, also process this as right child.((x+1) & x) == 0
, multiply by2
and add1
; otherwise if((x+2) & (x+1)) == 0
, add1
.If condition of step #3 is satisfied, this means addition on step #2 resulted in carry to "separation" bit. Other low-order bits represent some number that cannot be greater than original population count. Number of set bits in this number cannot be greater than logarithm of population count of the original value. This means that number of set bits after each iteration is at most logarithm of the number of set bits on previous iteration. Therefore worst case time complexity is O(log* n). This is very close to O(1). For example, for 32-bit numbers we need approximately 2 iterations or less.
Every step of this algorithm should be obvious, except probably step #5, correctness of which is to be proven. Note that this step is executed only when adding population count results in carry to "separation" bit but adding population count of only high-order bits does not result in this carry. "Separation" bit corresponds to value 2i. Difference between population count of all bits and population count of only high-order bits is at most
i
. So step #5 deals with a value at least 2i-i. Let's apply bit-by-bit algorithm to this value. 2i-i is large enough so that biti-1
is set. Clear this bit and add1
to the value in bits0..i-2
. This value is at least 2i-1-(i-1) because we just subtracted 2i-1 and added1
. Or if we move index one position to the right, we have again at least 2i-i. If we repeat this procedure we'll always find non-zero bit at positioni-1
. Each step gradually decreases difference between value in bits0..i-1
and nearest power of2
. When this difference comes to2
, we could stop this bit-by-bit algorithm because current node is clearly a right child. Since this bit-by-bit algorithm always comes to the same result, we could skip it and always process current node as a right child.Here is C++ implementation of this algorithm. More details and some other algorithms could be found on ideone.
If we need to find parent only for a leaf node, this algorithm and code may be simplified. (Ideone).