【发布时间】:2012-10-26 23:31:51
【问题描述】:
需要定义一个seek(u,v)函数,其中u是树中的新结点(我要开始搜索的结点),v是新结点下面的后代个数,这个函数将返回最高键值的索引。这棵树没有一个 BST,可以有许多子节点。示例:
input:
5 // tree with 5 nodes
1 3 5 2 7 // the nodes' keys
1 2 // 1 and 2 are linked
2 3 // 2 and 3 are linked
1 4 // 1 and 4 are linked
3 5 // 3 and 5 are linked
4 // # of seek() requests
2 3 // index 2 of tree, which would be key 5, 3 descendants from index 3
4 1 // index 4 of tree, but only return highest from index 4 to 4 (it would
// return itself)
3 2 // index 3, next 2 descendants
3 2 // same
output:
5 // Returned index 5 because the 7 is the highest key from array[3 'til 5]
4 // Returned index 4 because the range is one, plus 4's children are null
5 // Returned index 5 because the 7 is the highest key from array[4 'til 5]
5 // Same as prior line
我正在考虑将新根放入新的红黑树中,但找不到有效保存每个节点的后继或前驱信息的方法。还考虑放入一个数组,但由于不平衡和未排序树的性质,它不能保证我的树会被排序,而且因为它不是 BST,所以我不能执行无序树遍历。关于如何从特定范围获得最高键的任何建议?
【问题讨论】: