0

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. The question is: how to rewrite method getLevelOrderList using recursion?

  • You can use Guava's `TreeTraverser`, or even `BinaryTreeTraverser` – fge Apr 13 '14 at 22:30
  • http://stackoverflow.com/questions/19964048/level-order-traversal-w-binary-trees-using-java-and-recursion?rq=1 could be relevant? – geoff Apr 13 '14 at 22:34
  • Have you tried anything? Please post your attempeted solution(s) and explain how your results differed from the desired results. – Adi Inbar Apr 13 '14 at 22:51

1 Answers1

0

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. 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. By not using instance variables I removed that bug.

chubbsondubs
  • 37,646
  • 24
  • 106
  • 138