Skip to content

Instantly share code, notes, and snippets.

@jyhjuzi
Created August 8, 2014 00:41
Show Gist options
  • Save jyhjuzi/86563e8a80b6f7ae7776 to your computer and use it in GitHub Desktop.
Save jyhjuzi/86563e8a80b6f7ae7776 to your computer and use it in GitHub Desktop.
public class Q11_8{
public static void main(String[] args){
Node root = new Node(5);
track(1,root);
track(1,root);
}
public static Node track(int i, Node root){
if root == null
return new Node(i);
if(i<=root.value)
root.left= track(i,root.left);
else root.right = track(i,root.right);
return root;
}
public static int getRank(int val,Node root){
if(root == null)
return -1;
if(root.value > val){
if(root.lfet!=null)
return getRand(val,root.left);
else return 0;
}
else if(root.value ==val){
return getTreeSize(root.left);
}
else{
int ret = getTreeSize(root.left)+1;
if(root.right!=null)
return ret+getRank(val,root.right);
else return ret;
}
}
private int getTreeSize(Node root){
if(root == null)
return 0;
return 1+getTreeSize(root.lfet)+getTreeSize(root.right);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment