Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Implement A Binary Search Tree (Main Thread)

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:

  1. The left sub-tree of a node has a value less than or equal to its parent node's value.
  2. The right sub-tree of a node has a value greater than to its parent node's value.

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
  }
}

Constraints

  • Maximum number of nodes <= 1000
  • Value of each node can lie between -1000000000 and 1000000000
  • Expected time complexity for most BST operations (including search, insert, and delete) is O(log n) where n being the number of nodes
  • Expected space complexity : O(n)

You can see the full challenge with visuals at this link.

Challenges • Asked over 4 years ago by Anonymous

Jake from AlgoDaily Commented on Aug 02, 2020:

This is the main discussion thread generated for Implement A Binary Search Tree.

Anonymous Commented on Aug 02, 2020:

[deleted]

Jake from AlgoDaily Commented on Aug 02, 2020:

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.

Anonymous Commented on Aug 02, 2020:

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.

Jake from AlgoDaily Commented on Aug 02, 2020:

Fixed! Sorry, there was a regression in the JS engine. We've added a nice unit test to make sure this never happens again :-)