Complete Following Code Package Labs Import Javautiliterator Import Javautillinkedlist Imp Q37077610

Complete the following code

package Labs;

import java.util.Iterator;

import java.util.LinkedList;

import java.util.Stack;

public class Tree<T>implements Iterable<T> {

static class Node<T>

{

T data;

LinkedList<Node<T>> children;

public Node(T d)

{

data=d;

children=new LinkedList<>();

}

}

Node<T> root;

@Override

public Iterator<T> iterator() {

// TODO Auto-generated method stub

return new TreeIterator();

}

private class TreeIteratorimplements Iterator<T>

{

Stack<Node<T>> s;

public TreeIterator()

{

s=new Stack<>();

if (root!=null)

s.push(root);

}

@Override

public boolean hasNext() {

return false;

// TO DO: Once again, to implement this

//method, simply check if stack s is empty.

}

@Override

public T next() {

return null;

// TO DO:Call hasNext() to make sure there

//is another node to iterate over. If there

//isn’t, return null.

//If there is another node, pop it off of s and save it.

//Now push each of the children onto the stack s. This

//time, since there can be

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.