简体   繁体   English

二叉树中的水平遍历

[英]Level traversal in binary tree

Here is the part of code of the binary tree class that I'm writing. 这是我正在编写的二叉树类代码的一部分。

class Node<T> {
    private T value;
    private Node<T> left;
    private Node<T> right;

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }

    public Node<T> getLeft() {
        return left;
    }

    public void setLeft(Node<T> left) {
        this.left = left;
    }

    public Node<T> getRight() {
        return right;
    }

    public void setRight(Node<T> right) {
        this.right = right;
    }

    public Node() {}

    public Node(T value) {
        this.value = value;
    }

    public Node(T value, Node<T> left, Node<T> right) {
        this.value = value;
        this.left = left;
        this.right = right;
    }
}

import java.util.*;

public class Tree<T extends Comparable<T>> {
    private Node<T> root;
    private List<T> levelOrderList = new ArrayList<T>();

    public Node<T> getRoot() {
        return root;
    }

    public Tree() {
    }

    public Tree(Node<T> root) {
        this.root = root;
    }

    private List<T> getLevelOrderList(Node<T> root){
        if (root == null)
            return Collections.emptyList();

        Queue<Node<T>> level  = new LinkedList<Node<T>>();
        level.add(root);
        while(!level.isEmpty()){
            Node<T> node = level.poll();
            levelOrderList.add(node.getValue());
            if(node.getLeft() != null)
                level.add(node.getLeft());
            if(node.getRight() != null)
                level.add(node.getRight());
        }
        return levelOrderList;
    }

    public List<T> getLevelOrderList() {
        return getLevelOrderList(root);
    }
}

The method getLevelOrderList() returns list of elements in tree in level by level order. 方法getLevelOrderList()逐级返回树中的元素列表。 The question is: how to rewrite method getLevelOrderList using recursion? 问题是:如何使用递归重写方法getLevelOrderList?

What you need to do is remove the loop, and just focus on a single pass through what now is in the loop. 您需要做的是删除循环,而只需关注循环中的当前过程。 You'll need to move some of that code out of the private method and into the public method you created. 您需要将其中一些代码移出私有方法,然后移入您创建的公共方法。 Like the check for root == null, level instantiation, etc. Then you'll just keep calling the private method until level is empty. 就像检查root == null,级别实例化等。然后,您将继续调用私有方法,直到级别为空。 Here is how I'd change the signature: 这是更改签名的方法:

public List<T> getLevelOrderList() {
    if( root == null ) return Collections.emptyCollection();

    List<Node<T>> level = new ArrayList<Node<T>>();
    List<T> values = new ArrayList<T>();
    level.add( root );

    return getLevelOrderList( level, values );
}

private List<T> getLevelOrderList(List<Node<T>> level, List<T> values) {
    if( level.isEmpty() ) return values;

    // do the next step to visit the node at the head of the list and recurse
}

That should be enough to get you started, but I can't give this away since it's clearly homework. 那应该足以让您入门,但是我不能放弃它,因为它显然是家庭作业。 Oh and your program had a bug if you called getLevelOrderList() twice it would never clear out the instance variable you had so it would return double the number of items from the tree. 哦,如果您两次调用getLevelOrderList(),您的程序就会出现一个错误,因为它永远不会清除您拥有的实例变量,因此它将从树中返回两倍的项目数。 By not using instance variables I removed that bug. 通过不使用实例变量,我消除了该错误。

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

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