Skip to content

Commit d8ce3f9

Browse files
authored
Merge pull request #784 from kumarankm/tree-normal
Added normal tree using python
2 parents 7438ad0 + 125cb0c commit d8ce3f9

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

Data Structures/Trees/normal_tree.py

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

0 commit comments

Comments
 (0)