Solved

TreeNode *urgent*

Posted on 2004-04-12
31
301 Views
Last Modified: 2010-03-31
Can someone gimme the TreeNode codes cuz I need to build a tree. I need the class that has method such as isLeaf, insertChild or watever etc....pls~ Thanks~

0
Comment
Question by:jtcy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 9
  • 7
  • +1
31 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 300 total points
ID: 10810717
why not just use javax.swing.tree.DefaultMutableTreeNode
0
 

Author Comment

by:jtcy
ID: 10810750
i thought that is just an interface without any implementation?
0
 
LVL 9

Assisted Solution

by:mmuruganandam
mmuruganandam earned 150 total points
ID: 10810771
The following links would help you in building a tree with the TreeNode.


http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html
http://javaalmanac.com/egs/javax.swing.tree/AddNode.html?l=rel


Regards,
Muruga
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Assisted Solution

by:objects
objects earned 300 total points
ID: 10810775
TreeNode is just an interface.
DefaultMutableTreeNode is an implementation of that interface:

0
 

Author Comment

by:jtcy
ID: 10810909
I need to implement my own.
0
 
LVL 9

Assisted Solution

by:mmuruganandam
mmuruganandam earned 150 total points
ID: 10810937
Why you want to re-invent the wheel.  May be you can extend the DefaultMutableTreeNode and add your own functions to it.
0
 

Author Comment

by:jtcy
ID: 10810968
I have my class extends DefaultMutableTreeNode and above I have it import java.lang.*; it says cant resolve the class~
0
 
LVL 92

Assisted Solution

by:objects
objects earned 300 total points
ID: 10810969
You could just grab the source code for DefaultMutableTreeNode as a starting point.

What interface do you need to implement exactly?
0
 

Author Comment

by:jtcy
ID: 10811019
actually i just need methos like addChild, isLeaf, getChildAt, etc. Whether or not it's my own is not matter but if i could implement TreeNode myself with just few of these methods would be a better choice.
0
 
LVL 92

Expert Comment

by:objects
ID: 10811036
>  but if i could implement TreeNode myself with just few of these methods would be a better choice.

Why exactly, surely using an already existing class would be preferable?
0
 

Author Comment

by:jtcy
ID: 10811040
i donno if i am allowed to use them....anyway, i am trying to use them. Um...which package does DefaultMutablewatever class from? I try importing java.lang.*; still cant find it
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10811044
addChild, isLeaf, getChildAt and many more methods are already available in DefaultMutableTreeNode.



0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10811052
It is found at

javax.swing.tree.DefaultMutableTreeNode

the package that you are suppose to import is

import javax.swing.tree.DefaultMutableTreeNode;

0
 
LVL 92

Assisted Solution

by:objects
objects earned 300 total points
ID: 10811060
This should get you started:


public class TreeNode
{
   private ArrayList children = new ArrayList();

   public void addChild(TreeNode child)
   {
      children.add(child);
   }

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

   public TreeNode getChildAt(int index)
   {
      return (TreeNode) children.get(index);
   }

   public boolean isLeaf()
   {
      return children.size()==0;
   }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 10811065
> which package does DefaultMutablewatever class from?

see my original comment:
javax.swing.tree.DefaultMutableTreeNode
0
 

Author Comment

by:jtcy
ID: 10811084
objects, which class should ur treeNode extend?
0
 

Author Comment

by:jtcy
ID: 10811113
does arrayList works like vector where we don need to keep its element offset? and its size gets incremented automatically?
0
 
LVL 9

Assisted Solution

by:mmuruganandam
mmuruganandam earned 150 total points
ID: 10811128
Yes.

ArrayList is non-synchronized.  Vector is synchronized.

That is the major difference.  Otherwise, both are same.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 300 total points
ID: 10811131
> which class should ur treeNode extend?

doesn't need to extend anything. really depends on how you are using it.

> does arrayList works like vector where we don need to keep its element offset? and its size gets
> incremented automatically?

yes


0
 

Author Comment

by:jtcy
ID: 10811224
how do i do square root in java?
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10811238
Math.sqrt(<double>);
0
 

Author Comment

by:jtcy
ID: 10811246
if a variable is int, says int distance, can i do :
distance = Math.sqrt((double) distance); ??
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10811266
yes you can

distance = (int)Math.sqrt((double) distance);
0
 

Author Comment

by:jtcy
ID: 10811319
Okay, got a big trouble. For a given root node, i need to traverse it down and for every children of it, if it is a leaf node, then call a function called "evaluate(thisNode)", else just keep on traversing till its end. How can I do that?
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10811329
Here is the example, using which you can traverse the whole tree....


http://javaalmanac.com/egs/javax.swing.tree/GetNodes.html?l=rel

0
 
LVL 37

Accepted Solution

by:
zzynx earned 50 total points
ID: 10811933
public void evaluateAllNodes(TreeNode node) {

        if (node.isLeaf())
            evaluate(node);
   
        if (node.getChildCount() >= 0) {
            for (Enumeration e=node.children(); e.hasMoreElements(); ) {
                TreeNode n = (TreeNode)e.nextElement();
                evaluateAllNodes(n);
            }
        }
}

You call this function on your root:

    evaluateAllNode(myRoot);

0
 

Author Comment

by:jtcy
ID: 10822327
objects, could u write getParents for me please?~ i am stuck of it.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10822340
no longer interested in our input?
OK.
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10822344
getParent could be a TreeNode.

public TreeNode getParent(TreeNode node)
{
    return  node.getParent();
}
0
 

Author Comment

by:jtcy
ID: 10823142
will this work? an infinite recursion?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10949808
jtcy, are you doing a "spring clean up" of all your open Q's?
Nevertheless, thanks for awarding our efforts.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maven module vs maven project 3 106
eclipse console opening separately 2 46
Problem to Alipay 10 93
Html split(text) 2 45
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question