【发布时间】:2015-09-11 09:24:02
【问题描述】:
找到一个字符串:X,它可能存在也可能不存在于 AVL 树中。如果 X 存在或找到 X 之后的下一个最大字符串,我可以使用伪代码获取 X 吗?
我已经完成了后继者的代码。继任者找到下一个最大的节点。
protected BSTVertex successor(BSTVertex T)
{
if (T.right != null) // this subtree has right subtree
return findMin2(T.right); // the successor is the minimum of right subtree
else {
BSTVertex par = T.parent;
BSTVertex cur = T;
// if par(ent) is not root and cur(rent) is its right children
while ((par != null) && (cur == par.right)) {
cur = par; // continue moving up
par = cur.parent;
}
return par == null ? null : par; // this is the successor of T
}
}
例如,如果树由数字 1、2、3、4、7、9 组成。如果我想找到 6,它应该返回 7,因为 6 不存在,下一个最大值是 7
【问题讨论】:
-
x是什么,你卡在哪里了? -
嗨,我的意思是给定一个字符串,在 avl 树中找到该字符串,或者如果找不到该字符串,则查找下一个最大的最接近匹配项