-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmin-height-bst.py
104 lines (93 loc) · 2.77 KB
/
min-height-bst.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Min Height BST
# 🟠 Medium
#
# https://www.algoexpert.io/questions/min-height-bst
#
# Tags: Binary Search Trees
import timeit
from utils.binary_tree import BinaryTree
# Use a helper function to avoid array slicing. Pick the middle element
# of the array section that we are currently evaluating as the root of
# the tree that we are constructing then use the section to the left to
# construct its left sub-tree and the section to the right to construct
# its right subtree.
#
# Time complexity: O(n) - We assess each node once.
# Space complexity: O(log(n)) - The height of the call stack.
class Solution:
def minHeightBst(self, array):
def insert(l, r):
if l == r:
return BST(array[l])
else:
m = (l + r) // 2
tree = BST(array[m])
if l < m:
tree.left = insert(l, m - 1)
if m < r:
tree.right = insert(m + 1, r)
return tree
return insert(0, len(array) - 1)
# Updated the original tree to have a val attribute instead of a val
# attribute.
class BST:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def insert(self, val):
if val < self.val:
if self.left is None:
self.left = BST(val)
else:
self.left.insert(val)
else:
if self.right is None:
self.right = BST(val)
else:
self.right.insert(val)
def test():
executors = [Solution]
tests = [
[[1], [1]],
[[1, 2], [1, None, 2]],
[[1, 2, 3], [2, 1, 3]],
[
[1, 2, 5, 7, 10, 13, 14, 15, 22],
[
10,
2,
14,
1,
5,
13,
15,
None,
None,
None,
7,
None,
None,
None,
22,
],
],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(1):
for col, t in enumerate(tests):
sol = executor()
root = sol.minHeightBst(t[0])
result = BinaryTree(root).toList()
exp = t[1]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for"
+ f" test {col} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()