-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathp4_2.py
39 lines (29 loc) · 986 Bytes
/
p4_2.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
from typing import List
from utils.graphs import BiNode
from utils.treeviz import *
# CTCI 4.2
# Random links
# https://orth.uk
# https://foodprint.orth.uk for software engineering project
# https://devwebcl.blogspot.com/2016/12/big-o-comparison.html
# https://awwapp.com/b/umyxkgethk506/
def main():
# ordered_list = [1, 2, 3, 6]
ordered_list = [1, 2, 3, 6, 10, 12, 15, 16]
print(ordered_list)
result = create_binary_from_sorted_arr(ordered_list)
viz_tree(result)
print(result)
def create_binary_from_sorted_arr(arr: List[int]) -> BiNode:
if (len(arr) == 0):
return None
if (len(arr) == 1):
return BiNode(arr[0], None, None)
# pick out middle number
middle_index = len(arr) // 2
# recursively call
left = create_binary_from_sorted_arr(arr[0:middle_index])
right = create_binary_from_sorted_arr(arr[middle_index + 1:])
return BiNode(arr[middle_index], left, right)
if __name__ == "__main__":
main()