2

++ EDIT ++

Thanks to all of you, I have taken many compartments to get the code to finally working.

Here's the full code overview

class Node:
# Binary Roots: Left and Right, initiating data
def __init__(self, data):
    # Creating an object, Data, to be used for inputs
    self.data = data
    # The objects are defined as None, which creates an empty space for input
    self.left = None
    self.right = None

Define Binary Tree Class:

class BinaryTree:

# Defining Roots as Nodes, initiating at the same time.
def __init__(self, rootdata):
    # Declaring roots as new nodes which uses root data
    self.root = Node(rootdata)

Defining FindMax, which will find the maximum value in a binary tree, then return the maximum.

def FindMax(root):

# ???
if (root == None):
    return float('-inf')

# In this function, it will search for the maximum of three values, the roots, and maximums from left and right leaves.

# Initialization of 3 things: the maximum of root, and two maximums of left and right leaves
letsfind = root.data
lfound = FindMax(root.left)
rfound = FindMax(root.right)

# If a maximum is found on the left, new maximum is the value.
if(lfound > letsfind):
    letsfind = lfound

# If a maximum is found on the right, new maximum is the value.
if(rfound > letsfind):
    letsfind = rfound

# Return the maximum value found.
return letsfind

???

if name == 'main':

# The Inputs of the Binary Tree and leaves.
# root (Top most), root.left (Left Leaf), root.right (Right Leaf)
root = Node(2)
root.left     = Node(7)  
root.right     = Node(5)  
root.left.right = Node(6)  
root.left.right.left= Node(1)  
root.left.right.right= Node(11)  
root.right.right= Node(9)  
root.right.right.left= Node(4)  

# Print the Maximum
print("The Maximum Value in the Binary Tree is: ", FindMax(root))
    

I know it seems long, so I apologize. I have taken account that the function 'FindMax' needs to be outside of the class, in order for it to run properly.

One more thing though, what is the purpose of the statement if __name__ == '__main__':?

And What is the if (root == None): return float('-inf') Really for?

Thanks a lot guys. I appreciate it! :)

Steven H
  • 19
  • 1
  • 5

5 Answers5

0

Option 1

The problem is here

st = start.data
lis = start.left
ris = start.right

while you actually call for the data node on st. The other ones (lis and ris) are only been call as Nodes. You should change it with

st = start.data
lis = (start.left).data
ris = (start.right).data

to be reading the data from all nodes

Option 2

You override > for your node class.

Funny but a not for a rookie. If you are interested start reading this

bench
  • 136
  • 8
  • This is what I have at the bottom of the line, ' print(Tree.FindMax(Tree.root)) ' – Steven H Oct 08 '20 at 03:16
  • and it says that name 'FindMax' not defined – Steven H Oct 08 '20 at 03:17
  • You should send all the class to help you – bench Oct 08 '20 at 18:08
  • You should send all the class to help you. But if you are defining `fmax` inside `Node`, you should call it as `self.fmax()`, otherwise you are calling a global function `fmax` that i think you don't have. If this is your case @Raymond C. answer should help you – bench Oct 08 '20 at 18:14
0
lis = FindMax(start.left) 
ris = FindMax(start.right) 

You forgot to call the recursive search

Rockcat
  • 3,002
  • 2
  • 14
  • 28
0

This is the correct code:

def findMax(start): 
      
    # Base case  
    if start is None:  
        return float('-inf') 

    st = start.data 
    lis = findMax(start.left)  
    ris = findMax(start.right)  
    
    if (lis > st): 
        st = lis  
    if (ris > st):  
        st = ris  
    
    return st

and call findMax() now


Tree=BinaryTree("1")
Tree.root.left=Node("2")
Tree.root.right=Node("3")
Tree.root.left.left=Node("4")
Tree.root.left.right=Node("5")
Tree.root.right.left=Node("6")
Tree.root.right.right=Node("7")
Tree.root.right.right.right=Node("8")
print("Maximum element is",  findMax(start)) 
Raymond C.
  • 572
  • 4
  • 24
0

Note - There are a few exceptions to my answer:

  • It may contain errors as OP did not provide the BinaryTree class.
  • Does not take into account any logical issues.
  • It is focused on getting OP's code working.

Here are the errors that I spotted:

  • Python is a language which requires proper indentation. Indentation is vital in-order for code to function properly. It appears that the way you have indented your code is incorrect.
  • When you are trying to call a function inside a class, you would need to make the call either via an instance (self) or the class itself (classmethods). Explanation provided here.
  • Explanation of if __name__ == "__main__": here. It is not really required as I assume your code is a single module (in one Python file) but is good to have when working with multiple modules.

Based on the points I have mentioned, I have corrected your code accordingly as follows:

# Any required imports goes here...
# import XXX


class BinaryTree:
    # OP did not provide code sample...


class Node:
    def __init__(self, data):

        # Creating a node
        self.data = data

        # Pointing either left or right, but it is empty in default
        self.left = None
        self.right = None

    def fmax(self, start):
        
        if start is None:
            return 0

        st = start.data

        lis = self.fmax(start.left)
        ris = self.fmax(start.right)

        if (lis > st):
            st = lis
        if (ris > st):
            st = ris

        return st


if __name__ == "__main__":

    Tree = BinaryTree("1")
    Tree.root.left = Node("2")
    Tree.root.right = Node("3")
    Tree.root.left.left = Node("4")
    Tree.root.left.right = Node("5")
    Tree.root.right.left = Node("6")
    Tree.root.right.right = Node("7")
    Tree.root.right.right.right = Node("8")

    print(Tree.fmax(Tree.root))

If anyone spots any error with my code please feel free to edit it. My idea is to get OP a working code and he can expand from there.

Raymond C.
  • 572
  • 4
  • 24
0
def findMax(root): 
      
    if (root == None):  
        return float('-inf') 

    res = root.data 
    lres = findMax(root.left)  
    rres = findMax(root.right)  
    if (lres > res): 
        res = lres  
    if (rres > res):  
        res = rres  
    return res 
  
if __name__ == '__main__': 
    root = newNode(2)  
    root.left = newNode(7)  
    root.right = newNode(5)  
    root.left.right = newNode(6)  
    root.left.right.left = newNode(1)  
    root.left.right.right = newNode(11)  
    root.right.right = newNode(9)  
    root.right.right.left = newNode(4)  
  
    print("Maximum element is", findMax(root)) 

#Output

Maximum element is 11