Skip to content

Commit 5eaa6e8

Browse files
committed
Added normal tree using python
1 parent 4b90515 commit 5eaa6e8

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

Data Structures/Trees/normal_tree.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
class TreeNode:
2+
def __init__(self, data):
3+
self.data = data
4+
self.children = []
5+
self.parent = None
6+
7+
def add_child(self, child):
8+
child.parent = self
9+
self.children.append(child)
10+
11+
def getlevel(self):
12+
level = 0
13+
p = self.parent
14+
while p:
15+
level += 1
16+
p = p.parent
17+
return level
18+
19+
def printt(self):
20+
prefix = (" " * 4 * self.getlevel()) + ("|--" if self.parent else "")
21+
print(prefix + self.data)
22+
if self.children:
23+
for child in self.children:
24+
child.printt()
25+
26+
27+
def build_tree():
28+
print("Do the following given in eg bracket")
29+
print("\n")
30+
root = input('Enter a root node element (eg: Food): ')
31+
root = TreeNode(root)
32+
33+
print("\n")
34+
it = input('Enter the value for First child of root node(eg: Italian): ')
35+
italy = TreeNode(it)
36+
root.add_child(italy)
37+
38+
ch = input('Enter the value for Second child of root node(eg: Chinese): ')
39+
chinese = TreeNode(ch)
40+
root.add_child(chinese)
41+
42+
me = input('Enter the value for Third child of root node(eg: Mexican): ')
43+
mexican = TreeNode(me)
44+
root.add_child(mexican)
45+
46+
print("\n")
47+
pi = input('Enter the value for First child of Italian node(eg: Pizza): ')
48+
italy.add_child(TreeNode(pi))
49+
la = input('Enter the value for Second child of Italian node(eg: Lasagna): ')
50+
italy.add_child(TreeNode(la))
51+
pis = input(
52+
'Enter the value for Third child of Italian node(eg: Pistacho): ')
53+
italy.add_child(TreeNode(pis))
54+
55+
print("\n")
56+
no = input('Enter the value for First child of Chineese node(eg: Noodles): ')
57+
chinese.add_child(TreeNode(no))
58+
ric = input(
59+
'Enter the value for Second child of Chineese node(eg: Riceballs): ')
60+
chinese.add_child(TreeNode(ric))
61+
fr = input(
62+
'Enter the value for Third child of Chineese node(eg: Fried Rice): ')
63+
chinese.add_child(TreeNode(fr))
64+
65+
print("\n")
66+
tc = input('Enter the value for First child of Mexican node(eg: Tacos): ')
67+
mexican.add_child(TreeNode(tc))
68+
gr = input('Enter the value for Second child of Mexican node(eg: Gyro): ')
69+
mexican.add_child(TreeNode(gr))
70+
shw = input(
71+
'Enter the value for Third child of Mexican node(eg: Shawarma): ')
72+
mexican.add_child(TreeNode(shw))
73+
print('\n')
74+
print("\n")
75+
76+
return root
77+
78+
# Static Input sample code
79+
'''
80+
def buildtree():
81+
root = TreeNode('Electronics')
82+
83+
laptop = TreeNode('Laptop')
84+
laptop.add_child(TreeNode('Dell'))
85+
laptop.add_child(TreeNode('Apple'))
86+
laptop.add_child(TreeNode('Microsoft'))
87+
88+
mobile = TreeNode('Mobile')
89+
mobile.add_child(TreeNode('Mi'))
90+
mobile.add_child(TreeNode('One plus'))
91+
mobile.add_child(TreeNode('Vivo'))
92+
93+
tv = TreeNode('TV')
94+
tv.add_child(TreeNode('Samsung'))
95+
tv.add_child(TreeNode('LG'))
96+
97+
root.add_child(laptop)
98+
root.add_child(mobile)
99+
root.add_child(tv)
100+
101+
return root
102+
'''
103+
104+
105+
if __name__ == "__main__":
106+
root = build_tree()
107+
root.printt()

0 commit comments

Comments
 (0)