Write a function called tree_sum () with the following function interface documentation: def tree_sum(tnode, val): Purpose Given a binary search tree containing integers, calculate the sum of all data in the tree less than or eq ual to a given value. Pre-conditions: tnode: a binary serach tree containing integers (satisfying the BST property) val: an integer lists: Return: *** The sum of all data less than val The function should return the sum of all values in tnode that are less than or equal to val. You may assume that the values in tnode are all unique, and that the tree satisfies the binary search tree property. For example, suppose a BST named t1 contains the data 3,7,8,12: • tree_sum (t1, 7) should return 10 • tree_sum (t1, 11) should return 18 • tree_sum (t1, 2) should return O. Notes: By definition, the sum of all values in an empty tree is 0, for any value val. • You may import the treenode module. • The function interface documentation is given. You do not need to include it in your answer.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16SA
icon
Related questions
Question

treenode adt:

class TreeNode(object):
def __init__(self, data, left=None, right=None):
"""
Create a new TreeNode for the given data.
Pre-conditions:
data: Any data value to be stored in the KVTreeNode
left: Another TreeNode (or None, by default)
right: Another TreeNode (or None, by default)
"""
self.data = data
self.left = left
self.right = right

Write a function called tree_sum () with the following function interface documentation:
def tree_sum(tnode, val):
Purpose
Given a binary search tree containing integers, calculate the sum of all data in the tree less than or eq
ual to a given value.
Pre-conditions:
* * *
●
.
tnode: a binary serach tree containing integers (satisfying the BST property)
val: an integer lists:
The function should return the sum of all values in tnode that are less than or equal to val. You may assume
that the values in tnode are all unique, and that the tree satisfies the binary search tree property.
For example, suppose a BST named t1 contains the data 3,7,8,12:
Return:
tree_sum (t1, 7) should return 10
tree_sum (t1, 11) should return 18
• tree_sum (t1, 2) should return 0.
Notes:
.
The sum of all data less than val
By definition, the sum of all values in an empty tree is 0, for any value val.
You may import the treenode module.
• The function interface documentation is given. You do not need to include it in your answer.
Transcribed Image Text:Write a function called tree_sum () with the following function interface documentation: def tree_sum(tnode, val): Purpose Given a binary search tree containing integers, calculate the sum of all data in the tree less than or eq ual to a given value. Pre-conditions: * * * ● . tnode: a binary serach tree containing integers (satisfying the BST property) val: an integer lists: The function should return the sum of all values in tnode that are less than or equal to val. You may assume that the values in tnode are all unique, and that the tree satisfies the binary search tree property. For example, suppose a BST named t1 contains the data 3,7,8,12: Return: tree_sum (t1, 7) should return 10 tree_sum (t1, 11) should return 18 • tree_sum (t1, 2) should return 0. Notes: . The sum of all data less than val By definition, the sum of all values in an empty tree is 0, for any value val. You may import the treenode module. • The function interface documentation is given. You do not need to include it in your answer.
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Types of trees
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