title: Recursive binary tree insert
body: <p>When I added node into binary tree and trying to show information about it, info isn't showing. I think that i have some problems with references in recursive insert algorithm,  but can't fix it.</p>
  
  <pre><code>package test;
  
  class BinaryTree&lt;T&gt; {
      private static class Node&lt;T&gt; {
          int key;
          T data;
          Node&lt;T&gt; leftChild;
          Node&lt;T&gt; rightChild;
  
          public Node(int key,T data) {
              this.key = key;
              this.data = data;
          }
      }
  
      public Node&lt;T&gt; rootNode;
  
      public BinaryTree() {
          rootNode = null;
      }
  
      public Node&lt;T&gt; getRootNode() {
          return rootNode;
      }
  
      // insert node into binary tree
      public void insertNode(int key,T data, Node&lt;T&gt; rootNode) {
          // to create new node
  
          // if tree doesn't have root elements
          if(rootNode == null) {
              rootNode = new Node&lt;T&gt;(key,data);
              rootNode.leftChild = null;
              rootNode.rightChild = null;
          }
          else {
              Node&lt;T&gt; focusNode = rootNode;
  
              if(key &gt;= focusNode.key) {
                  insertNode(key,data,focusNode.rightChild);
              }
              else {
                  insertNode(key,data,focusNode.leftChild);
              }
          }
      }
  
      // inorder traverse tree
      public void inOrderTraverseTree(Node&lt;T&gt; focusNode) {
          if(focusNode != null) {
              inOrderTraverseTree(focusNode.leftChild);
              System.out.println(focusNode.data);
              inOrderTraverseTree(focusNode.rightChild);
          }
      }
  }
  
  public class MyApp {
      public static void main(String[] args) {
          BinaryTree&lt;String&gt; bintree = new BinaryTree&lt;String&gt;();
          bintree.insertNode(3, "Boss", bintree.rootNode);
          bintree.inOrderTraverseTree(bintree.rootNode);
      }
  }
  </code></pre>
  
  <p>If I'm adding node with this algorithm and trying to show info, it works. How can i fix problem with recursive algorithm?</p>
  
  <pre><code>public void addNode(int key, T name) {
          Node&lt;T&gt; newNode = new Node&lt;T&gt;(key,name);
          if(rootNode == null) {
              rootNode = newNode;
          }
          else {
              Node&lt;T&gt; focusNode = rootNode;
              Node&lt;T&gt; parent;
              while(true) {
                  parent = focusNode;
                  if(key &lt; focusNode.key) {
                      focusNode = focusNode.leftChild;
                      if(focusNode == null) {
                          parent.leftChild = newNode;
                          return;
                      }
                  }
                  else {
                      focusNode = focusNode.rightChild;
                      if(focusNode == null) {
                          parent.rightChild = newNode;
                          return;
                      }
                  }
              }
          }
      }
  </code></pre>
  
  <p>Thanks for any help.</p>