-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathclass-representation.py
67 lines (48 loc) · 1.33 KB
/
class-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
class TreeNode:
def __init__(self, key=None, left=None, right=None):
self.key = key
self.left = left
self.right = right
def insert_root_value(self, key=None):
self.key = key
def insert_left(self, key=None):
self.left = TreeNode(key, self.left)
def insert_right(self, key=None):
self.right = TreeNode(key, None, self.right)
def get_root_value(self):
return self.key
def get_left_child(self):
return self.left
def get_right_child(self):
return self.right
# 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() -> TreeNode:
tree: TreeNode = TreeNode('a')
tree.insert_right('f')
tree.insert_right('c')
tree.get_right_child().insert_left('e')
tree.insert_left('b')
tree.get_left_child().insert_right('d')
return tree
binary_tree: TreeNode = build_tree()
def print_tree(tree: TreeNode):
if tree is not None:
print_tree(tree.get_left_child())
print(tree.key, end=", ")
print_tree(tree.get_right_child())
print_tree(binary_tree)
# ['a',
# ['b',
# [],
# ['d', [], []]],
# ['c',
# ['e', [], []],
# ['f', [], []]
# ]
# ]