title: Java program to find the kth smallest element in a BST
body: <p>I am writing a Java program to find the kth smallest element in a BST.</p>
  
  <p>I have gone through some other posts on this question on stack overflow and have gone through their solutions but I cannot understand what is the problem with my code.
  If anyone could please tell me why my program is not printing anything.</p>
  
  <pre><code>//Program to find the kth smallest element in the BST
  public class KthSmallestElement {
  static Node root;
  //method to insert a key
  Node insertRec(Node root, int key)
  {
      //if the tree is empty
      if(root == null)
      {
         root = new Node(key);
         return root;
      }
      //otherwise recur down the tree
      else
      {
          if(key &gt; root.key)
          {
              root.right = insertRec(root.right, key);
          }
          else
          {
              root.left = insertRec(root.left, key);
          }
          return root;
       }
  }
  
  //This method is for calling the insertRec() method
  Node insert(int key)
      {
          root = insertRec(root, key);
          return root;
      }
  
  //This method is for doing the inorder traversal of the tree
  void kthSmallest(Node root, int k)
  {
      int counter = 0;
      if(root == null)
          return;
     else
      {
               kthSmallest(root.left, k);
               counter++;
  
      if(counter == k)
          {
            System.out.println(root.key);
           }
          kthSmallest(root.right, k); 
      }
   }    
  
   //main method
  public static void main(String[] args)
  {
      KthSmallestElement tree = new KthSmallestElement();
      Node root = null;
      root = tree.insert(20);
      root =tree.insert(8);
      root =tree.insert(22);
      root =tree.insert(4);
      root= tree.insert(12);
      root =tree.insert(10);
      root =tree.insert(14);
      tree.kthSmallest(root, 3);
  }
  
  }
  </code></pre>
  
  <p>And my node class is as follows:</p>
  
  <pre><code>//Class containing left and right child of current node and key value
  public class Node {
  int key;
  Node left, right, parent;
  
  //Constructor for the node
  public Node(int item){
    key = item;
    left = right = parent= null;
  }
  
  }
  </code></pre>
  
  <p>It isn't printing anything.That's the problem.
  Ok I am not so good at programming so please pardon me for asking such a question here.</p>