需要定义一个 seek(u,v) 函数,其中 u 是树中的新节点(我要开始搜索的节点),v 是新节点下面的后代数,这个函数会返回 index键值最高的。这棵树没有一个 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,所以我不能执行无序树遍历。关于如何从特定范围获得最高键的任何建议?