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 reachingNULL.
Input
- Line 1: integer
n; line 2:nintegers (inserted in this order, may be 0). - Line 3: integer
q(number of queries); line 4:qintegers to look up.
Output
qlines; for each query printfoundornot 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