javascript - Build a balanced binary tree -


const tree = function(value){    this.value = value    this.right = null    this.left = null  }      tree.prototype.insert = function(number){    if(this.left === null){      this.left = new tree(number)    } else if(this.right === null){      this.right = new tree(number)    } else {      this.left.insert(number)    }  }    const arbre = new tree(10)    arbre.insert(5)  arbre.insert(2)  arbre.insert(1)  arbre.insert(20)  arbre.insert(6)    console.log(json.stringify(arbre,null,4))

i'm trying build balanced binary tree. means each time i'm inserting value fill left node first , right node, , on. that:

 0  / \  1  2  /\ /  34 5

my code keep building in left side doesn't go in right side maintain balance of tree. like:

      0       / \       1   2       /\        34      /      5


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -