Created
August 8, 2014 00:41
-
-
Save jyhjuzi/6a266059a265c467eef7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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