Question

In: Computer Science

Write a function in Python that adds two Binary Trees together. The inputs of your function...

Write a function in Python that adds two Binary Trees together. The inputs of your function should be two binary trees (or their roots, depending on how you plan on coding this), and the output will be one Binary Tree that is the sum of the two inputted.

  • To add Binary Trees together: Add the values of nodes with the same position in both trees together, this will be the value assigned to the node of the same position in your output tree
  • If one tree has a node that the other tree does not, the value of that node in the output tree will just simply be the same as it is in the initial tree
  • Think about overlapping the trees and adding the nodes together that overlap
  • You are to assume that all the trees being inputted will be Binary Trees (only points left and right)

Solutions

Expert Solution


Required Answer

# Required python program to add two given binary trees
class new_node: 
        def __init__(self, data): 
                self.data = data 
                self.left = self.right = None

# Given a binary tree, prints nodes in inorder_traversal 
#first recur the left node,then root node and then the right node
def inorder_traversal(node): 
        if (not node): 
                return
        inorder_traversal(node.left) 
        print(node.data, end = " ") 
        inorder_traversal(node.right) 

# Function to Add given two binary trees 
def Add_Trees(b_tree1, b_tree2): 
        if (not b_tree1): 
                return b_tree2 
        if (not b_tree2): 
                return b_tree1 
        b_tree1.data += b_tree2.data 
        b_tree1.left = Add_Trees(b_tree1.left, b_tree2.left) 
        b_tree1.right = Add_Trees(b_tree1.right, b_tree2.right) 
        return b_tree1 

# Driver code 
if __name__ == '__main__': 
        
    #creating the first binary tree
        tree1 = new_node(10) 
        tree1.left = new_node(20) 
        tree1.right = new_node(30) 
        tree1.left.left = new_node(40) 
        tree1.left.right = new_node(50) 
        tree1.right.right = new_node(60)
        tree1.right.right.right = new_node(100)

    #creating the second binary tree
        tree2 = new_node(40) 
        tree2.left = new_node(10) 
        tree2.right = new_node(70) 
        tree2.left.left = new_node(30) 
        tree2.right.left = new_node(20) 
        tree2.right.right = new_node(60)
        
        #Printing the first binary tree in inorder traversal
        print("The first binary tree is:")
        inorder_traversal(tree1)
        
        #Printing the second binary tree in inorder traversal
        print("\nThe second binary tree is:")
        inorder_traversal(tree2)
        
        #Calling the function two add the two trees
        final_tree = Add_Trees(tree1, tree2) 
        print("\nThe final Binary Tree is:") 
        inorder_traversal(final_tree) 


Screenshot of code and output:


Related Solutions

code in python I want to make a function that adds all the multipliers together. The...
code in python I want to make a function that adds all the multipliers together. The code is posted below and please look at wanted output section to see what I want the code to output. The last line of the code is the only addition I need. Thanks. Code: initial=int(input("Initial value : "))       #taking inputs multiplier=float(input("Multiplier : ")) compound=int(input("No of compounds : ")) print("Your values are:") mult=initial                         #initalizing to find answer for i in range(0,compound):         #multiplying by multiplier    ...
In C++, write a function that takes in as inputs two arrays, foo and bar, and...
In C++, write a function that takes in as inputs two arrays, foo and bar, and their respective array sizes. The function should then output the concatenation of the two arrays as a singly linked list. You may assume that you are provided a singly linked list header file.
Program must be in Python Write a program in Python whose inputs are three integers, and...
Program must be in Python Write a program in Python whose inputs are three integers, and whose output is the smallest of the three values. Input is 7 15 3
Write a method for binary tree in Python that can determine whether a binary tree is...
Write a method for binary tree in Python that can determine whether a binary tree is a binary search tree or not. The input should be a binary tree. The output should be true or false. True= binary tree meets the criteria to be a binary search tree. False= does not meet the criteria to be a binary search tree.
Using Python. Write a program that reads a sequence (unknown number of inputs) of integer inputs...
Using Python. Write a program that reads a sequence (unknown number of inputs) of integer inputs and prints the number of even and odd inputs in the sequence. please explain. Thanks
Suppose there are two inputs in the production function, labor and capital, and these two inputs...
Suppose there are two inputs in the production function, labor and capital, and these two inputs are perfect substitutes. The existing technology permits 5 machines to do the work of 2 workers. So the production function is f(E, K) = 2K + 5E. The firm wants to produce q units of output, where q > 0 is some number. Suppose the price of capital is $10 per machine per hour. What combination of inputs will the firm use if the...
Need in JAVA. You are to use Binary Trees to do this Program. Write a complete...
Need in JAVA. You are to use Binary Trees to do this Program. Write a complete program, which will process several sets of numbers: For each set of numbers you should: 1. Create a binary tree. 2. Print the tree using “inorder”, “preorder”, and “postorder”. 3. Call a method Count which counts the number of nodes in the tree. 4. Call a method Children which prints the number of children each node has. 5. Inset and delete several nodes according...
Write a Matlab function called: lin_interp. The function should have three inputs: the two original data...
Write a Matlab function called: lin_interp. The function should have three inputs: the two original data arrays (call them x and f), and the array you would like to interpolate to (call it xstar). The function should have one output: the interpolated array ( call it fstar). The function should be able to interpolate x and f onto xstar using linear interpolation and give the result as fstar. The function may not use any intrinsic functions except length.
Write an application that reads three integers, adds all three together and computes an average of...
Write an application that reads three integers, adds all three together and computes an average of the three entries and computes any remainder of the integer division. A remainder represents the modulus result of dividing an even by an odd number or vice versa. Display the output. Enter an integer score 3 Enter an integer score 6 Enter an integer score 4 The average of 3, 6, 4 is 4 with a remainder of 1 Press any key to continue...
python Write a program that prints your name 100 times to the screen. Write a function...
python Write a program that prints your name 100 times to the screen. Write a function that takes a string s and an integer n as parameters and prints the string s a total of n times (once per line). Write a for loop that prints all the integers from 3141 to 5926, skipping every other one. Write a for loop that prints every 5th integer from 5926 down to 3141. Write a program (using a for loop) to print...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT