搜索RB树中的特定节点
seach for a specific node in an RB-tree
假设我有一个RB树,由与人们年龄相对应的数字组成;并且假设每个节点也有一个性别(女性或男性)。
我的问题是,如何使用OS-SELECT和秩值从树中获得特定的数字?具体数字意味着,在树上找到第二年轻的人。
示例:OS-SELECT(root,2)返回树中第二年轻的人。
目的不仅仅是找到第二个或第三个yougest节点,目的是找到第二小的男性或第二小的女性
只需按顺序遍历树并计算满足谓词的元素。在这种情况下,谓语应该是"is male"。在二进制搜索树中找到一个元素可以让我们提前结束遍历,这对实现来说不一定是微不足道的,所以这里有一个简单的算法伪代码:
# return value is used to track how many matching nodes must be
# found until the k'th one is reached
int OS-SELECT(node, rank)
if not node # leaf reached?
return rank # Keep searching.
rank = OS-SELECT(node.left, rank)
if not rank # Node was found in left subtree.
return 0 # End early.
if predicate(node) # Test the predicate.
if not --rank # The node matches: There are less matches to go through.
visit(node) # Rank dropped to 0: Found it. Visit the node and
return 0 # end early.
return OS-SELECT(node.right, rank)
相关文章:
- 反向给定链表中的K节点
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- Boost Graph Library,修复节点大小
- C++A*算法并不总是在路径中具有目标节点
- 如何找到2个单链表的公共节点
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 为什么我的删除节点函数实际上没有删除节点?
- 我们可以删除链表中静态内存中的节点吗
- 如何在pugixml中获取节点的内部XML
- 为什么我们要为avl树实现返回一个指向节点的指针,而不是void函数
- C++RapidXml-使用first_node()遍历以修改XML文件中节点的值
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- 节点 *temp; 和节点 *tmp = 新节点之间的差异
- 引擎节点:未定义的符号:_ZTV6Config
- 为什么"delete"关键字不删除节点?
- 如何使用发送数据包所花费的时间计算两个节点之间的距离?
- 如何按数字顺序插入链表节点?
- 为什么我的双向链表删除函数会删除多个节点?
- 搜索RB树中的特定节点