generated from brightvarghese/22_ODD_DS_Exercise-10
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
72 lines (61 loc) · 1.84 KB
/
Main.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
66
67
68
69
70
71
72
class BinaryTreeNode:
def __init__(self, data):
self.data = data
self.left_child = None
self.right_child = None
def insert(root, new_value) -> BinaryTreeNode:
"""If binary search tree is empty, make a new node, declare it as root and return the root.
If tree is not empty and if new_value is less than value of data in root, add it to left subtree and proceed recursively.
If tree is not empty and if new_value is >= value of data in root, add it to right subtree and proceed recursively.
Finally, return the root.
"""
# Write your code here
if not root:
root = BinaryTreeNode(new_value)
return root
if new_value < root.data:
if root.left_child:
insert(root.left_child, new_value)
else:
root.left_child = BinaryTreeNode(new_value)
else:
if root.right_child:
insert(root.right_child, new_value)
else:
root.right_child = BinaryTreeNode(new_value)
def inorder(root) -> None:
# Write your code here
if root is None:
return
inorder(root.left_child)
print(root.data, end = " ")
inorder(root.right_child)
def preorder(root) -> None:
# Write your code here
if root is None:
return
print(root.data, end = " ")
preorder(root.left_child)
preorder(root.right_child)
def postorder(root) -> None:
# Write your code here
if root is None:
return
postorder(root.left_child)
postorder(root.right_child)
print(root.data, end = " ")
# Do not change the following code
input_data = input()
flag = True
root = None
for item in input_data.split(', '):
if flag is True:
root = insert(None, int(item))
flag = False
else:
insert(root, int(item))
inorder(root)
print()
preorder(root)
print()
postorder(root)