OlveraeUNSW
Back to problems
COMP2521

BST: Insert & Search

BST: Insert & Search

Implement the two core BST operations: insert and search. They follow the same path — start at the root and compare: go left if smaller, right if larger.

  • insert(t, v): on reaching an empty spot (NULL), create a node there; if equal, treat as already present (no duplicate).
  • search(t, v): return true on a match; return false on reaching NULL.

Input

  • Line 1: integer n; line 2: n integers (inserted in this order, may be 0).
  • Line 3: integer q (number of queries); line 4: q integers to look up.

Output

  • q lines; for each query print found or not found.

Example: input 5 / 50 30 70 20 40 / 3 / 30 99 20 →

found
not found
found

Samples

stdin
5
50 30 70 20 40
3
30 99 20
expected
found
not found
found
stdin
1
42
2
42 7
expected
found
not found
c-dcc
14