TreeNode *urgent*

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~

jtcyAsked:
Who is Participating?
 
zzynxConnect With a Mentor Software engineerCommented:
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
 
objectsConnect With a Mentor Commented:
why not just use javax.swing.tree.DefaultMutableTreeNode
0
 
jtcyAuthor Commented:
i thought that is just an interface without any implementation?
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
mmuruganandamConnect With a Mentor Commented:
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
 
objectsConnect With a Mentor Commented:
TreeNode is just an interface.
DefaultMutableTreeNode is an implementation of that interface:

0
 
jtcyAuthor Commented:
I need to implement my own.
0
 
mmuruganandamConnect With a Mentor Commented:
Why you want to re-invent the wheel.  May be you can extend the DefaultMutableTreeNode and add your own functions to it.
0
 
jtcyAuthor Commented:
I have my class extends DefaultMutableTreeNode and above I have it import java.lang.*; it says cant resolve the class~
0
 
objectsConnect With a Mentor Commented:
You could just grab the source code for DefaultMutableTreeNode as a starting point.

What interface do you need to implement exactly?
0
 
jtcyAuthor Commented:
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
 
objectsCommented:
>  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
 
jtcyAuthor Commented:
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
 
mmuruganandamCommented:
addChild, isLeaf, getChildAt and many more methods are already available in DefaultMutableTreeNode.



0
 
mmuruganandamCommented:
It is found at

javax.swing.tree.DefaultMutableTreeNode

the package that you are suppose to import is

import javax.swing.tree.DefaultMutableTreeNode;

0
 
objectsConnect With a Mentor Commented:
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
 
objectsCommented:
> which package does DefaultMutablewatever class from?

see my original comment:
javax.swing.tree.DefaultMutableTreeNode
0
 
jtcyAuthor Commented:
objects, which class should ur treeNode extend?
0
 
jtcyAuthor Commented:
does arrayList works like vector where we don need to keep its element offset? and its size gets incremented automatically?
0
 
mmuruganandamConnect With a Mentor Commented:
Yes.

ArrayList is non-synchronized.  Vector is synchronized.

That is the major difference.  Otherwise, both are same.
0
 
objectsConnect With a Mentor Commented:
> 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
 
jtcyAuthor Commented:
how do i do square root in java?
0
 
mmuruganandamCommented:
Math.sqrt(<double>);
0
 
jtcyAuthor Commented:
if a variable is int, says int distance, can i do :
distance = Math.sqrt((double) distance); ??
0
 
mmuruganandamCommented:
yes you can

distance = (int)Math.sqrt((double) distance);
0
 
jtcyAuthor Commented:
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
 
mmuruganandamCommented:
Here is the example, using which you can traverse the whole tree....


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

0
 
jtcyAuthor Commented:
objects, could u write getParents for me please?~ i am stuck of it.
0
 
zzynxSoftware engineerCommented:
no longer interested in our input?
OK.
0
 
mmuruganandamCommented:
getParent could be a TreeNode.

public TreeNode getParent(TreeNode node)
{
    return  node.getParent();
}
0
 
jtcyAuthor Commented:
will this work? an infinite recursion?
0
 
zzynxSoftware engineerCommented:
jtcy, are you doing a "spring clean up" of all your open Q's?
Nevertheless, thanks for awarding our efforts.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.