0
第n个节点使用该引导程序:查找二叉搜索树
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每个节点都有一个指针left
和right
和称为element
的template <class Comparable>
的值。
二叉树中的“第N个节点”是什么意思?这不取决于二叉树是如何遍历的? – PaulMcKenzie
@PaulMcKenzie通过查找“第N个节点”,我的意思是插入树中的第n个值,这是非常明显的。 –
以任何您想要的顺序遍历树(可能按顺序)。为每个节点增加'* nodesVisited'。当'* nodesVisited == n'时停止。 –