KatInterview

Sort a List and Replay Commands on a Binary Search Tree

Trees & GraphsAuto-graded · 25 test cases
MediumAuto-gradedTrees & Graphsbinary-search-treequicksortrecursion

Last updated

Sort an integer list without built-in sorting, then run a script of insert, search, delete and in-order commands on a duplicate-free binary search tree built from scratch.

Write a single function that does two independent jobs.

Job 1: sorting. Given a list of integers values, produce a new list containing the same numbers in non-decreasing order. Implement the sorting algorithm yourself (a quicksort variant is the natural choice). Do not use sorted, list.sort, or any library sorting routine. The input list must not be modified.

Job 2: tree commands. Start with an empty binary search tree of integer keys. Process the commands in order. Command i has the name operations[i] and its integer parameters are in arguments[i]. Implement the tree yourself, without any library tree or ordered-container type.

CommandParametersEffectRecorded result
"insert"[key]Add key to the tree. If key is already present, the tree is left unchanged (no duplicates are stored).None
"search"[key]Check whether key is in the tree.True or False
"delete"[key]Remove key if present. If it is absent, nothing happens.None
"inorder"[]Read the keys in left-subtree, node, right-subtree order.A new list of all keys in ascending order (an empty list if the tree is empty)

When deleting a node with two children, replace its key with the smallest key of its right subtree and then remove that smallest node from the right subtree. This choice does not change what search or inorder return, but it is the standard approach.

The tree is entirely separate from values: the values being sorted are not inserted into the tree.

Return value. A dictionary with two entries:

  • "sorted": the sorted copy of values
  • "results": a list with one recorded result per command, in the same order as the commands. It is empty when there are no commands.

operations and arguments always have the same length.

Constraints

  • 0 <= len(values) <= 10^5
  • 0 <= len(operations) == len(arguments) <= 10^4
  • All numbers are integers that fit in a 64-bit signed integer; values may contain duplicates and may already be sorted or reverse sorted.
  • Every command name is one of "insert", "search", "delete", "inorder".
  • arguments[i] has exactly one integer for insert, search and delete, and is empty for inorder.
  • Your sorting should stay efficient on sorted input and on inputs with many equal values.
  • Keep recursion in mind: a very lopsided tree or partition can be deep, so avoid unbounded stack growth where you can.

Examples

Example 1

Input: sort_and_run_bst_commands([3, 6, 8, 10, 1, 2, 1], [], [])

Output: {'sorted': [1, 1, 2, 3, 6, 8, 10], 'results': []}

Example 2

Input: sort_and_run_bst_commands([5, 3, 7, 2, 4, 6, 8], [], [])

Output: {'sorted': [2, 3, 4, 5, 6, 7, 8], 'results': []}

Example 3

Input: sort_and_run_bst_commands([4, 2, 6, 1, 3, 5, 7], [], [])

Output: {'sorted': [1, 2, 3, 4, 5, 6, 7], 'results': []}

Hints

Hint 1

For sorting, pick the middle element as the pivot and split the range into three zones: smaller than, equal to, and greater than the pivot. Equal elements are then never revisited, so many duplicates stay fast.

Hint 2

To keep the stack shallow, recurse into the smaller of the two outer zones and loop on the larger one.

Hint 3

For tree deletion handle three cases: no left child (return the right child), no right child (return the left child), and two children (copy the smallest key from the right subtree into this node, then delete that key from the right subtree).

Hint 4

Make insert and delete return the possibly new subtree root so the parent can reassign its child pointer; this also handles deleting the root of the whole tree.