Question: Revise the algorithm of Figure 6.6 so that it performs an in-order enumeration, rather than preorder. Figure 6.6: class BinTree implements Iterable { BinTree left;

Revise the algorithm of Figure 6.6 so that it performs an in-order enumeration, rather than preorder.

Figure 6.6:

class BinTree implements Iterable { BinTree left; BinTree right; T val; // other methods: insert, delete, lookup, ... public Iterator iterator () { return new TreeIterator (this); private class TreeIterator implements Iterator { private Stack s = new Stack(); TreeIterator (BinTree n) { if (n.val != null) s.push(n); public boolean

class BinTree implements Iterable { BinTree left; BinTree right; T val; // other methods: insert, delete, lookup, ... public Iterator iterator () { return new TreeIterator (this); private class TreeIterator implements Iterator { private Stack s = new Stack (); TreeIterator (BinTree n) { if (n.val != null) s.push(n); public boolean hasNext () { return !s.empty (); public T next() { if (!hasNext ()) throw new NoSuchElementException(); BinTree n = s.pop(); if (n.right != null) s.push (n.right); if (n.left != null) s.push (n.left); return n.val; public void remove () { throw new UnsupportedOperationException();

Step by Step Solution

3.30 Rating (162 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To perform an inorder enumeration we need to visit the left child first then the current node and fi... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Language Pragmatics Questions!