广度优先遍历

Pri*_*kar 40 .net c# algorithm data-structures

我试图解决一个面试问题,但为此我必须逐级旅行二叉树.我设计的BinaryNode具有以下变量

private object data;
private BinaryNode left;
private BinaryNode right;
Run Code Online (Sandbox Code Playgroud)

有人可以帮我在BinarySearchTree类中编写BreadthFirstSearch方法吗?

更新:感谢大家的投入.所以这是面试问题."给定一个二叉搜索树,设计一个算法,创建每个深度的所有节点的链表(即,如果你有一个深度为D的树,你将有D个链表)".

这是我的方法,让我知道你的专家评论.

public List<LinkedList<BNode>> FindLevelLinkList(BNode root)
    {
        Queue<BNode> q = new Queue<BNode>();
        // List of all nodes starting from root.
        List<BNode> list = new List<BNode>();
        q.Enqueue(root);
        while (q.Count > 0)
        {
            BNode current = q.Dequeue();
            if (current == null)
                continue;
            q.Enqueue(current.Left);
            q.Enqueue(current.Right);
            list.Add(current);
        }

        // Add tree nodes of same depth into individual LinkedList. Then add all LinkedList into a List
        LinkedList<BNode> LL = new LinkedList<BNode>();
        List<LinkedList<BNode>> result = new List<LinkedList<BNode>>();
        LL.AddLast(root);
        int currentDepth = 0;
        foreach (BNode node in list)
        {
           if (node != root)
            {
                if (node.Depth == currentDepth)
                {
                    LL.AddLast(node);
                }
                else
                {
                    result.Add(LL);
                    LL = new LinkedList<BNode>();
                    LL.AddLast(node);
                    currentDepth++;
                }
            }
        }

        // Add the last linkedlist
        result.Add(LL);
        return result;
    }
Run Code Online (Sandbox Code Playgroud)

Cod*_*aos 73

广度优先搜索通常使用队列实现,使用堆栈进行深度优先搜索.

Queue<Node> q = new Queue<Node>();
q.Enqueue(root);
while(q.Count > 0)
{
    Node current = q.Dequeue();
    if(current == null)
        continue;
    q.Enqueue(current.Left);
    q.Enqueue(current.Right);

    DoSomething(current);
}
Run Code Online (Sandbox Code Playgroud)

作为null在出列后检查的替代方法,您可以在添加到队列之前进行检查.我没有编译代码,所以它可能包含一些小错误.


一个更好的(但更慢)版本,与LINQ很好地集成:

public static IEnumerable<T> BreadthFirstTopDownTraversal<T>(T root, Func<T, IEnumerable<T>> children)
{
    var q = new Queue<T>();
    q.Enqueue(root);
    while (q.Count > 0)
    {
        T current = q.Dequeue();
        yield return current;
        foreach (var child in children(current))
            q.Enqueue(child);
    }
}
Run Code Online (Sandbox Code Playgroud)

哪个可以与Children属性一起使用Node:

IEnumerable<Node> Children { get { return new []{ Left, Right }.Where(x => x != null); } }
Run Code Online (Sandbox Code Playgroud)

...

foreach(var node in BreadthFirstTopDownTraversal(root, node => node.Children))
{
   ...
}
Run Code Online (Sandbox Code Playgroud)


Via*_*ukh 13

var queue = new Queue<BinaryNode>();
queue.Enqueue(rootNode);

while(queue.Any())
{
  var currentNode = queue.Dequeue();
  if(currentNode.data == searchedData)
  {
    break;
  }

  if(currentNode.Left != null)
    queue.Enqueue(currentNode.Left);

  if(currentNode.Right != null)
    queue.Enqueue(currentNode.Right);
}
Run Code Online (Sandbox Code Playgroud)