-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBST_Traversal_Trees.py
57 lines (54 loc) · 1.39 KB
/
BST_Traversal_Trees.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
class BST:
def __init__(self,data):
self.data=data
self.left=None
self.right=None
def insert(root,data):
if root is None:
root=BST(data)
else:
if(data<root.data):
if root.left is None:
root.left=BST(data)
else:
insert(root.left,data)
else:
if root.right is None:
root.right=BST(data)
else:
insert(root.right,data)
return root
#Time - O(N) / Space - O(N) for all traversals
def inorder(root,array):
if root is None:
return None
inorder(root.left,array)
array.append(root.data)
inorder(root.right,array)
return array
def preorder(root,array):
if root is None:
return None
array.append(root.data)
preorder(root.left,array)
preorder(root.right,array)
return array
def postorder(root,array):
if root is None:
return None
postorder(root.left,array)
postorder(root.right,array)
array.append(root.data)
return array
N=int(input())
arr=[int(x) for x in input().split()]
root=None
for i in arr:
root=insert(root,i)
print(root.data)
inorderValues=inorder(root,[])
preorderValues=preorder(root,[])
postorderValues=postorder(root,[])
print(*inorderValues)
print(*preorderValues)
print(*postorderValues)