-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc-8.44.py
More file actions
29 lines (23 loc) · 660 Bytes
/
c-8.44.py
File metadata and controls
29 lines (23 loc) · 660 Bytes
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
"""
give an efficient algorithm that computes and prints for every position p of a tree T
the element of p followed by the height of the p's subtree
"""
def height_all(el):
if len(el) == 0:
print(f'el: {el}, h: 0')
return 0
else:
max = 0
for c in el.iterchildren():
h = height_all(c)
if h > max:
max = h
print(f'el: {el}, h: {1 + max}')
return 1 + max
if __name__ == "__main__":
from lxml import etree as et
tree = et.parse('./input/trees/bin_tree_1.xml')
root = tree.getroot()
h_all = height_all(root)
assert h_all == 3
print(h_all)