-
Notifications
You must be signed in to change notification settings - Fork 0
/
General_tree_exercise.py
74 lines (60 loc) · 2.09 KB
/
General_tree_exercise.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
68
69
70
71
72
73
74
class TreeNode:
def __init__(self,name,designation):
self.name=name
self.designation=designation
self.children=[]
self.parent=None
def get_lvl(self):
level=0
d=self.parent
while d:
level+=0
d=d.parent
return level
def add_child(self,child):
self.children.append(child)
child.parent=self
def print_tree(self,property_name):
if property_name=='both':
value= self.name+ "("+self.designation+")"
if property_name=='name':
value=self.name
else:
value=self.designation
spaces=' '*self.get_lvl()*3
prefix= spaces + "|______" if self.parent else ""
print(prefix+value)
if self.children:
for i in self.children:
i.print_tree(property_name)
def company():
comp=TreeNode("Nilupul","CEO")
mem=TreeNode("Chinmay","CTO")
submem=TreeNode("Vishwa","Infrastructure Head")
submem.add_child(TreeNode("Dhaval","Cloud Manager"))
submem.add_child(TreeNode("Abijith","App Manager"))
mem.add_child(submem)
mem.add_child(TreeNode("Amir","Application Head"))
mem2=TreeNode("Gels","HR Head")
mem2.add_child(TreeNode("Peter","Requirement Manager"))
mem2.add_child(TreeNode("Waqus","Product Manager"))
comp.add_child(mem)
comp.add_child(mem2)
return comp
# infra_head = TreeNode("Vishwa","Infrastructure Head")
# infra_head.add_child(TreeNode("Dhaval","Cloud Manager"))
# infra_head.add_child(TreeNode("Abhijit", "App Manager"))
# cto = TreeNode("Chinmay", "CTO")
# cto.add_child(infra_head)
# cto.add_child(TreeNode("Aamir", "Application Head"))
# # HR hierarchy
# hr_head = TreeNode("Gels","HR Head")
# hr_head.add_child(TreeNode("Peter","Recruitment Manager"))
# hr_head.add_child(TreeNode("Waqas", "Policy Manager"))
# ceo = TreeNode("Nilupul", "CEO")
# ceo.add_child(cto)
# ceo.add_child(hr_head)
# return ceo
if __name__=="__main__":
ff=company()
ff.print_tree("both")