let BST = function(value,name){ this.value = value this.name = name this.children = [ ] } BST.prototype.insert = function(value,name){ if(!this.children){ this.children = [] } if(value < this.value){ if(!this.children[0]){ this.children[0] = new BST(value,name) }else if(this.children[0].value == null){ this.children[0] = new BST(value,name) }else{ this.children[0].insert(value,name) } }else if(value > this.value){ if(!this.children[1]){ if(this.children.length == 0){ this.children[0] = new BST(null) } this.children[1] = new BST(value,name) }else{ this.children[1].insert(value,name) } } } BST.prototype.depthFirstTraversal = function(order, length = 1,queue = []){ if(!this.children){ this.children = [] } if(order == 'pre-order'){ queue.push(this.id) length-- } if(this.children[0] && this.children[0].value != null){ length++ this.children[0].depthFirstTraversal(order,length,queue) } if(order === 'in-order'){ queue.push(this.id) length-- } if(this.children[1]){ length++ this.children[1].depthFirstTraversal(order,length,queue) } if(order == 'post-order'){ queue.push(this.id) length-- } return queue }