-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathlist-representation.py
67 lines (43 loc) · 1.14 KB
/
list-representation.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
def create_tree(root=None) -> list:
return [None, [], []]
def insert_root(tree: list, root=None):
tree[0] = root
def insert_left(tree: list, root=None):
left_child: list = tree.pop(1)
tree.insert(1, [root, left_child, []])
def insert_right(tree: list, root=None):
right_child: list = tree.pop(2)
tree.insert(2, [root, [], right_child])
def get_root(tree: list):
return tree[0]
def get_left_child(tree: list) -> list:
return tree[1]
def get_right_child(tree: list) -> list:
return tree[2]
# Write a function build_tree that returns a tree
# using the list of lists functions that look like this :
# a
# / \
# b c
# \ / \
# d e f
def build_tree() -> list:
tree: list = create_tree()
insert_root(tree, 'a')
insert_right(tree, 'f')
insert_right(tree, 'c')
insert_left(get_right_child(tree), 'e')
insert_left(tree, 'b')
insert_right(get_left_child(tree), 'd')
return tree
binary_tree: list = build_tree()
print(binary_tree)
# ['a',
# ['b',
# [],
# ['d', [], []]],
# ['c',
# ['e', [], []],
# ['f', [], []]
# ]
# ]