小编Fre*_*obs的帖子

查找二叉堆中的最后一个节点

该堆由 BinaryNode 类支持,该类存储指向父级、左子级和右子级的指针。没有使用任何数组。

我正在尝试找到最深最右边的节点。我认为下面的代码可以工作,但它只检查正确的子树,因为我从右边开始。例如,在下面的两个堆中,如果最后一个节点位于 5、6 或 5 和 6 槽中,则此方法有效。如果这两个为空,它将返回槽 2 而不是槽 4 中的节点。

      0                     0
     / \                   / \  
    /   \                 /   \
   1     2               1     2
  / \   / \             / \    
 3   4 5   6           3   4 

# ---------- Find the last element ----------

def _find_last(self, node):           # PARAMETER node: the root of the tree
    # Start by going right

    if self._has_right(node):         # _has_right takes a node and returns True if there is a node

        node = …
Run Code Online (Sandbox Code Playgroud)

heap python-3.x

1
推荐指数
1
解决办法
1812
查看次数

标签 统计

heap ×1

python-3.x ×1