-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree_20.09.py
45 lines (35 loc) · 1.18 KB
/
tree_20.09.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
class Node:
def __init__(self, data):
self.data = data
self.elements = []
def __str__(self):
next_elements = []
if self.elements:
for element in self.elements:
next_elements.append(element.data)
return 'data: {}, next_element: {}\n'.format(self.data, next_elements)
def append_children(self, children):
self.elements.extend(children)
class Tree:
def __init__(self, root):
self.root = root
def print_tree(self, element):
print(element)
for child in element.elements:
self.print_tree(child)
return
def height(self, current_root):
result_height = 1
for child in current_root.elements:
result_height = max(result_height, 1+self.height(child))
return result_height
root = Node('A')
arr_nodes = [Node(chr(i)) for i in range(66, 70)]
root.append_children(arr_nodes)
arr_nodes_1 = [Node(chr(i)) for i in range(70, 72)]
arr_nodes[1].append_children(arr_nodes_1)
arr_nodes_2 = [Node(chr(i)) for i in range(72, 74)]
arr_nodes_1[0].append_children(arr_nodes_2)
tree = Tree(root)
tree.print_tree(root)
print(tree.height(root))