Python 正在尝试向我的类中添加预排序遍历方法 类节点: def u u init u u;(self,value=None): 自我价值=价值 self.left\u child=无 self.right\u child=无 self.parent=None 类二进制搜索树: 定义初始化(自): self.root=None def插入(自身,值): 如果self.root==无: self.root=节点(值) 其他: self.\u插入(值,self.root) 定义插入(自身、值、当前节点): 如果valuecur_node.value: 如果cur\u node.right\u child==无: cur\u node.right\u child=节点(值) cur\u node.right\u child.parent=cur\u node 其他: self.\u insert(值,当前节点.右\u子节点) 其他: 打印(“值已在树中!”) def打印树(自身): 如果你是我的根=无: self.\u打印树(self.root) 定义打印树(自身、当前节点): 如果是cur_节点=无: self.\u print\u树(cur\u节点.左\u子节点) 打印(str(当前节点值)) self.\u print\u树(cur\u node.right\u child) def高度(自身): 如果你是我的根=无: 返回self.\u高度(self.root,0) 其他: 返回0 定义高度(自身、当前节点、当前高度): 如果cur\u节点==无:返回cur\u高度 左\u高度=自身高度(当前节点。左\u子节点,当前高度+1) 右\u高度=自身高度(当前节点。右\u子节点,当前高度+1) 返回最大值(左高、右高) def查找(自我,值): 如果你是我的根=无: 返回self.\u查找(值,self.root) 其他: 一无所获 定义查找(自身、值、当前节点): 如果value==cur_node.value: 返回cur_节点 elif valuecur_node.value和cur_node.right_子项=无: 返回self.\u find(值,当前节点.右\u子节点) def预订单(自): 打印(自我价值) 如果self.left\u子项: self.left_child.preorder() 如果self.right\u儿童: self.right\u child.preorder() tree=二进制搜索树() 树.插入(21) 树.插入(26) 树.插入(30) 树.插入(9) 打印(树。预订单)

Python 正在尝试向我的类中添加预排序遍历方法 类节点: def u u init u u;(self,value=None): 自我价值=价值 self.left\u child=无 self.right\u child=无 self.parent=None 类二进制搜索树: 定义初始化(自): self.root=None def插入(自身,值): 如果self.root==无: self.root=节点(值) 其他: self.\u插入(值,self.root) 定义插入(自身、值、当前节点): 如果valuecur_node.value: 如果cur\u node.right\u child==无: cur\u node.right\u child=节点(值) cur\u node.right\u child.parent=cur\u node 其他: self.\u insert(值,当前节点.右\u子节点) 其他: 打印(“值已在树中!”) def打印树(自身): 如果你是我的根=无: self.\u打印树(self.root) 定义打印树(自身、当前节点): 如果是cur_节点=无: self.\u print\u树(cur\u节点.左\u子节点) 打印(str(当前节点值)) self.\u print\u树(cur\u node.right\u child) def高度(自身): 如果你是我的根=无: 返回self.\u高度(self.root,0) 其他: 返回0 定义高度(自身、当前节点、当前高度): 如果cur\u节点==无:返回cur\u高度 左\u高度=自身高度(当前节点。左\u子节点,当前高度+1) 右\u高度=自身高度(当前节点。右\u子节点,当前高度+1) 返回最大值(左高、右高) def查找(自我,值): 如果你是我的根=无: 返回self.\u查找(值,self.root) 其他: 一无所获 定义查找(自身、值、当前节点): 如果value==cur_node.value: 返回cur_节点 elif valuecur_node.value和cur_node.right_子项=无: 返回self.\u find(值,当前节点.右\u子节点) def预订单(自): 打印(自我价值) 如果self.left\u子项: self.left_child.preorder() 如果self.right\u儿童: self.right\u child.preorder() tree=二进制搜索树() 树.插入(21) 树.插入(26) 树.插入(30) 树.插入(9) 打印(树。预订单),python,Python,所以我有一个二叉搜索树类,它可以正常工作,但是我在我的类中添加了一个前序遍历方法,这不起作用。有人知道我如何修改此方法使其工作吗?我正在寻找它只是打印出预订单列表。我应该将根或值作为参数添加到方法中吗?我只是希望能够将它进行预排序遍历并打印出来,这就是我真正需要的。预排序只需更改\u print\u tree()中语句的顺序即可。。大概是这样的: class node: def __init__(self,value=None): self.value=value

