问题描述
++编辑++
感谢大家,我花了很多时间来使代码最终生效。
这是完整的代码概述
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
定义二叉树类:
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)
定义FindMax,它将在二叉树中找到最大值,然后返回最大值。
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
???
如果名称 =='主要':
# 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))
我知道这似乎很长,因此我深表歉意。 我考虑到函数'FindMax'必须在类之外,才能正常运行。
不过,if __name__ == '__main__':
语句的目的是什么?
if (root == None): return float('-inf')
的真正作用是什么?
非常感谢你们。我很感激! :)
解决方法
选项1
问题在这里
st = start.data
lis = start.left
ris = start.right
您实际上在st
上调用数据节点时。其他的lis
和ris
仅被称为Nodes
。您应该使用
st = start.data
lis = (start.left).data
ris = (start.right).data
要从所有节点读取数据
选项2
您为节点类覆盖>
。
有趣,但不是新秀。如果您有兴趣,请开始阅读this
,lis = FindMax(start.left)
ris = FindMax(start.right)
您忘记了调用递归搜索
,这是正确的代码:
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
并立即致电findMax()
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))
,
注意-我的回答有一些例外:
- 它可能包含错误,因为OP没有提供BinaryTree类。
- 不考虑任何逻辑问题。
- 它专注于使OP的代码正常工作。
这是我发现的错误:
- Python是一种需要适当缩进的语言。缩进对于代码正常运行至关重要。似乎您缩进代码的方式不正确。
- 当您尝试在类内部调用函数时,您将需要通过实例(自身)或类本身(类方法)进行调用。 Explanation provided here.
- 说明
if __name__ == "__main__":
here。因为我假设您的代码是一个模块(在一个Python文件中),所以它并不是真正需要的,但是在使用多个模块时很好。
根据我提到的观点,我已相应地更正了您的代码,如下所示:
# 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))
如果有人发现我的代码有任何错误,请随时对其进行编辑。我的想法是让OP正常工作,他可以从那里扩展。
,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
最大元素为11