Created
March 21, 2020 07:49
-
-
Save voidnerd/44d48c69ae6706473d003965ff18984f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node { | |
constructor(data) { | |
this.left; | |
this.right; | |
this.data = data; | |
} | |
insert(value) { | |
if(value < this.data) { | |
if(this.left == null) { | |
this.left = new Node(value); | |
}else { | |
this.left.insert(value) | |
} | |
}else { | |
if(this.right == null) { | |
this.right = new Node(value) | |
}else{ | |
this.right.insert(value) | |
} | |
} | |
} | |
contains(value) { | |
if(value == this.data) { | |
return true | |
}else if(value < this.data) { | |
if(this.left == null) { | |
return false | |
}else { | |
return this.left.contains(value); | |
} | |
}else { | |
if(this.right == null) { | |
return false | |
}else { | |
return this.right.contains(value); | |
} | |
} | |
} | |
printInOrder() { | |
if(this.left != null) { | |
this.left.printInOrder() | |
} | |
console.log(this.data) | |
if(this.right != null) { | |
this.right.printInOrder(); | |
} | |
} | |
} | |
let bst = new Node(3) | |
bst.insert(8) | |
// bst.insert(5); | |
// bst.insert(19); | |
console.log("contains:", bst.contains(8)); | |
bst.remove(8) | |
console.log("contains:", bst.contains(8)); | |
console.debug(bst); | |
// console.log("---------") | |
// console.log(bst.printInOrder()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment