-
Notifications
You must be signed in to change notification settings - Fork 617
/
n_ary_tree.py
136 lines (114 loc) · 3.44 KB
/
n_ary_tree.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
""" Ques: Create a general (N-ary) tree
Language: Python
Firstly, class treenode is defined with several inner functions including "__init__" for creating tree node with chils as list and #intitially storing 'None' as address of parent, "add_child" add data in child list, there is a helping function named "get_level" as #well to get the level of a child node while printing its value and finally "print_tree" function prints tree with according to its #level .
And function "build_tree" creats the whole tree using class "TreeNode"
"""
class TreeNode:
def __init__(self, data):
self.data = data
self.child = []
self.parent = None
def add_child(self, obj):
obj.parent = self
self.child.append(obj)
def get_level(self):
lev = 0
p = self.parent
while p != None:
lev += 1
p = p.parent
return lev
def print_tree(root):
s = root.data + " child nodes=> "
for i in root.child:
s = s + i.data
s += ", "
print(s)
for i in root.child:
print_tree(i)
def build_tree():
print('Enter data of root node:',end=" ")
data = input()
root = TreeNode(data)
no_node = int(input('Enter number of nodes for this root node:'))
for i in range(no_node):
d = build_tree()
root.child.append(d)
return root
if __name__ == "__main__":
root = build_tree()
print_tree(root)
"""e.g. 1
#input:
#Enter data of root node:2
#Enter number of nodes for this root node:3
#Enter data of root node:3
#Enter number of nodes for this root node:0
#Enter data of root node:4
#Enter number of nodes for this root node:2
#Enter data of root node:5
#Enter number of nodes for this root node:0
#Enter data of root node:6
#Enter number of nodes for this root node:0
#Enter data of root node:7
#Enter number of nodes for this root node:0
#Output:
#2 child nodes=> 3, 4, 7,
#3 child nodes=>
#4 child nodes=> 5, 6,
#5 child nodes=>
#6 child nodes=>
#7 child nodes=>
daigram:
2
/ | \
3 4 7
/ \
5 6
Enter data of root node:a
Enter number of nodes for this root node:3
Enter data of root node:b
Enter number of nodes for this root node:2
Enter data of root node:s
Enter number of nodes for this root node:0
Enter data of root node:t
Enter number of nodes for this root node:2
Enter data of root node:w
Enter number of nodes for this root node:0
Enter data of root node:r
Enter number of nodes for this root node:0
Enter data of root node:e
Enter number of nodes for this root node:2
Enter data of root node:u
Enter number of nodes for this root node:0
Enter data of root node:v
Enter number of nodes for this root node:0
Enter data of root node:r
Enter number of nodes for this root node:2
Enter data of root node:x
Enter number of nodes for this root node:0
Enter data of root node:y
Enter number of nodes for this root node:0
a child nodes=> b, e, r,
b child nodes=> s, t,
s child nodes=>
t child nodes=> w, r,
w child nodes=>
r child nodes=>
e child nodes=> u, v,
u child nodes=>
v child nodes=>
r child nodes=> x, y,
x child nodes=>
y child nodes=>
diagram:
a
/ | \
b e r
/\ /\ /\
s t u v x y
/\
w r
"""
#Time complexity: O(n) ->number of nodes
#Space complexity: O(n) ->number of nodes