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

The Archive Base Latest Questions

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

I have 2 classes. ErdosStruct contains all the data that needs to go into

  • 0

I have 2 classes. ErdosStruct contains all the data that needs to go into a JTree in SimpleTreeEx. What I’m struggling with is writing addNodes() that is recursive to add the nodes from the erdosStruct to a JTree. I have no real understanding of recursion and a buddy advised:

if it has no co authors
    return
else
    add the co authors

but I dont even know what that means 🙁

How would you go about using recursion too add nodes and subnodes to a tree?

import java.util.Vector;

/**
 * 
 * @info   The tree data structure. Each node in the tree is of type
 *         AuthNode. The root of the tree contains an AuthNode corresponding to
 *         "Root"
 */
public class ErdosStruct {
    private AuthNode top = new AuthNode("Root");

    public void createStruct() {

        top.addCoAuth(new AuthNode("Node 0"));
        top.addCoAuth(new AuthNode("Node 1"));
        AuthNode coAuth = top.getCoAuth(0); // get Node 0

        // Add to Node 0
        coAuth.addCoAuth(new AuthNode("Node 00"));
        coAuth.addCoAuth(new AuthNode("Node 01"));
        coAuth = coAuth.getCoAuth(0); // get Node 00

        coAuth.addCoAuth(new AuthNode("Node 000"));
        coAuth = coAuth.getCoAuth(0); // get Node 000

        // add to Node 000
        coAuth.addCoAuth(new AuthNode("Node 0000"));
        coAuth.addCoAuth(new AuthNode("Node 0001"));
        coAuth.addCoAuth(new AuthNode("Node 0002"));
        coAuth = coAuth.getCoAuth(2); // get Node 0002

        AuthNode Node0002 = coAuth;

        // add to Node 0002
        coAuth.addCoAuth(new AuthNode("Node 00020"));
        coAuth.addCoAuth(new AuthNode("Node 00021"));
        coAuth.addCoAuth(new AuthNode("Node 00022"));
        coAuth.addCoAuth(new AuthNode("Node 00023"));
        coAuth.addCoAuth(new AuthNode("Node 00024"));
        coAuth.addCoAuth(new AuthNode("Node 00025"));

        /// Other Path

        coAuth = top.getCoAuth(1); // get Node 1
        coAuth.addCoAuth(new AuthNode("Node 10"));
        coAuth = coAuth.getCoAuth(0);

        coAuth.addCoAuth(new AuthNode("Node 100"));
        coAuth = coAuth.getCoAuth(0);

        coAuth.addCoAuth(new AuthNode("Node 1000"));
        coAuth = coAuth.getCoAuth(0);
    }

    /**
     * @return the root element of type AuthNode of the tree data structure
     */
    public AuthNode getRoot() {
        return top;
    }
}

/**
 * @info  AuthNode structure and the interfaces
 */
class AuthNode {
    /**
     * Each AuthNode has a name and a vector of AuthNodes corresponding to co-authors
     */
    private String name;
    private Vector<AuthNode> coAuths = new Vector<AuthNode>();

    /**
     * Two types of constructor
     */
    public AuthNode() {
    }

    public AuthNode(String n) {
        name = n;
    }

    // Self-explanatory interfaces
    public void setName(String n) {
        name = n;
    }

    public String getName() {
        return name;
    }

    // Understand the usage of toString
    public String toString() {
        return name;
    }

    public int getCoAuthCount() {
        return coAuths.size();
    }

    public void addCoAuth(AuthNode coAuthor) {
        coAuths.addElement(coAuthor);
    }

    public AuthNode getCoAuth(int i) {
        return (AuthNode) coAuths.get(i);
    }
}

import java.awt.BorderLayout;
import java.awt.Dimension;   
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;

/**
 * 
 * @info   Main window of the UI - mainFrame of type JFrame contains ErdosStructurePanel of type JPanel
 */
public class SimpleTreeEx extends JFrame {
    public static void main(String[] args) {
        /**
         * erdosStruct contains a tree where node of the tree represents a
         * computer scientist - there is an edge from one node to another if the
         * computer scientists associated to these nodes have co-authored a
         * scientific article (See createStruct method in ErdosStruct class for
         * details)
         */
        ErdosStruct erdosStruct = new ErdosStruct();
        erdosStruct.createStruct();

        SimpleTreeEx mainFrame = new SimpleTreeEx();
        /**
         * ErdosStructPanel constructor takes a parameter of type ErdosStruct
         */
        mainFrame.getContentPane().add(new ErdosStructPanel(erdosStruct));

        mainFrame.setSize(500, 500);
        mainFrame.setVisible(true);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

class ErdosStructPanel extends JPanel {
    /**
     * @info    contains the tree data structure with information on co-authorship relations
     */
    public ErdosStructPanel(ErdosStruct erdosStruct) {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode(erdosStruct.getRoot());
        DefaultTreeModel tModel = new DefaultTreeModel(root);

        JTree tree = new JTree(tModel);
        tree.setShowsRootHandles(true);
        JScrollPane scroll = new JScrollPane(tree);
        add(scroll, BorderLayout.CENTER);
    }

    private void addNodes(ErdosStruct erdosStruct, DefaultTreeModel tModel) {
        if (erdosStruct.getRoot().getCoAuthCount() == 0) {
            return;
        } else {
            erdosStruct.getRoot().getCoAuth(0)
            tModel.insertNodeInto(new DefaultMutableTreeNode(), (MutableTreeNode) tModel.getRoot(), 0);
        }
    }
}
  • 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-12T15:13:21+00:00Added an answer on June 12, 2026 at 3:13 pm
    private void addNodes(ErdosStruct erdosStruct, DefaultTreeModel tModel) {
        if (erdosStruct.getRoot().getCoAuthCount() == 0) {
            return;
        } else {
            AuthNode node = erdosStruct.getRoot();
            addNodes(node, tModel, (MutableTreeNode) tModel.getRoot());
        }
    }
    
    protected void addNodes(AuthNode node, DefaultTreeModel tModel, MutableTreeNode parent) {
        if (node != null) {
            MutableTreeNode newParent = new DefaultMutableTreeNode(node);
            tModel.insertNodeInto(newParent, parent, parent.getChildCount() - 1);
            for (int index = 0; index < node.getCoAuthCount(); index++) {
                AuthNode child = node.getCoAuth(index);
                addNodes(child, tModel, newParent);
            }
        }
    }
    

    Okay, that was really painful. This is a “possible” solution. Personally, I prefer to add a child to the parent directly, not pass it into the method to added, but head hurts

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have classes such as AccountsController, ProductsController etc that all inherit from BaseController. Unity
I have classes that store data, and methods to go get data for individual
I have classes that are named exactly the same across different plug-ins that I
I have classes that are needed in both my web service and my server.
I have classes that can take a file as an argument, for example: ParserClass(file('/some/file',
I have classes Project , Resource and File . where A Project contains LIST
I have classes like the following. That is many classes with the same few
I'm working with xcode and I have classes associated with other projects that I
Does it impact performance to have classes that are not used for styling on
I have classes that are structured like the following: public class Forecast { [Key]

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.