++ 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! :)