-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxelement.py
50 lines (40 loc) · 1.05 KB
/
maxelement.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
#! /usr/bin/env python3
'''
Challenge goals:
Start with empty sequence and given N queries:
1) Push the element x into the stack.
2) Delete the element present at the top of the stack.
3) Print the maximum element in the stack.
'''
# from rb_tree2 import RedBlackTree
from ds.balbintree import BinaryTree
def getMax(ops):
stack = []
tree = BinaryTree()
results = []
for op in ops:
choice, *arg = op.split()
if choice == '1':
val = int(arg[0])
stack.append(val)
tree.insert(val)
elif choice == '2':
val = stack.pop()
tree.remove(val)
elif choice == '3':
results.append(tree.max)
else:
raise ValueError(f'Unexpected selection: {choice}')
# Debug
'''
print(f'Stack: {stack}')
print(f'Tree: {tree}')
print('='*50)
'''
return results
def main():
ops = [input() for _ in range(int(input()))]
results = getMax(ops)
print(results)
if __name__ == '__main__':
main()