Iterate through binary search tree to find all leaves

43,360

Solution 1

Use recursion:

public void visitNode(Node node) {
    if(node.left != null) {
        visitNode(node.left);
    }
    if(node.right != null) {
        visitNode(node.right);
    }
    if(node.left == null && node.right == null) {
        //OMG! leaf!
    }
}

start it by supplying root:

visitNode(root);

In order to translate this into an Iterator<Node> you'll have to translate recursion to loop and then to traversal with state. Non-trivial, but should give you a lot of fun.

Solution 2

class Node {
    public Node left = null;
    public Node right = null;
    // data and other goodies
}
class Tree {
    public Node root = null;
    // add and remove methods, etc.
    public void visitAllLeaves(Node root) {
        // visit all leaves starting at the root
        java.util.Stack<Node> stack = new java.util.Stack<Node>();
        if (root == null) return; // check to make sure we're given a good node
        stack.push(root);
        while (!stack.empty()) {
            root = stack.pop();
            if (root.left == null && root.right == null) {
                // this is a leaf
                // do stuff here
            }
            if (root.left != null) {
                stack.push(root.left);
            }
            if (root.right != null) {
                stack.push(root.right);
            }
        }
    }
}

I'm not sure if the above code works, but that's somewhere along the lines of what needs to be done. Another option is javax.swing.TreeModel (half-joking).

Solution 3

Here is how one could implement an Iterator that would only return the leaf nodes, i.e. nodes without a left or right subtree.

The iterator searches for leaf nodes in the tree by doing a depth-first search, remembering the current state of the search in a stack and "pausing" when it has found a leaf node (see fetchNext() method).

The search is resumed when the client "consumes" the leaf node by calling next().

class Node {
  public Node left;
  public Node right;
}

class LeaveIterator implements Iterator<Node> {
  private final Stack<Node> stack = new Stack<>();
  private Node nextNode = null;

  public LeaveIterator(Node root) {
    if (root != null) {
      stack.push(root);
      nextNode = fetchNext();
    }
  }

  private void fetchNext() {
    Node next = null;
    while (!stack.isEmpty() && next == null) {
      Node node = stack.pop();
      if (node.left == null && node.right == null) {
        next = node;
      }
      if (node.right != null) {
        stack.push(node.right);
      }
      if (node.left != null) {
        stack.push(node.left);
      }
    }
    return next;
  }

  public boolean hasNext() {
    return nextNode != null;
  }

  public Node next() {
    if (!hasNext()) {
      throw new NoSuchElementException();
    }
    Node n = nextNode;
    nextNode = fetchNext();
    return n;
  }

  public void remove() {
    throw new UnsupportedOperationException();
  }
}
Share:
43,360
Sti
Author by

Sti

Swift is so beautiful!

Updated on July 09, 2022

Comments

  • Sti
    Sti almost 2 years

    I am pretty new to trees, and I am trying to create kind of a "leaf iterator". I'm thinking it should put all nodes that does not have a .left and .right value onto a stack, but I'm not sure how or even if it's the right thing to do. I have tried searching for it, but every example I come over starts with going to the leftmost leaf, and going p = node.parent, and I am avoiding linking to the node's parent.

    I don't understand how I can repeatedlty start from the root and go through the vines without visiting the same vines over and over.

    EDIT

    I see people suggests using a recursive method to solve this, and I agree now. But I have been banging my head trying to find the solution for an iterator-class-way to do this for a while, and I still would like to know if that's possible, and how!

  • Sti
    Sti over 11 years
    But would'n this only fine ONE leaf? The leftmost?
  • Whymarrh
    Whymarrh over 11 years
    It uses the system stack to visit all leaves.
  • Tomasz Nurkiewicz
    Tomasz Nurkiewicz over 11 years
    @Sti: the keyword is recursion. Once it reaches the leftmost leaf it returns and gradually traverses the whole tree.
  • Sti
    Sti over 11 years
    @TomaszNurkiewicz Okay, so turning the if()if()if() into if()elseif()else() would be a deal breaker in recursion-world? But for the fun of it, what do you mean by "translate recursion to loop and traversal with state"? This does sound fun.
  • Tomasz Nurkiewicz
    Tomasz Nurkiewicz over 11 years
    @Sti: just try implementing an Iterator over all leaves, you'll see the challenges.
  • Ben
    Ben over 9 years
    Could you explain why this answers the question? The best answers include more than just code!