forked from lupamo3-zz/coding-interview-gym
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BST_Traversal.py
61 lines (50 loc) · 1.78 KB
/
BST_Traversal.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
# https://www.algoexpert.io/questions/BST%20Traversal
class BST:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
# Average: O(log(n)) time | O(1) space
# Worst: O(n) time | O(1) space
def insert(self, value):
currentNode = self
while True:
if value < currentNode.value:
if currentNode.left is None:
currentNode.left = BST(value)
break
else:
currentNode = currentNode.left
else:
if currentNode.right is None:
currentNode.right = BST(value)
break
else:
currentNode = currentNode.right
return self
# O(n) time | O(n) space
def in_order_traverse(tree, array):
if tree is not None:
in_order_traverse(tree.left, array)
array.append(tree.value)
in_order_traverse(tree.right, array)
return array
# O(n) time | O(n) space
def pre_order_traverse(tree, array):
if tree is not None:
array.append(tree.value)
in_order_traverse(tree.left, array)
in_order_traverse(tree.right, array)
return array
# O(n) time | O(n) space
def post_order_traverse(tree, array):
if tree is not None:
in_order_traverse(tree.left, array)
in_order_traverse(tree.right, array)
array.append(tree.value)
return array
# driver/test code
test_tree = BST(100).insert(5).insert(15).insert(5).insert(2).insert(1).insert(22) \
.insert(1).insert(1).insert(3).insert(1).insert(1).insert(502).insert(55000) \
.insert(204).insert(205).insert(207).insert(206).insert(208).insert(203) \
.insert(-51).insert(-403).insert(1001).insert(57).insert(60).insert(4500)