title: inserting an element to a binary search tree and find its level
body: <p>I have written such the code below that ;</p>
  
  <p>I have an array list which has <code>3</code> objects . each object has <code>2 fields 1)digit2)level</code></p>
  
  <p>I want to insert each object to a binary search tree and after inserting each element to the tree ,I need to find its level.</p>
  
  <p>Code:</p>
  
  <pre><code>private void greedyMethod(ArrayList&lt;Element&gt; array) {
      //it will print the objects in the array list.
      System.out.println(array.toString());
  
      array.get(0).setLevel(1);
      root = new DNode(array.get(0), null, null);
      for (int i = 1; i &lt; array.size(); i++) {
          insert(array.get(i));
      }
  }
  
  private void insert(Element e) {
      root = makeBST(root, e);
  }
  
  private DNode makeBST(DNode root, Element e) {
      if (root == null) {
          root = new DNode(e, null, null);
          // This will print wrong objects.
          System.out.println(root.getElement().toString());
      } else if (e.getDigit() &lt; root.getElement().getDigit()) {
          e.setDigit(root.getElement().getLevel() + 1);
          root.prev = makeBST(root.prev, e);
      } else if (e.getDigit() &gt; root.getElement().getDigit()) {
          e.setLevel(root.getElement().getLevel() + 1);
          root.next = makeBST(root.next, e);
      }
  
      System.out.println(averageAccessTime);
      return root;
  }
  </code></pre>
  
  <p>my array list is :</p>
  
  <pre><code>[digit :25   level:0, digit :27   level:0, digit :26   level:0]
  </code></pre>
  
  <p>but as I mentioned in the code above,the line :
      <code>System.out.println(root.getElement().toString());</code> prints wrong objects (I mean wrong digit or wrong level!!!!)--></p>
  
  <pre><code>digit :27  probability: 0.2 level:2
  digit :3  probability: 0.1 level:2
  </code></pre>
  
  <p>please help me to find my mistake ,thanks</p>