在二叉搜索树中找到第n个节点

Find the nth Node in Binary Search Tree

本文关键字:节点 搜索树      更新时间:2023-10-16

使用这个bootstrap:

template <class Comparable>
const Comparable& AugmentedBinarySearchTree<Comparable>::NthElement(int n)
{
  int *i = 0;
  return NthElement(root, i, n)->element;
}

如何使用nodesVisited指针来跟踪检查的节点来找到BST中的第n个节点?

template <class Comparable>
BinaryNode<Comparable>* AugmentedBinarySearchTree<Comparable>::
NthElement(BinaryNode<Comparable> *t, int *nodesVisited, int n) const
{
}

BST中的每个节点都有一个指针leftright,以及一个名为element的值template <class Comparable>

听起来我(从你的评论)像你正在寻找Boost的多索引容器(http://www.boost.org/doc/libs/1_59_0/libs/multi_index/doc/index.html)。您将拥有矢量视图和地图视图,并在使用地图视图按键搜索时使用push_back插入矢量视图。然后,您将使用矢量视图来获取第n个插入的元素。