title: How to make binary tree from array in javascript?
body: <p>I have an array <code>var array = [8,10,12,5,3,6];</code></p>
  
  <p><strong>Logic</strong></p>
  
  <ol>
  <li>First node would be root node.</li>
  <li>If new node value is less or equal <code>=&lt;</code> than parent node, It would be left node of parent node</li>
  <li>If new node value is greater <code>&gt;</code> than parent node, It would be right node  of parent node</li>
  </ol>
  
  <p>And I am trying to achieve output like below object:</p>
  
  <pre><code>{
     value:8,
     left:{
        value:5,
        left:{ value:3 },
        right:{value:6}
     },
     right:{
        value:10,
        right:{value:12}
     }
  }
  </code></pre>
  
  <p>Which would be in image like this</p>
  
  <p><a href="https://i.stack.imgur.com/uf2ql.png" rel="noreferrer"><img src="https://i.stack.imgur.com/uf2ql.png" alt="enter image description here"></a> </p>
  
  <p>I tried below code:</p>
  
  <pre><code>var arr = [8,10,12,5,3,6];
  var root = arr[0];
  var rv = {};
  for (var i = 0; i &lt; arr.length; i++){
      if(arr[i] &lt; root){
      rv.left = arr[i];
    }else{
      rv.right = arr[i];
    }
  }
  console.log(rv);
  </code></pre>
  
  <p>Please help me to solve this.</p>