title: get root of a binary search tree in java
body: <p>I have created a binary search tree in java that allow user to add nodes to the tree</p>
  
  <p>This is my implementation of the binary tree in java which accept root node on creation and then automatically figure out that it should add the child into left side or right side of the tree. </p>
  
  <pre><code>public class BinarySearchTree {
  
      Node root = null;
      public BinarySearchTree(Node root){
          this.root =root;
      }
      public void add(int data){
          Node newNode = new Node(data);
          if(this.root ==null){
              newNode =this.root;
          }
          if(data&gt;this.root.data){
              addRight(root,newNode);
          }
  
          if(data&lt;this.root.data){
              addLeft(root,newNode);
          }
      }
  
      public Node getRoot(){
         return  this.root;
      }
  
      private void addLeft(Node root, Node newNode) {
          if(root.leftChild == null){
              root.leftChild = newNode;
          }
          else {
              this.root = this.root.leftChild;
              add(newNode.data);
          }
      }
  
      private void addRight(Node root,Node newNode) {
          if (root.rightChild == null){
              root.rightChild = newNode;
          }
          else {
              this.root = this.root.rightChild;
              add(newNode.data);
          }
      }
  
  }
  </code></pre>
  
  <p>But when I try to retrieve the root node with <code>getRoot()</code> method. it return me the child of the root rather than the actual root node that I had passed in.</p>
  
  <p>this is a example of using it </p>
  
  <pre><code>TreeHight treeHight = new TreeHight();
          Node root = new Node(100);
          BinarySearchTree unbalance = new BinarySearchTree(root);
          unbalance.add(200);
          unbalance.add(50);
          unbalance.add(250);
          unbalance.add(350);
  </code></pre>
  
  <p>when I try to get root node it give me <code>250</code> as the first node rather than <code>100</code>.</p>
  
  <p>How can I retrieve the root node of this tree ?</p>