Here is the interview question prompt, presented for reference.
Let's implement a Binary Search Tree
! Recall that a binary search tree
, or a BST
, is a binary tree
that is ordered via these two properties:
Given the following tree node definition:
class Node {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
Can you fill out the skeleton code and implement a binary search
tree with #add
and #remove
methods?
class BST {
constructor(val) {
this.root = new Node(val);
}
add(val) {
// fill this in
}
remove(val) {
// fill this in
}
}
1000
-1000000000
and 1000000000
O(log n)
where n
being the number of nodes O(n)
You can see the full challenge with visuals at this link.
Challenges • Asked over 4 years ago by Anonymous
This is the main discussion thread generated for Implement A Binary Search Tree.
[deleted]
There was an error on this one. We had included the Node
class definition as part of the initial code and solution code. However, this Node
class definition is already part of the environment every "trees" question executes in. We've seen removed the duplicate from the initial and solution code, and have tested this to work.
I'm still getting "SyntaxError: Invalid or unexpected token" when trying to run the tests. I've confirmed that my code does at least run and pass the simple commented-out use case in the starter code.
Fixed! Sorry, there was a regression in the JS engine. We've added a nice unit test to make sure this never happens again :-)