Question

In: Computer Science

Python Explain Code #Python program class TreeNode:

Python Explain Code

 

#Python program

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


def findMaxDifference(root, diff=float('-inf')):
    if root is None:
        return float('inf'), diff

    leftVal, diff = findMaxDifference(root.left, diff)
    rightVal, diff = findMaxDifference(root.right, diff)

    currentDiff = root.key - min(leftVal, rightVal)

    diff = max(diff, currentDiff)

    return min(min(leftVal, rightVal), root.key), diff

root = TreeNode(6)
root.left = TreeNode(3)
root.right = TreeNode(8)
root.right.left = TreeNode(2)
root.right.right = TreeNode(4)
root.right.left.left = TreeNode(1)
root.right.left.right = TreeNode(7)

print(findMaxDifference(root)[1])

Solutions

Expert Solution

Given program finds maximum difference between a Node and its Decendents in Binary tree.

This program solve the problem in linear time by processing the tree nodes in bottom up manner.
It visits left and right subtree before processing current tree node.

The function findMaxDifference() returns minimum value among all nodes in sub-tree rooted at it.

For a node, we get minimum value in left subtree and minimum value in right subtree .

And find the maximum difference for every node and if difference is more then maximum difference calculated so far, it update it.

#Tree Node class stores a Binary tree Node
class TreeNode:
    def __init__(self, key):
        self.key = key
        self.left = None
        self.right = None

#This function finds maximum difference between a node and its descendants
def findMaxDifference(root, diff=float('-inf')):
    # if tree is empty return infinity
    if root is None:
        return float('inf'), diff

    #recursively called on left and right sub tree
    leftVal, diff = findMaxDifference(root.left, diff)
    rightVal, diff = findMaxDifference(root.right, diff)
  
    # find maximum difference between current node and its descendants
    currentDiff = root.key - min(leftVal, rightVal)

    # update the maximum difference
    diff = max(diff, currentDiff)
  
    # return minimum value among all nodes in sub-tree rooted at it
    return min(min(leftVal, rightVal), root.key), diff

root = TreeNode(6)
root.left = TreeNode(3)
root.right = TreeNode(8)
root.right.left = TreeNode(2)
root.right.right = TreeNode(4)
root.right.left.left = TreeNode(1)
root.right.left.right = TreeNode(7)

print(findMaxDifference(root)[1])


Related Solutions

Explain this python program as if you were going to present it to a class in...
Explain this python program as if you were going to present it to a class in a power point presentation. How would you explain it? I am having a hard time with this. I have my outputs and code displayed throughout 9 slides. #Guess My Number Program # Python Code is modified to discard duplicate guesses by the computer import random #function for getting the user input on what they want to do. def menu(): #print the options print("\n\n1. You...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
In python make a simple code. You are writing a code for a program that converts...
In python make a simple code. You are writing a code for a program that converts Celsius and Fahrenheit degrees together. The program should first ask the user in which unit they are entering the temperature degree (c or C for Celcius, and f or F for Fahrenheit). Then it should ask for the temperature and call the proper function to do the conversion and display the result in another unit. It should display the result with a proper message....
(CODE IN PYTHON) Program Input: Your program will display a welcome message to the user and...
(CODE IN PYTHON) Program Input: Your program will display a welcome message to the user and a menu of options for the user to choose from. Welcome to the Email Analyzer program. Please choose from the following options: Upload text data Find by Receiver Download statistics Exit the program Program Options Option 1: Upload Text Data If the user chooses this option, the program will Prompt the user for the file that contains the data. Read in the records in...
Please write in beginner level PYTHON code! Your job is to write a Python program that...
Please write in beginner level PYTHON code! Your job is to write a Python program that asks the user to make one of two choices: destruct or construct. - If the user chooses to destruct, prompt them for an alternade, and then output the 2 words from that alternade. - If the user chooses construct, prompt them for 2 words, and then output the alternade that would have produced those words. - You must enforce that the users enter real...
Please add to this Python, Guess My Number Program. Add code to the program to make...
Please add to this Python, Guess My Number Program. Add code to the program to make it ask the user for his/her name and then greet that user by their name. Please add code comments throughout the rest of the program If possible or where you add in the code to make it ask the name and greet them before the program begins. import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the...
The code that creates this program using Python: Your program must include: You will generate a...
The code that creates this program using Python: Your program must include: You will generate a random number between 1 and 100 You will repeatedly ask the user to guess a number between 1 and 100 until they guess the random number. When their guess is too high – let them know When their guess is too low – let them know If they use more than 5 guesses, tell them they lose, you only get 5 guesses. And stop...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT