Question

In: Computer Science

Python: Create two Binary Tree class methods that can return the maximum value within a Binary...

Python:

Create two Binary Tree class methods that can return the maximum value within a Binary Tree and the minimum value within a Binary Tree.

Test the methods in your code

Example syntax to call these methods:

MyTree.Max()

MyTree.Min()

Solutions

Expert Solution

Please refer to the below python code for the above stated problem:

Please refer to the comments in the code snippet for better understanding.

# A class to create a new node 
class newNodeCreate: 
        def __init__(self, data): 
                self.data = data 
                self.left = self.right = None                 #defining the left and the right nodes of the root
        
# Returns maximum value in a  
def findMaxValue(root): 
        
        # Base case where the binary tree might be empty
        if (root == None): 
                return float('-inf') 

        # Return maximum of 3 values: 
        # 1) Root's value 2) Max in Left Subtree 
        # 3) Max in right subtree 
        rootdata = root.data 
        left1 = findMaxValue(root.left) 
        right1 = findMaxValue(root.right) 
        if (left1 > rootdata): 
                rootdata = left1 
        if (right1 > rootdata): 
                rootdata = right1 
        return rootdata

# Returns the min value in a binary tree 
def findMinValue(root): 
    if root is None:                # Base case where the binary tree might be empty
        return float('inf') 
  
    # Return minimum of 3 values: 
        # 1) Root's value 2) Min in Left Subtree 
        # 3) Min in right subtree 

    rootdata = root.data 
    left1 = findMinValue(root.left) 
    right1 = findMinValue(root.right) 
    if left1 < rootdata: 
        rootdata = left1 
    if right1 < rootdata: 
        rootdata = right1 
    return rootdata 


# Code to define the starting binary tree (Driver Code) and find the max and min values
if __name__ == '__main__':                                  # this defines the first piece of code that runs when we run the python module
        root = newNodeCreate(2) 
        root.left        = newNodeCreate(7) 
        root.right       = newNodeCreate(5) 
        root.left.right = newNodeCreate(6) 
        root.left.right.left=newNodeCreate(1) 
        root.left.right.right=newNodeCreate(11) 
        root.right.right=newNodeCreate(9) 
        root.right.right.left=newNodeCreate(4) 


        #testing the function findMaxValue the returned value should be 11 according to the above created tree
        print("Maximum element is", 
                                findMaxValue(root))
        #testing the function findMinValue the returned value should be 1 according to the above created tree
        print("Minimum element is", 
                                findMinValue(root))

Related Solutions

Creat a python method for a binary tree class that has an inputted lookup value and...
Creat a python method for a binary tree class that has an inputted lookup value and will output if the lookup values exist within the Binary Tree by returning True or False. Exists within the binary tree -True. No exist within the binary tree - false.Test the method in your code
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.
PYTHON CODING Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary...
PYTHON CODING Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary Search Tree in ascending or deceasing order. The order type is an input to the method and can be "ascending" or "descending". The ascending input would return the node values of the tree beginning with the smallest and ending with the largest, descending returns the opposite. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly identify...
PYTHON CODING Using the structural node and methods discussed in Binary Search Tree below # Binary...
PYTHON CODING Using the structural node and methods discussed in Binary Search Tree below # Binary Tree Node structure class Node: # Constructor to create a new node def __init__(self, data): self.data = data self.left = None self.right = None class BSTree(): def __init__(self, rootdata): self.root = Node(rootdata)    def insert(self, data, cur_node): if data < cur_node.data: if cur_node.left == None: cur_node.left = Node(data) else: self.insert(data, cur_node.left)    elif data > cur_node.data: if cur_node.right == None: cur_node.right = Node(data) else:...
PYTHON CODING Create a method for a Binary Search tree that finds the lowest common ancestor...
PYTHON CODING Create a method for a Binary Search tree that finds the lowest common ancestor of two nodes in a tree (nodesLCA). The two nodes are input by the user identified by their values. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly identify its purpose.Illustrate the performance of the nodesLCA method. For the BST of datalist excute the method on following pairs: (500, 271), (21, 203) and (53 , 991)...
PYTHON CODING Create a method for a Binary Search tree that finds the lowest common ancestor...
PYTHON CODING Create a method for a Binary Search tree that finds the lowest common ancestor of two nodes in a tree (nodesLCA). The two nodes are input by the user identified by their values. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly identify its purpose. Illustrate the performance of the nodesLCA method. For the BST of datalist excute the method on following pairs: (500, 271), (21, 203) and (53 ,...
PYTHON CODING Create a method for the Binary Search Tree (deleteNode) that deletes a specified node...
PYTHON CODING Create a method for the Binary Search Tree (deleteNode) that deletes a specified node identified by its value, and rearranges the descendants of the deleted node to ensure the resulting Tree meets the requirements of a Binary Search Tree. a) Discuss and justify your approach to address each possible case. b) Is the new tree (with the deleted node removed) unique? Discuss your answer. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or...
PYTHON CODING Create a method for the Binary Search Tree (deleteNode) that deletes a specified node...
PYTHON CODING Create a method for the Binary Search Tree (deleteNode) that deletes a specified node identified by its value, and rearranges the descendants of the deleted node to ensure the resulting Tree meets the requirements of a Binary Search Tree. a) Discuss and justify your approach to address each possible case. b) Is the new tree (with the deleted node removed) unique? Discuss your answer. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or...
In java, Finding the maximum value in a BST (Binary Search Tree). Students need to write...
In java, Finding the maximum value in a BST (Binary Search Tree). Students need to write the code.
This is using Python, it is utilizing code from a Fraction class to create a Binary...
This is using Python, it is utilizing code from a Fraction class to create a Binary Class Please leave comments so I may be able to learn from this. Instruction for Binary Class: Exercise 6.18: Design an immutable class BinaryNumber, in the style of our Fraction class. Internally, your only instance variable should be a text string that represents the binary value, of the form '1110100'. Implement a constructor that takes a string parameter that specifies the original binary value....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT