Question

In: Computer Science

Python question Define a function called max_value_length(tree) that takes a Binary Tree object as a parameter...

Python question

Define a function called max_value_length(tree) that takes a Binary Tree object as a parameter and returns an integer that contains the longest character length of all the values in that tree. Use recursion.

For example, the character length of value 123 is 3, because there are three digits. The character length of value “Ruru” is 4, because it has four characters. Further, the max_value_length() function would return 4 if the only node values in the tree were 123 and “Ruru”, because 4 is greater than 3.

Note 1: You must use recursion to answer this question (i.e., not loops).
Note 2: Values in the tree are always treated as strings.

For example:

Test Result
binary_tree = create_tree_from_nested_list([7, [2, [14, None, None], [5, None, None]], [9, None, [144, None, None]]])
print(max_value_length(binary_tree))
3
binary_tree = create_tree_from_nested_list(["Hoiho", ["Kaki", ["Takahe", None, None], None], ["Ruru", None, ["Moa", None, ["Piwaiwaka", None, None]]]])
print(max_value_length(binary_tree))
9
binary_tree = create_tree_from_nested_list([])
print(max_value_length(binary_tree))
0

Solutions

Expert Solution

Solution:


binary_tree = ["Hoiho", ["Kaki", ["Takahe", None, None], None], ["Ruru", None, ["Moa", None, ["Piwaiwaka", None, None]]]]

max=0 #intializing max variable to "0", this vriable is used to find maximmm size

def my_function(binary_tree):
global max #Accessing max variable out side this function

if(len(binary_tree) == 0): # if length of input list is empty ,then return
return
if(index_in_list(binary_tree, 1) and index_in_list(binary_tree, 2) ): # if statement to left and right childs
my_function(binary_tree[1]) #recursion with left child
my_function(binary_tree[2]) #recursion with right child
  
if binary_tree is not None: #length will not be calculated for None Type
if max < len(str(binary_tree[0])): # condition to compare and to assign maximum value
max = len(str(binary_tree[0]))


return max #return maximum value

def index_in_list(a_list, index): #if condition used to check wether the index exist or not
if a_list is not None: #if index exists and not None,then return True
return index < len(a_list)

my_function(binary_tree) #calling function

print(max) # finally printing max value

Code in python 3.8.1 IDLE:

Output:

List1: for the list  [7, [2, [14, None, None], [5, None, None]], [9, None, [144, None, None]]]

List2: for the list ["Hoiho", ["Kaki", ["Takahe", None, None], None], ["Ruru", None, ["Moa", None, ["Piwaiwaka", None, None]]]]

List3: for the list []


Related Solutions

Write a recursive function in python called make_palindrome that takes a sequence as a parameter and...
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and returns a new sequence that is twice the length of the parameter sequence but that contains the contents of the original in palindrome form. For example, if the sequence "super" is passed into the function, the function will return "superrepus".
Define a function called 'filterWords, which takes a parameter. The first parameter, text" is an nltk.text.Text...
Define a function called 'filterWords, which takes a parameter. The first parameter, text" is an nltk.text.Text object. The function definition code stub is given in the editor. Perform the given operation for the 'text' object and print the results: • Filter the words whose length is greater than 15 from the complete set of 'text', and store into "large_words' variable as a list
Python question Define a function called selection_order(items, interval) which takes two parameters: items is a list...
Python question Define a function called selection_order(items, interval) which takes two parameters: items is a list of elements and interval is an integer larger than 0. Imagine that the elements in items were arranged in a circle. Including the first element, count off the elements in items up to the interval position and then remove that element from the circle. From that position, begin counting the positions of the elements again and remove the element that has the next interval...
Python question Define a function called hash_string_weighted_folding(string_to_hash, modulus) that takes two parameters: a string variable string_to_hash...
Python question Define a function called hash_string_weighted_folding(string_to_hash, modulus) that takes two parameters: a string variable string_to_hash and an integer called modulus. The function returns an integer that is the hash value of string_to_hash. This hash function processes the string in blocks of 4 characters (the final block may be shorter depending on the length of string_to_hash) and multiplies each character’s ASCII value by a weight. The weight depends on the position of the character in the block of 4 characters:...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the name parameter value is a name in the form LastName, FirstName(e.g., ”Grounds, Nic”) then the function should return a list of two elements where FirstName is the first element and LastName is the second element. If the name parameter value is a name in the form FirstName LastName(e.g., ”Nic Grounds”) then the function should return a list of two elements where FirstName is the...
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
This is an intro to python question. #Write a function called search_for_string() that takes two #parameters,...
This is an intro to python question. #Write a function called search_for_string() that takes two #parameters, a list of strings, and a string. This function #should return a list of all the indices at which the #string is found within the list. # #You may assume that you do not need to search inside the #items in the list; for examples: # # search_for_string(["bob", "burgers", "tina", "bob"], "bob") # -> [0,3] # search_for_string(["bob", "burgers", "tina", "bob"], "bae") # -> []...
​Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
In python Define a function called cfb_graph which takes no arguments. Form a directed graph from...
In python Define a function called cfb_graph which takes no arguments. Form a directed graph from the file cfb2010.csv by considering the teams as vertices and creating an edge between team1 and team2 only if team1 defeated team2. You should familiarize yourself with this file before attempting this part. cfb_graph will return a dictionary giving this representation.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT