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    ...
Write an algorithm to determine if two binary trees are identical when the ordering of the...
Write an algorithm to determine if two binary trees are identical when the ordering of the subtrees for a node is ignored. For example, if a tree has root node with value R, left child with value A and right child with value B, this would be considered identical to another tree with root node value R, left child value B, and right child value A. Make the algorithm as efficient as you can. Analyze your algorithm’s running time. How...
Research the topic of binary search trees. Write a brief summary of your understanding of this....
Research the topic of binary search trees. Write a brief summary of your understanding of this. Design a simple program, using pseudocode, that performs a search of a binary search tree.. In your own words, explain how a binary search tree works using graph theory terminology. Construct a binary search tree for any alphabetically ordered list of five words. Represent it visually as a graph. Discuss any characteristics you note about the graph you created in part (d). In your...
Python. Write a function last_occur(s, e) that takes as inputs a sequence (i.e., a string or...
Python. Write a function last_occur(s, e) that takes as inputs a sequence (i.e., a string or list) s and an element e, and that calls itself recursively to find and return the index of the last occurrence of e in s. If s is a string, e will be a single-character string; if s is a list, e can be any value. Don’t forget that the index of the first element in a sequence is 0. Important notes: If e...
Using Python, create a function whose inputs are two matrices and whose output is the element...
Using Python, create a function whose inputs are two matrices and whose output is the element wise product of the two matrices.
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.
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...
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 3 machines to do the work of 2 worker. So F(E,K)=2K+3E. The firm wants to produce 60 units of output. Suppose the price of capital is $10 per machine per hour. What combination of inputs will the firm use if the wage rate is $10 or $15 or $20 per hour? What if the firm wants to...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT