If the rank of the BST is k, it implies how many nodes/keys are less than k.
So, it boils down to 3 easy recursive calls
So, it boils down to 3 easy recursive calls
- In the simplest case if K==node value, then whole of the let is rank of the node
- if K < node.value then we know that rank depends on the size of left sub tree and its less than sub tree's length
- If K > node.value then we know that we have to count full left subtree w.r.t with that node, and some nodes in right
public int rank(int K, node x) { if(x==null) return 0; if(K < x.data) return rank(K,x.left); else if(K > x.data) return 1 + size(x.left) + rank(K,x.right); else if(K == x.data) return size(x.left); }
0 comments:
Post a Comment