-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem_D.py
35 lines (31 loc) · 832 Bytes
/
problem_D.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import sys
sys.setrecursionlimit(100000)
def add(tree, param):
while tree and tree[0] != param:
tree = tree[1] if param < tree[0] else tree[2]
if not tree:
tree += [param, [], []]
return True
return False
def search(tree, param):
while tree and tree[0] != param:
tree = tree[1] if param < tree[0] else tree[2]
return bool(tree)
def print_tree(tree, depth):
if tree:
print_tree(tree[1], depth + 1)
print('.' * depth + str(tree[0]))
print_tree(tree[2], depth + 1)
with open('input.txt', 'r') as fin:
tree = []
for line in fin:
line = line.strip()
if line == 'PRINTTREE':
print_tree(tree, 0)
else:
comm, param = line.split()
param = int(param)
if comm == 'ADD':
print('DONE' if add(tree, param) else 'ALREADY')
elif comm == 'SEARCH':
print('YES' if search(tree, param) else 'NO')