-1

我已经坐在这个上面一段时间了。在二叉搜索树中,当你进行广度优先遍历时,有这一行代码我不知道它是做什么的。在教科书中我使用的代码如下。

public void breadthFirst()
    {
        BSTNode<T> p = root;
        Queue<BSTNode<T>> queue = new Queue<BSTNode<T>>();
        if(p != null)
        {
            queue.enqueue(p); 
        while(!queue.isEmpty()) 
        {
            **p = queue.dequeue();**  
            visit(p); 
            if(p.left != null)
            {
                queue.enqueue(p.left);
            }
            if(p.right != null)
            {
                 queue.enqueue(p.right);
                }
               }
    }
}
4

1 回答 1

0

The Queue contains nodes that we have to visit. Before when we visit a node we take it out of the queue (dequeue) so that it will not be visited again.

于 2013-11-14T08:29:02.993 回答