/** = rank k node of tree t. k < 0 or k > size of tree, throw exception */
public BSTNodeSize findKth (int k, BSTNodeSize t) {
if (t == null)
throw new IllegalArgumentException();
int lsize= 0; // size of left subtree
if (t.left != null)
lsize= ((BSTNodeSize)(t.left)).size;
if (k = lsize + 1)
return t;
if (k < = lsize)
return findKth(k, (BSTNodeSize)t.left);
return findKth(k – lsize – 1, (BSTNodeSize)t.right);
}
/** = rank k node of tree t. k < 0 or k > size of tree, throw exception */ public BSTNodeSize findKth (int k, BSTNodeSize t) { if (t == null) throw new IllegalArgumentException(); int lsize= 0; // size of left subtree if (t.left != null) lsize= ((BSTNodeSize)(t.left)).size; if (k = lsize + 1) return t; if (k < = lsize) return findKth(k, (BSTNodeSize)t.left); return findKth(k – lsize – 1, (BSTNodeSize)t.right); }from
Last Page of
http://www.cs.cornell.edu/courses/cs211/2004sp/handout/lecture19moretrees.pdf
http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi?board=riddles_cs;action=display;num=1238159942