Skip to content

Commit 9311bb2

Browse files
committed
add height to general tree node
1 parent 26cbea6 commit 9311bb2

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

data-structure/trees/general_tree.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
23
class NodeTree:
34
def __init__(self, data):
45
self.data = data
@@ -23,6 +24,15 @@ def get_level(self):
2324
lvl +=1
2425
p = p.parent
2526
return lvl
27+
28+
def height(self):
29+
if len(self.children) == 0:
30+
return 0
31+
else:
32+
max_height = -1
33+
for child in self.children:
34+
max_height = max(max_height, child.height())
35+
return max_height + 1
2636

2737

2838
if __name__ == "__main__":
@@ -56,25 +66,4 @@ def get_level(self):
5666
root.add_node(tv)
5767

5868
root.print_tree()
59-
60-
# print(root.get_level())
61-
# print(root.children[0].get_level())
62-
# print(root.children[0].children[0].get_level())
63-
64-
65-
# print("\n")
66-
# print(root.data)
67-
# print("\t" + root.children[0].data)
68-
# print("\t\t" + root.children[0].children[0].data)
69-
# print("\t\t" + root.children[0].children[1].data)
70-
# print("\t\t" + root.children[0].children[2].data)
71-
72-
# print("\t" + root.children[1].data)
73-
# print("\t\t" + root.children[1].children[0].data)
74-
# print("\t\t" + root.children[1].children[1].data)
75-
# print("\t\t" + root.children[1].children[2].data)
76-
77-
# print("\t" + root.children[2].data)
78-
# print("\t\t" + root.children[2].children[0].data)
79-
# print("\t\t" + root.children[2].children[1].data)
80-
# print("\t\t" + root.children[2].children[2].data)
69+
print(root.height())

0 commit comments

Comments
 (0)