title: Simple binary search tree in Java
body: <p>Here there is code for a simple <code>BST</code> in Java that keys are only <code>int</code>, but when I want to test it and use <code>print()</code> just <code>root.key</code> can be printed and <code>root.right</code> and <code>root.left</code> are <code>null</code>.</p>
  
  <pre><code>public class BST {
  
      private class Node {
          private int key;
          private Node left, right;
  
          public Node(int key) {
              this.key = key;
          }
      }
  
      private Node root;
  
      public BST(int key) {
          root = new Node(key);
      }
  
      public void insert(int key) {
          insert(root, key);
      }
  
      private void insert(Node x, int key) {
          if (x == null) {
              x = new Node(key);
          }
          if (key &gt; x.key) {
              insert(x.right, key);
          } else if (key &lt; x.key) {
              insert(x.left, key);
          } else {
              x.key = key;
          }
      }
  
      public void print() {
          print(root);
      }
  
      private void print(Node x) {
          System.out.print(x.key + " ");
          if (x.left != null)
              print(x.left);
          if (x.right != null)
              print(x.right);
      }
  }
  </code></pre>
  
  <p>For example, when I insert 25, 9, 10, 30, 40 and call <code>print()</code> then it just prints 25.</p>