假设一个完整的二叉树,每个节点都可以使用它在给定树遍历算法中出现的位置来处理。
例如,高度为3的简单完整树的节点索引如下所示:
首先是广度(也就是水平顺序): 0
/ \
1 2
/ \ / \
3 4 5 6
首先订购部门:
6
/ \
2 5
/ \ / \
0 1 3 4
给出了树的高度和后序遍历中的索引。
如何根据此信息计算广度优先索引?
答案 0 :(得分:1)
我认为它必须以迭代/递归方式计算。话虽如此,有人会在37秒内通过一个简单的单行计算来进行并向我投降。尽管如此,它可以通过递归思考来解决。考虑深度优先的后序遍历的简单树(从1开始):
3
/ \
1 2
从递归的角度来看,这就是你必须要考虑的全部内容。您要么位于子树(3)的根部,要么位于子树(1)的左侧,要么位于右侧部分(2)中。如果你是根,那么你就完成了。否则,左右子树是相同的,右子树中的后序遍历索引等于相应的左子树索引+子树中的节点数。
以下代码在 O(log n)
中执行此计算。对于深度为10(1023个节点)的树,它以最多10次迭代(递归)计算索引值。
它跟踪给定节点的深度,并根据它是处理左子树还是右子树来计算广度优先的行位置。请注意,这使用从1开始的索引值。我发现用这些术语来思考它更简单(深度为2的树中有3个节点,后序遍历中的最顶层节点为3)。还要注意,它认为树的深度为1才能有一个节点(我不确定这是否是典型约定)。
// Recursively compute the given post-order traversal index's position
// in the tree: Its position in the given level and its depth in the tree.
void ComputePos( int treedepth, int poindex, int *levelposition, int *nodedepth )
{
int nodes;
int half;
// compute number of nodes for this depth.
assert( treedepth > 0 );
nodes = ( 1 << ( treedepth )) - 1;
half = nodes / 2; // e.g., 7 / 2 = 3
//printf( "poindex = %3d, Depth = %3d, node count = %3d", poindex, treedepth, nodes );
(*nodedepth)++;
if ( poindex == nodes ) {
// This post-order index value is the root of this subtree
//printf( " Root\n" );
return;
}
else if ( poindex > half ) {
// This index is in the right subtree
//printf( " Right\n" );
poindex -= half;
*levelposition = 2 * *levelposition + 1;
}
else {
// Otherwise it must be in the left subtree
//printf( " Left\n" );
*levelposition = 2 * *levelposition;
}
treedepth -= 1;
ComputePos( treedepth, poindex, levelposition, nodedepth );
}
int main( int argc, char* argv[] )
{
int levelposition = 0; // the 0-based index from the left in a given level
int nodedepth = 0; // the depth of the node in the tree
int bfindex;
int treedepth = atoi( argv[1] ); // full depth of the tree (depth=1 means 1 node)
int poindex = atoi( argv[2] ); // 1-based post-order traversal index
ComputePos( treedepth, poindex, &levelposition, &nodedepth );
//printf( "ComputePos( %d, %d ) = %d, %d\n", treedepth, poindex, levelposition, nodedepth );
// Compute the breadth-first index as its position in its current
// level plus the count of nodex in all the levels above it.
bfindex = levelposition + ( 1 << ( nodedepth - 1 ));
printf( "Post-Order index %3d = breadth-first index %3d\n", poindex, bfindex );
return 0;
}
以下是为下一个树(深度4)计算的值,它显示了后序遍历索引值(从1开始)。
15
/ \
/ \
/ \
/ \
/ \
7 14
/ \ / \
/ \ / \
3 6 10 13
/\ / \ /\ / \
1 2 4 5 8 9 11 12
[C:\tmp]for /l %i in (1,1,15) do po2bf 4 %i
Post-Order index 1 = breadth-first index 8
Post-Order index 2 = breadth-first index 9
Post-Order index 3 = breadth-first index 4
Post-Order index 4 = breadth-first index 10
Post-Order index 5 = breadth-first index 11
Post-Order index 6 = breadth-first index 5
Post-Order index 7 = breadth-first index 2
Post-Order index 8 = breadth-first index 12
Post-Order index 9 = breadth-first index 13
Post-Order index 10 = breadth-first index 6
Post-Order index 11 = breadth-first index 14
Post-Order index 12 = breadth-first index 15
Post-Order index 13 = breadth-first index 7
Post-Order index 14 = breadth-first index 3
Post-Order index 15 = breadth-first index 1
答案 1 :(得分:0)
蛮力的方式,直到你找到一个更好的答案:
使用索引:索引的前半部分是左子树,下半部分是右子树,中间节点是根。递归每个子树。
遍历树广度优先,将计算出的广度优先索引作为映射对的值放入map,键是节点的值。 map.put( node.value, tree_visitor.current_index)
询问地图,传递所需的键(后序节点的索引)以获取相应的广度优先节点的索引。