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

时间:2019-03-09 15:09:17

标签: python binary-search-tree breadth-first-search

给出这棵树:

         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个元素,因为这就是我现在要说的。我只是不知道如何考虑水平。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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]]