计算二叉搜索树中节点的等级

问题描述 投票:4回答:3

如果二叉搜索树中的每个节点都存储其权重(其子树中的节点数),那么当我在树中搜索它时,计算给定节点的排名(其在排序列表中的索引)的有效方法是什么? ?

algorithm tree binary-search-tree
3个回答
12
投票

从零开始排名。当二进制搜索从根向下进行时,添加搜索跳过的所有左子树的大小,包括找到的节点的左子树。

即,当搜索离开时(从父项到左项),它发现没有比搜索项更少的新值,因此排名保持不变。当它向右时,父节点加上左子树中的所有节点都小于搜索的项目,因此添加一个加上左子树大小。当它找到搜索到的项目时。包含该项的节点的左子树中的任何项都小于它,因此将其添加到排名中。

把这一切放在一起:

int rank_of(NODE *tree, int val) {
  int rank = 0;
  while (tree) {
    if (val < tree->val) // move to left subtree
      tree = tree->left;
    else if (val > tree->val) {
      rank += 1 + size(tree->left);
      tree = tree->right;
    }
    else 
      return rank + size(tree->left);
  }
  return NOT_FOUND; // not found
}

这将返回从零开始的排名。如果您需要从1开始,则将rank初始化为1而不是0。


2
投票

由于每个节点都有一个存储其权重的字段,因此首先应该实现一个方法调用size(),它返回节点的子节点中的节点数:

private int size(Node x)
{
if (x == null) return 0;
else return x.N;
} 

然后计算给定节点的等级很容易

public int rank(Node key)
{ return rank(key,root) }

    private int rank(Node key,Node root)
    {
        if root == null 
             return 0;
        int cmp = key.compareTo(root);
// key are smaller than root, then the rank in the whole tree
// is equal to the rank in the left subtree of the root.
        if (cmp < 0) {
            return rank(key, root.left) 
        } 
//key are bigger than root,the the rank in the whole tree is equal
// to the size of subtree of the root plus 1 (the root) plus the rank 
//in the right sub tree of the root.
        else if(cmp > 0){
            return size(root.left) + 1 + rank(key,root.right); 
        } 
// key equals to the root, the rank is the size of left subtree of the root
        else return size( root.left);  
    }

0
投票

取决于BST的实现,但我相信你可以递归地解决它。

public int rank(Key key){
    return rank(root, key);
}

private int rank(Node n, Key key){
    int count = 0;
    if (n == null)return 0;
    if (key.compareTo(n.key) > 0) count++;
    return count + rank(n.left, key) + rank(n.right, key);
}
© www.soinside.com 2019 - 2024. All rights reserved.