class Node{
constructor(val){
this.val=val
this.right=null
this.left=null
}
}
class BinaryTree{
constructor(){
this.length=0
this.root=null
}
insert(value){
const newNode=new Node(value)
if(!this.root){
this.root=newNode
return this
}
// infinite loop
let curr=this.root
while(true){
if(newNode.val<curr.val){
if(!curr.left){
curr.left=newNode
return this
}else{
curr= curr.left
}
}else{
if(!curr.right){
curr.right=newNode
return this
}else{
curr=curr.right
}
}
}
}
// iterative bds with also return the maximum depth
bfsI(){
let queue=[this.root],list=[],level=0
while(queue.length>0){
const tempLength=queue.length
for(let i=0;i<tempLength;i++){
const curr=queue.shift()
if(curr.left){
queue.push(curr.left)
}
if(curr.right){
queue.push(curr.right)
}
list.push(curr.val)
}
level++
}
return [{max_depth:list},{bfs:list}]
}
bfsR(queue,list){
if(queue.length===0){
return list
}
let curr=queue.shift()
list.push(curr.val)
if(curr.left){
queue.push(curr.left)
}
if(curr.right){
queue.push(curr.right)
}
return this.bfsR(queue,list)
}
}
const myBinarySearch = new BinaryTree();
myBinarySearch.insert(9);
myBinarySearch.insert(20);
myBinarySearch.insert(4);
myBinarySearch.insert(1);
myBinarySearch.insert(-1);
myBinarySearch.insert(15);
myBinarySearch.insert(6);
myBinarySearch.insert(170);
myBinarySearch.insert(180);
myBinarySearch.insert(2000);
console.log(myBinarySearch.bfsR([myBinarySearch.root],[]))