Python 3.x 使用节点和树的单独类实现二叉树
我不熟悉Python和数据结构。在学习二叉树时,我发现所有可用的实现都在node类中有树的方法 但是我想以不同的方式实现它,其中除了节点类之外,还有一个二叉树类,它将包含二叉树的方法 这是我编写的代码,但它会导致传递给函数Python 3.x 使用节点和树的单独类实现二叉树,python-3.x,oop,data-structures,binary-tree,binary-search-tree,Python 3.x,Oop,Data Structures,Binary Tree,Binary Search Tree,我不熟悉Python和数据结构。在学习二叉树时,我发现所有可用的实现都在node类中有树的方法 但是我想以不同的方式实现它,其中除了节点类之外,还有一个二叉树类,它将包含二叉树的方法 这是我编写的代码,但它会导致传递给函数insertleft()和insertright()的参数出错 我不确定正确的语法应该是什么。提前感谢您的帮助定义函数时,您还可以定义将要接受的参数。但在类内部,只要方法不是静态的或类方法,第一个参数就应该是self或表示实例化对象本身的变量 在您的情况下,调用函数时,您已经在
insertleft()
和insertright()
的参数出错
我不确定正确的语法应该是什么。提前感谢您的帮助定义函数时,您还可以定义将要接受的参数。但在类内部,只要方法不是静态的或类方法,第一个参数就应该是self
或表示实例化对象本身的变量
在您的情况下,调用函数时,您已经在传递temp.left
和temp.right
的值。将函数定义更改为仅使用temp
应该可以正常工作。或者更易于阅读,节点
,因为这正是您实际所指的内容
您的insertleft
和insertright
函数执行与insert
相同的操作,不需要包含它们。只需修改insert
函数即可完成所有操作
最终结果应该是这样的:
class Node():
def __init__(self, arg):
self.left = None
self.right = None
self.data = arg
class Bt():
def __init__(self, root=None): #Added default value
self.root = Node(root)
def insert(self, data):
if self.root.data is None:
self.root.data = data #Set the root data if it doesn't exist.
else:
self._insert(self.root, data) #Prime the helper function with root node
def _insert(self, node, data): #Insert helper function to do the insertion
if data < node.data: #Check if data needs to be inserted on the left
if node.left is None: #Set left node if it doesn't exist
node.left = Node(data)
else: #Else let the left node decide where it goes as a child
self._insert(node.left, data)
else: #Else data needs to be inserted on the right
if node.right is None: #Set right node if it doesn't exist
node.right = Node(data)
else: #Else let the right node decide where it goes as a child
self._insert(node.right, data)
r = Bt(5)
r.insert(4)
r.insert(6)
class节点():
定义初始化(self,arg):
self.left=无
self.right=无
self.data=arg
类Bt():
def uuu init(self,root=None):#添加了默认值
self.root=节点(根)
def插入(自身,数据):
如果self.root.data为无:
self.root.data=data#如果根数据不存在,则设置根数据。
其他:
self._insert(self.root,data)#用根节点初始化helper函数
def_insert(self、node、data):#insert helper函数执行插入操作
如果数据
现在您只是缺少打印树和数据的函数。。。和访问数据。您正在类中定义函数。第一个参数应该是一个变量,它表示实例化的类对象it
self
。以下参数是对传入参数的引用。因此,在您的情况下,temp.left
应该只是temp
或node
。你们在逻辑上还有一些其他的问题……还有,你们似乎正在努力完成的事情。谢谢你们,我现在明白了。:)这是我的搜索节点实现:
def insertleft(temp.left, data):
^
SyntaxError: invalid syntax
class Node():
def __init__(self, arg):
self.left = None
self.right = None
self.data = arg
class Bt():
def __init__(self, root=None): #Added default value
self.root = Node(root)
def insert(self, data):
if self.root.data is None:
self.root.data = data #Set the root data if it doesn't exist.
else:
self._insert(self.root, data) #Prime the helper function with root node
def _insert(self, node, data): #Insert helper function to do the insertion
if data < node.data: #Check if data needs to be inserted on the left
if node.left is None: #Set left node if it doesn't exist
node.left = Node(data)
else: #Else let the left node decide where it goes as a child
self._insert(node.left, data)
else: #Else data needs to be inserted on the right
if node.right is None: #Set right node if it doesn't exist
node.right = Node(data)
else: #Else let the right node decide where it goes as a child
self._insert(node.right, data)
r = Bt(5)
r.insert(4)
r.insert(6)