简体   繁体   English

试图在Java中的二叉搜索树上实现层序遍历

[英]Trying to implement level order traversal on a binary search tree in Java

Okay, so I'm trying to make a method that will return the level order of a basic binary search tree that carries int values in its nodes.好的,所以我正在尝试创建一个方法,该方法将返回在其节点中携带 int 值的基本二叉搜索树的级别顺序。 I've figured out most of the other methods, such as insertion, post order and pre order, but I keep running into the same problem with the level order method我已经弄清楚了大多数其他方法,例如插入、后订购和预订购,但我一直遇到与级别订购方法相同的问题

Here's the code:这是代码:

private DoubleStackQueue<Node> queue = new DoubleStackQueue<Node>();
//this is a queue that uses two stacks, one for the front and one for the back.
//it works just like a queue.
public String levelOrder(){  
    s = "";  //The s is a private String that is implemented earlier
    queue.add(this);  
    while (!queue.isEmpty())  
    {  
        Node node = (Node)queue.remove();  
        if (!(node.equals(null))) {s += ""+node.getVal();}  
        if (!(left.equals(null))) {queue.add(node.left);}  
        if (!(right.equals(null))) {queue.add(node.right);}  
    }  
    return s;  
}

The main problem that I am having is that, when the program gets to a leaf node, it still adds its children to the queue, even though there are no children, just null, so I'll get a queue that has two nulls in front of the actual item in it.我遇到的主要问题是,当程序到达叶节点时,它仍然将其子节点添加到队列中,即使没有子节点,只有 null,所以我会得到一个包含两个空值的队列在它的实际项目的前面。 I originally had the if statements as (left,= null) and such.我最初的 if 语句为 (left,= null) 等。 but that didn't work either.但这也没有用。 I'm just trying to figure out how to make the program recognize when there aren't any children?我只是想弄清楚如何让程序识别没有孩子的情况? What do I need to do?我需要做什么?

Within your code I can find the lines在您的代码中,我可以找到这些行

if (!(left.equals(null))) {queue.add(node.left);}  
if (!(right.equals(null))) {queue.add(node.right);}

where in the condition it states left and right which are nowhere defined in the parts you show but on the right it reads node.left and node.right .在条件下,它声明leftright在您显示的部分中没有定义,但在右侧它读取node.leftnode.right Is this intentionally?这是故意的吗? I'd expect also node.left and node.right in the if conditions.我希望在 if 条件下也node.leftnode.right

Look at this question: Level-order tree traversal I believe this is the same thing you're trying to achieve, is it not?看看这个问题: Level-order tree traversal我相信这和你想要实现的目标是一样的,不是吗? This is a pretty classic problem, so in my experience it's been discussed over-and-over before.这是一个非常经典的问题,所以根据我的经验,它之前已经被反复讨论过。

Several comments:几条评论:

  1. The primary issue is that you're using left and right instead of node.left and node.right in your comparisons.主要问题是您在比较中使用leftright而不是node.leftnode.right

  2. To compare against null use if (var != null) .要与 null 进行比较,请使用if (var != null) Do not use equals() .不要使用equals() If a variable is null you cannot call methods on it as that will trigger NullPointerException s.如果一个变量是 null 你不能调用它的方法,因为这会触发NullPointerException s。

  3. Once you have fixed your code you will never have null inserted into the queue.修复代码后,您将永远不会将null插入队列。 The first object you add is this which is guaranteed to be non-null, and subsequently you always check for null before inserting items onto the queue.您添加的第一个 object 就是this ,它保证不为空,随后您总是在将项目插入队列之前检查 null。 That means your first if (node != null) check is unnecessary.这意味着您的第一次if (node != null)检查是不必要的。

  4. The cast in (Node) queue.remove() should be unnecessary. (Node) queue.remove()中的强制转换应该是不必要的。 Your compiler ought to warn you about this.你的编译器应该警告你这一点。

Result:结果:

queue.add(this);

while (!queue.isEmpty())  
{  
    Node node = queue.remove();

    s += "" + node.getVal();

    if (node.left  != null) { queue.add(node.left);  }
    if (node.right != null) { queue.add(node.right); }
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM