Skip to content

Instantly share code, notes, and snippets.

@syedjafer
Created May 2, 2021 08:49
Show Gist options
  • Select an option

  • Save syedjafer/7c0a08eee56768a64fcf5f4548924666 to your computer and use it in GitHub Desktop.

Select an option

Save syedjafer/7c0a08eee56768a64fcf5f4548924666 to your computer and use it in GitHub Desktop.
class Node{
constructor(data){
this.data = data;
this.left;
this.righ;
}
insert(data){
if (this.data != null){
if (data < this.data){
if (this.left == null){
this.left = new Node(data);
}else{
console.log(this.left);
this.left.insert(data);
}
}
else if(data > this.data){
if(this.right == null){
this.right = new Node(data);
}else{
this.right.insert(data);
}
}
}else{
this.data = new Node(data);
}
}
print_tree(){
if (this.left){
this.left.print_tree();
}
console.log(this.data+" ,");
if (this.right){
this.right.print_tree();
}
}
}
root = new Node(27)
root.insert(14)
root.insert(35)
root.insert(31)
root.insert(10)
root.insert(19)
root.print_tree()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment