-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinary-tree_hackerearth.py
65 lines (57 loc) · 1.72 KB
/
binary-tree_hackerearth.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
62
63
64
65
#!/usr/bin/python3
# https://www.hackerearth.com/practice/data-structures/trees/binary-and-nary-trees/tutorial/
class Node:
def __init__(self, data, left=None, right=None):
self.data = data
self.left = left
self.right = right
def insert(self, value, path=''):
for c in path:
pass
if value <= self.data:
if self.left is None:
self.left = Node(value)
else:
self.left.insert(value)
else:
if self.right is None:
self.right = Node(value)
else:
self.right.insert(value)
# def contains(self, value):
# if value == self.data:
# return True
# elif value <= self.data:
# if self.left is None:
# return False
# else:
# return self.left.contains(value)
# else:
# if self.right is None:
# return False
# else:
# return self.right.contains(value)
def print_in_order(self):
if self.left is not None:
self.left.print_in_order()
print(self.data)
if self.right is not None:
self.right.print_in_order()
if __name__ == '__main__':
T, X = [int(x) for x in input().split()]
root = Node(X)
i = (T - 1) * 2
while i > 0:
path = input()
value = int(input())
print('Output: ', path, value)
i -= 2
# root = Node(10)
# root.insert(5)
# root.insert(15)
# root.insert(8)
# root.insert(17)
# root.insert(12)
# print('Is 8 exist: ', root.contains(8))
# print('Is 12 exist: ', root.contains(12))
# root.print_in_order()