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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:10:02+00:00 2026-05-26T23:10:02+00:00

i want to build a tree in java. operations such as insert delete update.

  • 0

i want to build a tree in java. operations such as insert delete update. how to go about it? I dont want a binary tree, i want to add children based on user input

i have implemented the following

import java.util.ArrayList;


public class Tree {

private Node root;

public Tree(String rootData)
{
    root=new Node();
    root.data=rootData;
    root.children=new ArrayList<Node>();
}

public void addChild(String name)
{

}
}

import java.util.*;

class Node { 
      String data;
      Node parent;
     List<Node> children;

 public Node()
 {
     data=null;
     children=null;
     parent=null;
 }
 public Node(String name)
 {
   Node n=new Node(name);
   n.data=name;
   n.children=new ArrayList<Node>();
 }

}

  • 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-05-26T23:10:03+00:00Added an answer on May 26, 2026 at 11:10 pm

    Here is the solution

    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTree;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeModel;
    
    
    public class MyTree extends JFrame{
    
    private JTree tree;
    private DefaultTreeModel model;
    private DefaultMutableTreeNode rootNode;
    
    
    public MyTree()
    {
         DefaultMutableTreeNode philosophersNode = getTree();
            model = new DefaultTreeModel(philosophersNode);
            tree = new JTree(model);
            JButton addButton = new JButton("Add ");
            addButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent event) {
                addNode();
              }
            });
            JButton removeButton = new JButton("Delete");
            removeButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent event) {
                  removeSelectedNode();
              }
            });
            JPanel inputPanel = new JPanel();
            inputPanel.add(addButton);
            inputPanel.add(removeButton);
    
            Container container = getContentPane();
    
            container.add(new JScrollPane(tree), BorderLayout.CENTER);
    
            container.add(inputPanel, BorderLayout.NORTH);
    
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(400, 500);
            setVisible(true);
    
    }
    
     private DefaultMutableTreeNode getSelectedNode() {
    
            return (DefaultMutableTreeNode)   tree.getLastSelectedPathComponent();
          }
    
     private DefaultMutableTreeNode getTree() {
             rootNode = new DefaultMutableTreeNode("Root");
            return rootNode;
          }
    
     private void removeSelectedNode() {
            DefaultMutableTreeNode selectedNode = getSelectedNode();
            if (selectedNode != null){
                 if(selectedNode.toString()=="Root")
                    {
                         JOptionPane.showMessageDialog(MyTree.this, "Cannot delete root element", "Error",
                                  JOptionPane.ERROR_MESSAGE);
                    }else{
    
              model.removeNodeFromParent(selectedNode);
                    }
            }
    
          }
    
     private void addNode() {
            DefaultMutableTreeNode parent = getSelectedNode();
            if (parent == null) {
              JOptionPane.showMessageDialog(MyTree.this, "Select an area.",   "Error",
                  JOptionPane.ERROR_MESSAGE);
    
              return;
            }
            String name = JOptionPane.showInputDialog(MyTree.this, "Enter Name:");
            model.insertNodeInto(new DefaultMutableTreeNode(name), parent,     parent.getChildCount());
    
          }
    
     public static void main(String args[]) {
            new MyTree();
          }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to build binary tree with key - value leafs with tuple (k,v).
I want to build a game tree for nine men's morris game. I want
I want to build a web based front-end to manage/administer my Linux box. E.g.
I want to build simple parse tree/but it's not working. I think that strlen
I have a typical nested tree model and I want to build an array
We have an object and we want to build a linq query based on
i want build a photography app with effects . e.g. old images with brown
I want build a sketch pad app on iPhone, I assume that this type
I want to build a lightweight linux configuration to use for development. The first
I want to build a bot that asks someone a few simple questions and

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.