Question

In: Computer Science

Write a python function comparing two binary trees and returns whethere they are same. Input two...

Write a python function comparing two binary trees and returns whethere they are same. Input two binary tress and output true or false. True= They are the same, False= They are not . Test the function .

Solutions

Expert Solution

BINARY TREE :- A binary tree is defined as a finite set of elements called as nodes such that

a. T is empty called the null tree

b. T contains a distinguished nodes F called the root of tree and the remaining nodes of the tree from an ordered pair of disjoint binary tree T1 and T2.

IDENTICAL TREE :- two trees are said to be identical when they have same data and same arrangement of data.

ALGORITHM FOR CHECKING WHETHER THE BINARY TREES ARE IDENTICAL OR NOT :-

1. Check for data for both trees. whether they are equal or not.

2. Turn to left of root.

3. Turn to right of root.

class TreeNode:

    def __init__(self, key=None, left=None, right=None):
        self.key = key
        self.left = left
        self.right = right


# Recursive function

def same(x, y):

    # if both trees are empty,  it will return true

    if x is None and y is None:
        return True

    return (x and y) and (x.key == y.key) and \
           same(x.left, y.left) and same(x.right, y.right)


if __name__ == '__main__':

    # Nodes of first binary tree

    x = TreeNode(6)
    x.left = TreeNode(2)
    x.right = TreeNode(8)
    x.left.left = TreeNode(3)
    x.left.right = TreeNode(5)

    # Nodes of second binary tree

    y = TreeNode(6)
    y.left = TreeNode(2)
    y.right = TreeNode(8)
    y.left.left = TreeNode(3)
    y.left.right = TreeNode(5)

    if same(x, y):
        print("True = they are same")
    else:
        print("False = they are not ")

OUTPUT : -
True = they are same

Process finished with exit code 0

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------THANK YOU ----------------------------------------------------------------------------------


Related Solutions

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...
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
Write a PYTHON function CommonLetters(mystring) that takes mystring as input and returns the number of letters...
Write a PYTHON function CommonLetters(mystring) that takes mystring as input and returns the number of letters in mystring that also occur in the string ‘Python’. Using above function, write a program that repeatedly prompts the user for a string and then prints the number of letters in the string that are also in string ‘Python’. The program terminates when the user types an empty string.
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns...
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns the number of 1's in the binary representation of n. Use the fact that this is equal to the number of 1's in the representation of n//2 (integer division) plus 1 if n is odd. >>>numOnes(0) 0 >>>numOnes(1) 1 >>>numOnes(14) 3
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the...
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use at...
Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
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...
C++ write a function called divideBy that takes two integers as its input and returns the...
C++ write a function called divideBy that takes two integers as its input and returns the remainder. If the divisor is 0, the function should return -1, else it should return the remainder to the calling function.
Using Python Question 1 Write an input function. Then use it to supply the input to...
Using Python Question 1 Write an input function. Then use it to supply the input to following additional functions: i) Print multiplication table of the number from 1 to 12. ii) Print the sum of all the numbers from 1 to up the number given. iii) Print if the number supplied is odd or even. Question 2 Write function that asks for input from a user. If the user types 'end', the program exits, otherwise it just keeps going.
In Python Write a function to read a Sudoku board from an input string. The input...
In Python Write a function to read a Sudoku board from an input string. The input string must be exactly 81 characters long (plus the terminating null that marks the end of the string) and contains digits and dots (the `.` character represents an unmarked position). The input contains all 9 rows packed together. For example, a Sudoku board that looks like this: ``` ..7 ... ... 6.4 ... ..3 ... .54 ..2 ... .4. ... 9.. ... ..5 385...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT