Write a recursive function that calculates the sum of the branch lengths given a tree and node number. The function should return the sum of all branches leading from the node to all descendant leaves. The function should return 0 if a leaf is given and return an error message if the node/leaf doesn't exist. Jse your function and print the output for nodes: 17, 15, 13, 9, 2

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 6PE
icon
Related questions
Question

Code you provide code in python? 

Write a recursive function that calculates the sum of the branch lengths given a tree and node number. The function should return the sum of all branches leading from
the node to all descendant leaves. The function should return O if a leaf is given and return an error message if the node/leaf doesn't exist.
Use your function and print the output for nodes: 17, 15, 13, 9, 2
]: tree3
{
15: ['N',{'node': 14, 'branch': 0.1}, {'node': 12, 'branch': 0.3}],
14: ['N',{'node': 13, 'branch': 0.2}, {'node': 1, 'branch': 1.5}],
13: ['N',{'node': 11, 'branch': 0.3}, {'node': 9, 'branch': 0.4}],
12: ['N',{'node': 10, 'branch': 0.3}, {'node': 8, 'branch': 0.7}],
11: ['N',{'node': 2, 'branch': 0.25}, {'node': 3, 'branch': 0.35}],
10: ['N',{'node': 6, 'branch': 0.1}, {'node': 7, 'branch': 0.12}],
9: ['N',{'node': 4, 'branch': 0.02}, {'node': 5, 'branch': 0.08}],
8: ['L'],
7: ['L'],
6: ['L'],
5: ['L'],
4: ['L'],
3: ['L'],
2: ['L'],
1: ['L']
}
%3D
# Answer
Transcribed Image Text:Write a recursive function that calculates the sum of the branch lengths given a tree and node number. The function should return the sum of all branches leading from the node to all descendant leaves. The function should return O if a leaf is given and return an error message if the node/leaf doesn't exist. Use your function and print the output for nodes: 17, 15, 13, 9, 2 ]: tree3 { 15: ['N',{'node': 14, 'branch': 0.1}, {'node': 12, 'branch': 0.3}], 14: ['N',{'node': 13, 'branch': 0.2}, {'node': 1, 'branch': 1.5}], 13: ['N',{'node': 11, 'branch': 0.3}, {'node': 9, 'branch': 0.4}], 12: ['N',{'node': 10, 'branch': 0.3}, {'node': 8, 'branch': 0.7}], 11: ['N',{'node': 2, 'branch': 0.25}, {'node': 3, 'branch': 0.35}], 10: ['N',{'node': 6, 'branch': 0.1}, {'node': 7, 'branch': 0.12}], 9: ['N',{'node': 4, 'branch': 0.02}, {'node': 5, 'branch': 0.08}], 8: ['L'], 7: ['L'], 6: ['L'], 5: ['L'], 4: ['L'], 3: ['L'], 2: ['L'], 1: ['L'] } %3D # Answer
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Computational Systems
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning