Kth Smallest element of a Binary Search Tree

Given a Binary Search Tree, write a program to print the kth smallest element

2 Comments

  1. techie007 says:
    /** = 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

Leave a Reply