BST广度优先遍历包括已删除的节点

问题描述 投票:1回答:2

鉴于这棵树:

         7
    5         9
  _  6      8  _ 
    _ _    _ _

我希望输出为:

[[Node(7)], [Node(5), Node(9)], [None, Node(6), Node(8), None], [None, None, None, None]]

因此,重要的是包含“无”并且输出是列表中的列表。

我尝试了很多东西,但这就是我现在所处的位置:

class Node(object):
  def __init__(self, key, value=None):
    self.key = key
    self.value = value
    self.parent = None
    self.left_child = None
    self.right_child = None
    self.height = 0 

def breadth_first_traversal(self):
  self.height = 1
  to_do = [self.root]
  if (self.root == None):
    return to_do
  output = []
  current_height = self.height
  output.append([str(node) for node in to_do])

  while (to_do):
    done = []
    current = to_do.pop(0)
    if (current.height > current_height):
      current_height += 1
    if (current.left_child):
      current.left_child.height = current_height + 1 
      to_do.append(current.left_child)
      done.append(current.left_child)
    elif (not current.left_child):
      done.append(None)
    if (current.right_child):
      current.right_child.height = current_height + 1 
      to_do.append(current.right_child)
      done.append(current.right_child)
    elif (not current.right_child):
      done.append(None) 
    output.append([str(node) for node in done])

  print(output)
  return output

现在的输出是:

[['7'], ['5', '9'], ['None', '6'], ['8', 'None'], ['None', 'None'], ['None', 'None']]

我理解为什么要制作2个元素的列表,因为这就是我现在应该做的。我只是不知道如何考虑水平。

python binary-search-tree breadth-first-search
2个回答
0
投票

一种可能性是找到所有节点,包括存储None的叶子,以及每个节点的深度,然后按深度分组:

为简单起见,我创建了一个二叉树,可以使用kwargs轻松初始化,以及遍历树并提供运行深度值的方法:

from itertools import groupby

class Node:
  def __init__(self, **kwargs):
     self.__dict__ = {i:kwargs.get(i, None) for i in ['left', 'right', 'value']}
  def get_depths(self, _count = 0):
    yield [_count, self.value]
    if self.left is not None:
      yield from self.left.get_depths(_count+1)
    else:
      yield [_count+1, None]
    if self.right is not None:
      yield from self.right.get_depths(_count+1)
    else:
      yield [_count+1, None]

tree = Node(value=7, left=Node(value=5, right=Node(value=6)), right=Node(value=9, left=Node(value=8)))
flattened = [[c for _, c in b] for _, b in groupby(sorted(list(tree.get_depths()), key=lambda x:x[0]), key=lambda x:x[0])]

输出:

[[7], [5, 9], [None, 6, 8, None], [None, None, None, None]]

0
投票

由于您正在使用二叉搜索树,因此将结果作为元组连接到数组是有意义的。

如果要根据数组的相对深度连接数组,则需要实现一个聚合器函数,该函数继续将元素附加到列表,直到深度递增为止,此时列表将被保存并清除以供下一组使用。

或者,您可以将结果传递给辅助函数,该函数只需按照您希望的方式连接元素。

编辑1:以下应该有效;但是,我还没有测试过。我只是将done移到while循环之外,这样它就不会在每次迭代后重新初始化。此外,当深度增加时,我只将done附加到output,因为这是没有其他元素要处理的时刻。

class Node(object):
  def __init__(self, key, value=None):
    self.key = key
    self.value = value
    self.parent = None
    self.left_child = None
    self.right_child = None
    self.height = 0 

def breadth_first_traversal(self):
  self.height = 1
  to_do = [self.root]
  if (self.root == None):
    return to_do
  output = []
  current_height = self.height
  output.append([str(node) for node in to_do])
  done = []

  while (to_do):
    current = to_do.pop(0)
    if (current.height > current_height):
      current_height += 1
      output.append([str(node) for node in done])
      done = []
    if (current.left_child):
      current.left_child.height = current_height + 1 
      to_do.append(current.left_child)
      done.append(current.left_child)
    elif (not current.left_child):
      done.append(None)
    if (current.right_child):
      current.right_child.height = current_height + 1 
      to_do.append(current.right_child)
      done.append(current.right_child)
    elif (not current.right_child):
      done.append(None) 

  print(output)
  return output
© www.soinside.com 2019 - 2024. All rights reserved.