Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8287281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:59:38+00:00 2026-06-08T11:59:38+00:00

I have implemented a Generic (n-ary) tree in Java as given here and by

  • 0

I have implemented a Generic (n-ary) tree in Java as given here and by referencing the source given on the GitHub repository of the author 1. I want to implement a pre-order and post-order traversal of the n-ary tree using the Iterator in java. Thus, the methods hasNext() will return a true whenever there is a node and the method next() will return the node which would be present next in the pre/post-order traversal.

I am trying to follow the pseudo-codes given in this question but I am not able to plug it in the next method of Iterator which I’ve written below

public class DepthFirstIterator<T> implements Iterator<TreeNode<T>> {

    private Stack<TreeNode<T>> dfsStack;
    private Tree<T> tree;
    private TreeNode<T> start;

    public DepthFirstIterator(Tree<T> tree) {
        this.tree = tree;
        this.dfsStack = new Stack<TreeNode<T>>();
        if (!this.tree.isEmpty())
            this.dfsStack.push(this.tree.getRoot());
    }

    public DepthFirstIterator(Tree<T> tree, TreeNode<T> startNode) {
        this.tree = tree;
        this.dfsStack = new Stack<TreeNode<T>>();
        if (startNode != null)
            this.dfsStack.push(startNode);
    }

    public boolean hasNext() {
        return (!this.dfsStack.isEmpty());
    }

    public TreeNode<T> next() {
                // Iterative code to obtain pre/post-order traversal
    }

    public void remove() {
     // Do nothing
    } 

Tree Class:

public class Tree<T> {

    private TreeNode<T> root;

    public TreeNode<T> getRoot() {
        return this.root;
    }

    public void setRoot(TreeNode<T> element) {
        this.root = element;
    }

    public boolean isEmpty() {
        return (this.root == null);
    }

    public int size() {
        if (isEmpty())
            return 0;
        else
            return getNumberOfNodes(root) + 1;
    }

    private int getNumberOfNodes(TreeNode<T> node) {
        int num = 0;
        Stack<TreeNode<T>> nodeStack = new Stack<TreeNode<T>>();
        nodeStack.push(node);
        while (!nodeStack.isEmpty()) {
            TreeNode<T> top = nodeStack.pop();
            for (TreeNode<T> child : top.getChildren()) {
                num++;
                nodeStack.push(child);
            }
        }
        return num;
    }
}

TreeNode class:

public class TreeNode<T> {

    private T data;
    private List<TreeNode<T>> children;
    private TreeNode<T> parent;

    public TreeNode() {
        super();
        children = new ArrayList<TreeNode<T>>();
        parent = null;
    }

    public TreeNode(T data) {
        this();
        setData(data);
    }

    public void setData(T data) {
        this.data = data;
    }

    public T getData() {
        return this.data;
    }

    public List<TreeNode<T>> getChildren() {
        return children;
    }

    public void setChildren(List<TreeNode<T>> children) {
        for (TreeNode<T> child : children)
            child.parent = this;
        this.children = children;
    }

    public void addChild(TreeNode<T> child) {
        child.parent = this;
        children.add(child);
    }

    public void insertChildAt(int index, TreeNode<T> child)
            throws IndexOutOfBoundsException {
        child.parent = this;
        children.add(index, child);
    }

    public TreeNode<T> getChildAt(int index) throws IndexOutOfBoundsException {
        return children.get(index);
    }

    public void removeChildAt(int index) throws IndexOutOfBoundsException {
        children.remove(index);
    }

    public void removeChildren() {
        children.clear();
    }

    public int getNumberOfChildren() {
        return children.size();
    }

    public String toString() {
        return getData().toString();
    }

    public boolean hasChildren() {
        return (getChildren().size() > 0);
    }

    public TreeNode<T> getParent() {
        return this.parent;
    }
}

I know that using as-many for blocks as the depth of the tree goes is totally wrong, but the stack logic was not being intuitive to me. If somebody,could please guide me I would really appreciate it.

Thanks,
Chaitanya

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:59:39+00:00Added an answer on June 8, 2026 at 11:59 am
    Stack<Treenode<T>> preorder;
    

    /*in constructor: */

    preorder.push(tree.getRoot());
    

    //Preorder next

    public TreeNode<T> next() {
    Treenode<t> ret = preorder.pop();
    
    for (int i = ret.getChildren().size()-1 ; i>=0; i--) {
                preorder.push(ret.getChildAt(i));
    
            }
    return ret;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a generic java interface in scala. I have looked at:
I have implemented Generic repository which only depends on IUnitOfWork (in Infrastructure.Repositories Library) and
I have implemented a generic IPropertyChangedNotifier using castle dynamic proxy. Here I intercept setter
I have seen a question here where they implemented a generic dispose method that
I have implemented the repository pattern using the following generic interface. public interface IRepository<T>
I have a generic interface IConstrained which is implemented by the generic Constrained class.
I want to have a generic object that implements an interface. I mean if
I have implemented a repository pattern in my asp.net mvc web application... But i
I have implemented a set datatype in javascript based in a generic object type,
I have a little game, where I implemented some collision detection. Now I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.