None: """ 将项目插入此BST。 """ #找到插入点。 父级,当前=无,self.root 当前: 如果项目当前项目 父级,当前=当前,当前。右侧 #创建新节点并将其适当链接。 新节点=\u节点(项) 如果是家长: 如果项目parent.item parent.right=新节点 其他: self.root=新节点,python,binary-search-tree,Python,Binary Search Tree" /> None: """ 将项目插入此BST。 """ #找到插入点。 父级,当前=无,self.root 当前: 如果项目当前项目 父级,当前=当前,当前。右侧 #创建新节点并将其适当链接。 新节点=\u节点(项) 如果是家长: 如果项目parent.item parent.right=新节点 其他: self.root=新节点,python,binary-search-tree,Python,Binary Search Tree" />

python中的BST问题 BST级: “”“二元搜索树。”“” def uuu init uuuu(self:'BST',container:list=None)->None: """ 通过从容器中插入项来初始化此BST(默认值[]) 一个接一个,按照给定的顺序。 """ #初始化空树。 self.root=None #插入容器中的每个项目。 如果是集装箱: 对于容器中的项目: 自行插入(项目) def insert(self:'BST',item:object)->None: """ 将项目插入此BST。 """ #找到插入点。 父级,当前=无,self.root 当前: 如果项目当前项目 父级,当前=当前,当前。右侧 #创建新节点并将其适当链接。 新节点=\u节点(项) 如果是家长: 如果项目parent.item parent.right=新节点 其他: self.root=新节点

python中的BST问题 BST级: “”“二元搜索树。”“” def uuu init uuuu(self:'BST',container:list=None)->None: """ 通过从容器中插入项来初始化此BST(默认值[]) 一个接一个,按照给定的顺序。 """ #初始化空树。 self.root=None #插入容器中的每个项目。 如果是集装箱: 对于容器中的项目: 自行插入(项目) def insert(self:'BST',item:object)->None: """ 将项目插入此BST。 """ #找到插入点。 父级,当前=无,self.root 当前: 如果项目当前项目 父级,当前=当前,当前。右侧 #创建新节点并将其适当链接。 新节点=\u节点(项) 如果是家长: 如果项目parent.item parent.right=新节点 其他: self.root=新节点,python,binary-search-tree,Python,Binary Search Tree,这是我为BST类构建的代码,我想实现一个max_node函数,该函数不使用递归查找最大节点,我该怎么做呢?在a中,最大节点就是最右边的节点,因此,从头部开始,一直选取正确的子节点,直到找到没有正确子节点的节点为止。这可以很容易地以迭代的方式完成(事实上,我可能就是这样做的) 在伪代码中: class BST: """A Binary Search Tree.""" def __init__(self: 'BST', container: list =None) -> No

这是我为BST类构建的代码,我想实现一个max_node函数,该函数不使用递归查找最大节点,我该怎么做呢?

在a中,最大节点就是最右边的节点,因此,从头部开始,一直选取正确的子节点,直到找到没有正确子节点的节点为止。这可以很容易地以迭代的方式完成(事实上,我可能就是这样做的)

在伪代码中:

class BST:
    """A Binary Search Tree."""

    def __init__(self: 'BST', container: list =None) -> None:
        """
        Initialize this BST by inserting the items from container (default [])
        one by one, in the order given.
        """
        # Initialize empty tree.
        self.root = None
        # Insert every item from container.
        if container:
            for item in container:
                self.insert(item)

   def insert(self: 'BST', item: object) -> None:
    """
    Insert item into this BST.
    """
    # Find the point of insertion.
    parent, current = None, self.root
    while current:
        if item < current.item:
            parent, current = current, current.left
        else:  # item > current.item
            parent, current = current, current.right
    # Create a new node and link it in appropriately.
    new_node = _BSTNode(item)
    if parent:
        if item < parent.item:
            parent.left = new_node
        else:  # item > parent.item
            parent.right = new_node
    else:
        self.root = new_node
最大节点=头部 而hasRightChild(最大节点) max\u node=max\u node.right\u子节点;
为什么不使用递归就要这样做?这棵树有多大? max_node = head while hasRightChild(max_node) max_node = max_node.right_child;