-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trees3.py
62 lines (40 loc) · 1.31 KB
/
Trees3.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
class Treenode:
def __init__(self,Employee,Post):
self.Employees = []
self.Post = []
self.data = Employee
self.data1 = Post
self.parent = None
def add_child(self,emp):
emp.parent = self
self.Employees.append(emp)
self.Post.append(self.data1)
def get_level(self):
level = 0
p = self.parent
while p:
level += 1
p = p.parent
return level
def print_tree(self):
spaces = " "*self.get_level()*2
print(f'{spaces}{self.data} ({self.data1})')
if self.Employees:
for employee in self.Employees:
employee.print_tree()
def build_Company_Tree():
head = Treenode("Nilupal","CEO")
head1 = Treenode("Chinmay","CTO")
head1_1 = Treenode("Vishwa","Infrastructure head")
head1_1.add_child(Treenode("Dhaval","Cloud Manager"))
head1_1.add_child(Treenode("Abhijit","App Manager"))
head1.add_child(head1_1)
head2 = Treenode("Gels","Hr Head")
head2.add_child(Treenode("Peter","Recruitment Manager"))
head2.add_child(Treenode("Waqas","Policy Manager"))
# print(head1_1.get_level())
head.add_child(head1)
head.add_child(head2)
return head
root = build_Company_Tree()
root.print_tree()