FAQ: Trees: Java - Review

This community-built FAQ covers the “Review” exercise from the lesson “Trees: Java”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Pass the Technical Interview with Java

FAQs on the exercise Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I tried to redo the breadthFirstTraversal to use an iterable/iterator.
This is what I got:

import java.util.LinkedList;
import java.util.Queue;
import java.util.Iterator;

public class Tree {
  public TreeNode root;

  public Tree(TreeNode root) {
    this.root = root;
  }

  public void print() {
    print(this.root, 0);
  }

  public static void print(TreeNode current, int level) {
    String levelMarks = "";
    for (int i = 0; i < level; i++) {
      levelMarks += "-- ";
    }
    System.out.println(levelMarks + current.data);
    for (TreeNode child : current.children) {
      print(child, level + 1);
    }
  }

  public static void depthFirstTraversal(TreeNode current) {
    System.out.print(current.data + " ");
    for (TreeNode child : current.children) {
      depthFirstTraversal(child);
    }
  }

  public void depthFirstTraversal() {
    this.depthFirstTraversal(this.root);
  }

  private class BreadthFirstIterable implements Iterable<TreeNode>, Iterator<TreeNode> {
    Queue<TreeNode> queue;
    TreeNode current;
    
    public BreadthFirstIterable(TreeNode startNode) {
      current = startNode;
      queue = new LinkedList<>();
      queue.add(current);
    }
    public BreadthFirstIterable() {
      this(root);
    }

    public boolean hasNext() {
      return !queue.isEmpty();
    }
    public TreeNode next() {
      TreeNode current = queue.poll();
      queue.addAll(current.children);
      return current;
    }

    @Override
    public Iterator<TreeNode> iterator() {
      return this;
    }
  }

  public void breadthFirstTraversal() {
    BreadthFirstIterable iterable = new BreadthFirstIterable();
    for (TreeNode current : iterable) {
      System.out.print(current.data + " ");
    }
  }
  /*
  // Define breadthFirstTraversal() below
  public void breadthFirstTraversal() {
    TreeNode current = this.root;
    Queue<TreeNode> queue = new LinkedList<>();
    queue.add(current);
    while (!queue.isEmpty()) {
      current = queue.poll();
      System.out.print(current.data + " ");
      queue.addAll(current.children);
    }
  }
  */

  public static void main(String[] args) {
    TreeNode treeRoot = new TreeNode("S");
    TreeNode child1 = new TreeNode("N");
    TreeNode child2 = new TreeNode("O");
    TreeNode grandchild1 = new TreeNode("W");
    TreeNode grandchild2 = new TreeNode("Y");
    treeRoot.addChild(child1);
    treeRoot.addChild(child2);
    child1.addChild(grandchild1);
    child2.addChild(grandchild2);

    Tree tree = new Tree(treeRoot);
    tree.print();

    // Breadth-first traversal below:
    tree.breadthFirstTraversal();

    System.out.println("\n");
    tree.depthFirstTraversal();
  }
}