所以我有一个二叉搜索树类,它可以正常工作,但是我在我的类中添加了一个前序遍历方法,这不起作用。有人知道我如何修改此方法使其工作吗?我正在寻找它只是打印出预订单列表。我应该将根或值作为参数添加到方法中吗?我只是希望能够将它进行预排序遍历并打印出来,这就是我真正需要的。

预排序只需更改
\u print\u tree()中语句的顺序即可。
。大概是这样的:

class node:
    def __init__(self,value=None):
        self.value=value
        self.left_child=None
        self.right_child=None
        self.parent=None 

class binary_search_tree:
    def __init__(self):
        self.root=None
    def insert(self,value):
        if self.root==None:
            self.root=node(value)
        else:
            self._insert(value,self.root)

    def _insert(self,value,cur_node):
        if value<cur_node.value:
            if cur_node.left_child==None:
                cur_node.left_child=node(value)
                cur_node.left_child.parent=cur_node 
            else:
                self._insert(value,cur_node.left_child)
        elif value>cur_node.value:
            if cur_node.right_child==None:
                cur_node.right_child=node(value)
                cur_node.right_child.parent=cur_node 
            else:
                self._insert(value,cur_node.right_child)
        else:
            print("Value already in tree!")

    def print_tree(self):
        if self.root!=None:
            self._print_tree(self.root)

    def _print_tree(self,cur_node):
        if cur_node!=None:
            self._print_tree(cur_node.left_child)
            print (str(cur_node.value))
            self._print_tree(cur_node.right_child)

    def height(self):
        if self.root!=None:
            return self._height(self.root,0)
        else:
            return 0

    def _height(self,cur_node,cur_height):
        if cur_node==None: return cur_height
        left_height=self._height(cur_node.left_child,cur_height+1)
        right_height=self._height(cur_node.right_child,cur_height+1)
        return max(left_height,right_height)

    def find(self,value):
        if self.root!=None:
            return self._find(value,self.root)
        else:
            return None

    def _find(self,value,cur_node):
        if value==cur_node.value:
            return cur_node
        elif value<cur_node.value and cur_node.left_child!=None:
            return self._find(value,cur_node.left_child)
        elif value>cur_node.value and cur_node.right_child!=None:
            return self._find(value,cur_node.right_child)



    def preorder(self):
        print(self.value)
        if self.left_child:
            self.left_child.preorder()
        if self.right_child:
            self.right_child.preorder()


tree = binary_search_tree()
tree.insert(21)
tree.insert(26)
tree.insert(30)
tree.insert(9)
print(tree.preorder)
并像
tree.preorder()那样调用它


我强烈建议您学习理论或有序树遍历:

预排序只需更改
\u print\u tree()
中语句的顺序即可。大概是这样的:

class node:
    def __init__(self,value=None):
        self.value=value
        self.left_child=None
        self.right_child=None
        self.parent=None 

