【发布时间】:2011-04-14 20:24:44
【问题描述】:
pair<K,V> *RedBlackTree<K,V,Compare>::successor(K key) {
Node *found = findNode(key, root);
Node *p;
Node *ch;
Node *x;
Node *y;
if(found->right != sentinel)
return new pair<K,V>(found->right->key, found->right->value);
y = found->parent;
/* if it does not have a left child,
predecessor is its first left ancestor */
while(y != NULL && found == y->right) {
found = y;
y = y->parent;
}
return new pair<K,V>(y->key, y->value);
}
【问题讨论】:
-
也许这种问题更适合codereview.stackexchange.com。
-
你在代码审查中可能会有更好的运气:codereview.stackexchange.com
-
神圣的动态分配,蝙蝠侠!返回指向动态分配的
pair的指针几乎肯定是错误的。您可能希望按值返回pair。这段代码看起来不对:found->right并不总是中序遍历中的下一个节点,因为found->right可能有一个左子树。我发布了一个如何找到继任者in an answer to another question的示例。
标签: c++ red-black-tree