Skip to content

Commit 125cb0c

Browse files
authored
Update normal_tree.py
1 parent 5eaa6e8 commit 125cb0c

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

Data Structures/Trees/normal_tree.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1+
# Class to build a tree node
12
class TreeNode:
3+
# Constructor to create tree node
24
def __init__(self, data):
35
self.data = data
46
self.children = []
57
self.parent = None
6-
8+
9+
# Function to add a add in individual node
710
def add_child(self, child):
811
child.parent = self
912
self.children.append(child)
10-
13+
14+
# Function to get a level of the selected node
1115
def getlevel(self):
1216
level = 0
1317
p = self.parent
1418
while p:
1519
level += 1
1620
p = p.parent
1721
return level
18-
22+
23+
# Print the contructed tree
1924
def printt(self):
2025
prefix = (" " * 4 * self.getlevel()) + ("|--" if self.parent else "")
2126
print(prefix + self.data)
@@ -24,7 +29,9 @@ def printt(self):
2429
child.printt()
2530

2631

32+
# Funcation to build a tree
2733
def build_tree():
34+
# Taking input dynamically
2835
print("Do the following given in eg bracket")
2936
print("\n")
3037
root = input('Enter a root node element (eg: Food): ')

0 commit comments

Comments
 (0)