class binary_search_tree:
    def __init__(self):
        self.root=None
    def insert(self,value):
        if self.root==None:
            self.root=node(value)
        else:
            self._insert(value,self.root)

    def _insert(self,value,cur_node):
        if value<cur_node.value:
            if cur_node.left_child==None:
                cur_node.left_child=node(value)
                cur_node.left_child.parent=cur_node 
            else:
                self._insert(value,cur_node.left_child)
        elif value>cur_node.value:
            if cur_node.right_child==None:
                cur_node.right_child=node(value)
                cur_node.right_child.parent=cur_node 
            else:
                self._insert(value,cur_node.right_child)
        else:
            print("Value already in tree!")

    def print_tree(self):
        if self.root!=None:
            self._print_tree(self.root)

    def _print_tree(self,cur_node):
        if cur_node!=None:
            self._print_tree(cur_node.left_child)
            print (str(cur_node.value))
            self._print_tree(cur_node.right_child)

    def height(self):
        if self.root!=None:
            return self._height(self.root,0)
        else:
            return 0

    def _height(self,cur_node,cur_height):
        if cur_node==None: return cur_height
        left_height=self._height(cur_node.left_child,cur_height+1)
        right_height=self._height(cur_node.right_child,cur_height+1)
        return max(left_height,right_height)

    def find(self,value):
        if self.root!=None:
            return self._find(value,self.root)
        else:
            return None

    def _find(self,value,cur_node):
        if value==cur_node.value:
            return cur_node
        elif value<cur_node.value and cur_node.left_child!=None:
            return self._find(value,cur_node.left_child)
        elif value>cur_node.value and cur_node.right_child!=None:
            return self._find(value,cur_node.right_child)



    def preorder(self):
        print(self.value)
        if self.left_child:
            self.left_child.preorder()
        if self.right_child:
            self.right_child.preorder()


tree = binary_search_tree()
tree.insert(21)
tree.insert(26)
tree.insert(30)
tree.insert(9)
print(tree.preorder)
并像
tree.preorder()那样调用它


我强烈建议您学习理论或有序树遍历:

问题在于,您正在混合和匹配两种方法来组织递归

方法1:

def preorder(self):
    if self.root!=None:
        self._preorder(self.root)

def _preorder(self,cur_node):
    if cur_node!=None:
        print (str(cur_node.value))
        self._preorder(cur_node.left_child)
        self._preorder(cur_node.right_child)
这里的一般逻辑是递归调用回调同一个树对象,但将关于操作哪个节点的信息作为参数传递。这将逻辑排除在节点类之外,节点类只是普通数据存储

方法2:

def _insert(self,value,cur_node):
    # skipping logic, until we get to a recursive call:
            self._insert(value,cur_node.left_child)
这试图通过委托给节点实例来实现递归,这要求它们具有行为。整个代码块作为节点方法是有意义的,因为它是具有
left\u child
right\u child
属性(可从
self
访问)的节点。如果您想采用这种方法,那么该代码将进入
节点
类,然后在
二进制搜索树
中,您只需要委托给根节点:

def preorder(self):
    # the recursive calls look instead like this:
        self.left_child.preorder()
无论哪种方法,如果你选择一种方法并坚持下去都会更好:要么

  • 将树类委托给根节点,并为节点提供执行递归所需的功能;或

  • 通过跟踪“当前”节点,让树类处理整个递归。(您还可以轻松地将此方法转换为迭代。)


问题在于,您正在混合和匹配两种方法来组织递归

方法1:

def preorder(self):
    if self.root!=None:
        self._preorder(self.root)

def _preorder(self,cur_node):
    if cur_node!=None:
        print (str(cur_node.value))
        self._preorder(cur_node.left_child)
        self._preorder(cur_node.right_child)
这里的一般逻辑是递归调用回调同一个树对象,但将关于操作哪个节点的信息作为参数传递。这将逻辑排除在节点类之外,节点类只是普通数据存储

方法2:

def _insert(self,value,cur_node):
    # skipping logic, until we get to a recursive call:
            self._insert(value,cur_node.left_child)
这试图通过委托给节点实例来实现递归,这要求它们具有行为。整个代码块作为节点方法是有意义的,因为它是具有
left\u child
right\u child
属性(可从
self
访问)的节点。如果您想采用这种方法,那么该代码将进入
节点
类,然后在
二进制搜索树
中,您只需要委托给根节点:

def preorder(self):
    # the recursive calls look instead like this:
        self.left_child.preorder()
无论哪种方法,如果你选择一种方法并坚持下去都会更好:要么

  • 将树类委托给根节点,并为节点提供执行递归所需的功能;或

  • 通过跟踪“当前”节点,让树类处理整个递归。(您还可以轻松地将此方法转换为迭代。)


您能否更好地描述当前行为而不是“它不工作”?调用
self.left\u child.preorder()
不起作用,因为这两个子属性是
node
实例,它没有这样的方法。你有一个明显的工作顺序遍历