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 8429431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:20:12+00:00 2026-06-10T05:20:12+00:00

In Java, is Tree the best structure to represent a tree with such properties:

  • 0

In Java, is Tree the best structure to represent a tree with such properties:

  • all nodes are unique ints;
  • the depth of a tree is given by int d > 0
  • there is no restriction on how many children a node could have

Operations I need todo:

  • iterate over the children located on the first level down only of any node
  • add nodes
  • remove a subtree, that is a node with all its children all the way down
  • extract a subtree, that is locate and copy (clone) in a separate tree

Operations I do not need:

  • edit nodes

Properties are just perfect for a Tree, so maybe there is some super implementation available in terms of performance. XMLTree or whatever.

Currently I’m using an array of arrays to store elements, but I find it not subtle.

  • 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-10T05:20:14+00:00Added an answer on June 10, 2026 at 5:20 am

    Here is a basic example of a Node class that could be used to form your tree structure. There is some complexity in writing methods to iterate over all child nodes in a depth-first or breadth-first fashion.

    As an alternative you could consider using DefaultMutableTreeNode which provides these methods for free (depthFirstEnumeration(), breadthFirstEnumeration()). This node implementation also allows you to attach a user object by calling setUserObject(Object). The drawback is that the implementation may not be as compact as writing your own structure, so it really depends on the size of your tree.

    public class Node {
      private final int value;
      private final List<Node> children;
    
      public Node(int value) {
        this.value = value;
        this.children = new LinkedList<Node>();
      }
    
      public int getValue() {
        return value;
      }
    
      public List<? extends Node> getChildren() {
        return Collections.unmodifiableList(children);
      }
    
      public void addChild(Node child) { 
        children.add(child);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a built-in data structure in Java to represent a Crit-bit tree? Or
I'm looking to build a tree-like structure for my Java application. I've tried using
i want to build a tree in java. operations such as insert delete update.
Which structure provides the best performance results; trie (prefix tree), suffix tree or suffix
Given a generic tree based (Java) data model. Let's say abstract class XModel {
Is there any way I could visualize a binary tree in java and C++?
I'm writing a Java Tree in which tree nodes could have children that take
i have the following code, taken partly from a Red Black Tree Java implementation.
In my source tree have Java code which is automatically generated from XSD files.
I've built a suffix tree in Java based on the site here http://marknelson.us/1996/08/01/suffix-trees/ but

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.