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

标签 python arrays python-2.7 tree binary-tree

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

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

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

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

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

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

最佳答案

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

不同的是,我们不是一层一层地清洗,而是一次性全部清洗。这利用了这样一个事实,即 child 总是在 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]

关于python - 从二叉树中删除一个节点及其所有子节点,而不使用树数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49618983/

相关文章:

python - 从 oauth2client.contrib.appengine 导入 AppAssertionCredentials 导入错误 : No module named appengine

java - 显示两个数组中不属于两个数组的元素

python-2.7 - 执行python scikit-learn grid-search方法时出现无效参数错误

python - 如何使用 gui 工具确定开放图形的图形尺寸?

python - 我怎样才能向量化这个 python 计数排序,使其绝对尽可能快?

ruby-on-rails - (未定义的方法 `+@' 用于 [] :Array)

python - 字符串格式占位符可最大程度地兼容 python 2.6、2.7 和 3.x

python - 无法访问返回的 h5py 对象实例

python - Mypy 似乎忽略了 TypeVar 类型的界限

javascript - React Render Components with Loop,但是当状态更新时它会重新渲染所有内容