【发布时间】:2018-06-14 22:39:54
【问题描述】:
我已经用伪代码实现了一个方法 rankOfElement(x),它返回给定节点 x 的排名:
function rankofElement(x) {
rank = 0;
Node temp = root;
while (temp.key != x) {
if (x < temp.key) {
temp = temp.leftson
} else if (x > temp.key) {
rank += temp.leftson.size + 1;
temp = temp.rightson;
} else if (temp.key == x) {
return rank + temp.leftson.size
} else return "key not found"
}
现在我应该用伪代码实现一个方法 (elementbyRank(k)),它在二叉树的上下文中返回一个具有特定等级 k 的节点。
我正在为此苦苦挣扎,希望您能给我一个答案。
【问题讨论】:
标签: binary-tree binary-search-tree rank