从二叉树中删除一个节点及其所有子节点,而不使用树数据结构

Delete a node and all of its children from a binary tree, without using a tree data structure

假设我有一个数组作为输入,比如说

array = -1 0 0 1 2 1 3 5 5 6 6

(这实际上是二叉树的父数组表示)

在这种情况下树看起来像这样:

         0
       /   \
      1     2
     / \   /
    3   5 4
   /   / \
  6   7   8
 / \
9  10

我还有另一个输入,比如

input = 1

(这是要从树中删除的节点)

我想实现的基本是:

  1. 删除数组[输入]
  2. 在剩余数组中搜索输入变量
  3. 删除所有value = input的元素
  4. 存储删除值的索引
  5. 在剩余数组中搜索 value = indices
  6. 删除那些元素并存储它们的索引
  7. 从第 5 步开始重复,直到无法再删除为止

这是我想到的删除节点及其所有子节点的算法。

所以基本上对于上面给出的示例输入,输出应该是:

-1 0 2

我选择不使用树数据结构,因为我觉得从父数组构造树,然后搜索节点并删除它可能会更慢(尽管我可能是错的)。

虽然我无法在代码中实现这一点,但我有逻辑。每当我尝试编写伪代码时,我的逻辑中的一个或多个问题就会变得明显。这就是为什么我在这里还没有 post 的具体代码。所以我不知道这需要多长时间。

这是一个 class 作业的问题,只使用树数据结构就可以了,但如果可能的话,我想在这种情况下实施更好的解决方案。

感谢任何帮助。不需要给我完整的代码。只需一些指示就足够了。

这里是你的算法的一个稍微修改版本的实现。

不同的是,我们不是一层一层地清洗,而是一次性全部清洗。这利用了 children 始终位于其 parent 右侧的事实,因此如果我们只浏览一次列表并标记要删除的所有内容,我们就不会遗漏任何内容。

这使得树处于非规范状态,因为移除的节点仍然存在,它们只是被标记为移除(通过将它们的 parent 设置为 -2)。

因此我还添加了一个可选的压缩步骤,删除这些节点并重新编号剩余的节点,以便剩余的节点编号为 0、1、2 ...,没有间隙。

from itertools import islice, count

def delete_branch_from_tree(parents, branch_root,
                            compress=False, inplace=False):
    n = len(parents)
    if not inplace: # make a copy
        parents = parents[:]
    if branch_root == 0:
        parents[:] = [] if compress else n * [-2]
        return parents
    # -2 will indicate missing nodes
    parents[branch_root] = -2
    for node in range(branch_root+1, n):
        if parents[parents[node]] == -2:
            parents[node] = -2
    if compress: # remove nodes marked as missing, renumber the other ones
        c = count()
        new_number = [None if parent==-2 else next(c) for parent in parents]
        parents[:] = [new_number[parent] for parent in parents if parent != -2]
        # -1 was not in the lookup table (new_number), must fix manually
        parents[0] = -1
    return parents

演示:

>>> tree = [-1, 0, 0, 1, 2, 1, 3, 5, 5, 6, 6]
>>> 
>>> delete_branch_from_tree(tree, 1)
[-1, -2, 0, -2, 2, -2, -2, -2, -2, -2, -2]
>>> delete_branch_from_tree(tree, 1, compress=True)
[-1, 0, 1]
>>> delete_branch_from_tree(tree, 5)
[-1, 0, 0, 1, 2, -2, 3, -2, -2, 6, 6]
>>> delete_branch_from_tree(tree, 5, compress=True)
[-1, 0, 0, 1, 2, 3, 5, 5]