-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBSTOPS.py
61 lines (54 loc) · 1.63 KB
/
BSTOPS.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# I have been stuck on the problem for quite a while and people in forums are saying to skip
# So I had to copy and edit an AC solution to see why is mine not working
from sys import stdin, stdout
class Node:
def __init__(elf, data, pos):
elf.key = data
elf.pos = pos
elf.right = None
elf.left = None
def insert(root, key, pos):
if root == None:
root = Node(key, pos)
stdout.write(f'{pos}\n')
elif key < root.key:
root.left = insert(root.left, key, pos=(2 * pos))
else:
root.right = insert(root.right, key, pos=(2 * pos + 1))
return root
def minValueNode(node):
current = node
while current.left is not None:
current = current.left
return current
def delete(root, key, pr=True):
if root is None:
return
elif key < root.key:
root.left = delete(root.left, key, pr)
elif key > root.key:
root.right = delete(root.right, key, pr)
else:
if pr:
stdout.write(f'{root.pos}\n')
if root.left is None and root.right is None:
root = None
elif root.left is None:
root = root.right
elif root.right is None:
root = root.left
else:
temp = minValueNode(root.right)
root.key = temp.key
root.right = delete(root.right, temp.key, pr=False)
return root
root = None
try:
for _ in range(int(stdin.readline())):
op, x = stdin.readline().split()
if op == 'i':
root = insert(root, int(x), 1)
else:
root = delete(root, int(x))
except:
pass