#K41597. Binary Tree Operations
Binary Tree Operations
Binary Tree Operations
You are given a series of operations to perform on a binary search tree (BST). The BST supports the following operations:
- add x: Insert the integer x into the BST. If x already exists in the tree, ignore this command.
- delete x: Remove the integer x from the BST. If x does not exist, nothing happens.
- find x: Check whether the integer x exists in the BST. Print Exist if it is present, and Not Exist otherwise.
The operations will be given in order and you must process them sequentially. Only the find operations produce an output.
Note that a binary search tree is defined by the property that for any node with value $v$, all values in its left subtree are less than $v$ and all values in its right subtree are greater than $v$. This can be expressed as:
$$\text{if } node \neq null: \quad \forall x \in \text{left subtree},\; x node.value.$$
inputFormat
The first line of input contains a single integer N, the number of operations. Each of the following N lines contains one operation in one of the following forms:
• add x • delete x • find x
Here, x is an integer. Read input from standard input (stdin).
outputFormat
For each "find" operation, output a line with either "Exist" or "Not Exist" depending on whether x is present in the BST. Output the results to standard output (stdout).## sample
6
add 10
add 5
add 15
find 10
delete 10
find 10
Exist
Not Exist
</p>