5

I need to implement two rank queries [rank(k) and select(r)]. But before I can start on this, I need to figure out how the two functions work.

As far as I know, rank(k) returns the rank of a given key k, and select(r) returns the key of a given rank r.

So my questions are:

1.) How do you calculate the rank of a node in an AVL(self balancing BST)?

2.) Is it possible for more than one key to have the same rank? And if so, what woulud select(r) return?

I'm going to include a sample AVL tree which you can refer to if it helps answer the question.

enter image description here

Thanks!

tvguide1234
  • 197
  • 1
  • 3
  • 8

3 Answers3

3

Your question really boils down to: "how is the term 'rank' normally defined with respect to an AVL tree?" (and, possibly, how is 'select' normally defined as well).

At least as I've seen the term used, "rank" means the position among the nodes in the tree -- i.e., how many nodes are to its left. You're typically given a pointer to a node (or perhaps a key value) and you need to count the number of nodes to its left.

"Select" is basically the opposite -- you're given a particular rank, and need to retrieve a pointer to the specified node (or the key for that node).

Two notes: First, since neither of these modifies the tree at all, it makes no real difference what form of balancing is used (e.g., AVL vs. red/black); for that matter a tree with no balancing at all is equivalent as well. Second, if you need to do this frequently, you can improve speed considerably by adding an extra field to each node recording how many nodes are to its left.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Yes, this is correct. When you say "count the number of nodes to its left" do you mean the left subtree? For example would rank(10) then be 0? Also, I was told that I could use a size parameter to optimize code which might be related to what you're saying about improving the speed. Is there any link you can recommend where I can find out how to go about implementing this? Thanks again. – tvguide1234 Feb 28 '11 at 03:28
  • 1
    IIRC, it's normally all the nodes to the left in the tree as a whole, so rank(10) would be 5 or 6 (depending on whether you start from 0 or 1). Offhand, I don't know of any good links on the subject, but IIRC _Introduction to Algorithms_ by Cormen, Leiserson, Rivest and Stein has a section on it. – Jerry Coffin Feb 28 '11 at 03:31
  • Ah ok. Just so I'm clear on this; would you say that rank(13) would be 7 or 8 then? – tvguide1234 Feb 28 '11 at 03:46
  • I am guessing the main problem of homework is how to do the balancing of tree so as to be able to maintain the counts. For local rotations, this is always possible (so even works with Red-Black trees etc). I beleive the name for this structure is Order Statistics Tree. +1, though. –  Feb 28 '11 at 03:48
  • @Jerry Coffin: I see. Ok so, from what I can tell, every node will have a unique rank. Thus when we run `select(r)` it will always return the one node that has that specific rank. Is this true? @Moron: Thanks, I will look into this to see if it'll help with my problem. – tvguide1234 Feb 28 '11 at 04:26
  • @tvguide1234: yes, that sounds right -- with the proviso that it would be much better to look things up than depend on my memory. – Jerry Coffin Feb 28 '11 at 04:31
  • @Jerry Coffin:Heh, yea I'm going to try and catch my professor when he's in his office tomorrow but I wanted to figure out as much as possible before going in since he's usually busy. I'll report back on what he says tomorrow night. Anyways, thanks a bunch man! – tvguide1234 Feb 28 '11 at 06:10
  • @JerryCoffin Rank has to be calculated based on the pre-order traversal. Am i right? rank(1) is 1 and rank(3) is 2. Am i right? – VINOTH ENERGETIC Apr 12 '16 at 17:06
1

Rank is the number of nodes in the Left sub tree plus one, and is calculated for every node. I believe rank is not a concept specific to AVL trees - it can be calculated for any binary tree.

Select is just opposite to rank. A rank is given and you have to return a node matching that rank.

The following code will perform rank calculation:

void InitRank(struct TreeNode *Node)
{
        if(!Node)
        {
                return;
        }
        else
        {       Node->rank = 1 + NumeberofNodeInTree(Node->LChild);
                InitRank(Node->LChild);
                InitRank(Node->RChild);
        }

}


int NumeberofNodeInTree(struct TreeNode *Node)
{
        if(!Node)
        {
                return 0;
        }
        else
        {
                  return(1+NumeberofNodeInTree(Node->LChild)+NumeberofNodeInTree(Node->RChild));
        }
}
jam
  • 3,640
  • 5
  • 34
  • 50
0

Here is the code i wrote and worked fine for AVL Tree to get the rank of a particular value. difference is just you used a node as parameter and i used a key a parameter. you can modify this as your own way. Sample code:

    public int rank(int data){
    return rank(data,root);
}

private int rank(int data, AVLNode r){
    int rank=1;
    while(r != null){
        if(data<r.data)
            r = r.left;
        else if(data > r.data){
            rank += 1+ countNodes(r.left);
            r = r.right;
        }
        else{
            r.rank=rank+countNodes(r.left);
            return r.rank;
        }
    }
    return 0;
}

[N.B] If you want to start your rank from 0 then initialize variable rank=0. you definitely should have implemented the method countNodes() to execute this code.