AniUI Academy
medium+250 XPPractice

A binary search tree that stays sorted

A binary search tree keeps every value smaller than a node in its left subtree and every larger value in its right, so an in-order walk emerges sorted and a lookup only ever follows one path down.

Implement a BST class holding numbers. It is constructed with no arguments and starts empty. insert(value) adds a number and returns true, or returns false and changes nothing if that number is already in the tree. has(value) returns true or false. inOrder() returns an array of the stored numbers in ascending order, and [] when empty. size() returns how many distinct numbers are stored. min() and max() return the smallest and largest stored numbers, or undefined when the tree is empty.

What it has to do

  • inOrder() returns the values in ascending order.
  • insert returns false for a duplicate and does not add it twice.
  • has returns false for a value that was never inserted.
  • min() and max() return undefined on an empty tree.
  • size() counts distinct values only.

Your workspace

Try it yourself
Loading playground...

Ready to check it?

5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.

AI Crack & Solution Assist

Stuck? Get instant AI hints or break down the optimal solution.

Stuck? The javascript course covers everything this challenge needs.