Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2006

Tip: Looking for answers? Try searching our database.

Node/Tree Data Structure Needed

Thread view: 
Brian Bagnall - 21 Dec 2006 20:50 GMT
Hi,

I'm looking for a data type that will store a group of x,y Point objects. It's
a series of nodes, which in the end is the same as a branching tree structure.
The first point could have perhaps 4 destination points - so the first point
must be classified as the parent of those 4 points. The data type should be
able to retrieve the parent given a child node, or retrieve all the child
nodes for that parent. And of course, each child in the tree can also be a
parent of more Point's.

    o
   /  \
 o    o
      /  \
    o    o

What would be the best data structure in Java to use for this sort of thing?

- Brian
Aneesh - 22 Dec 2006 04:12 GMT
Any Documents on setting up/Start up two Jboss iNstance on a single Node
Hemal  Pandya - 22 Dec 2006 07:34 GMT
> Hi,
>
> I'm looking for a data type that will store a group of x,y Point objects. It's
> a series of nodes, which in the end is the same as a branching tree structure.

Here goes. I am sure it has bunch of problems and that the experts will
frown. At least I have got the Node <T extends Node<T>> thing right, I
think. Take it for what it is worth...

import java.util.Set;
import java.util.HashSet;

abstract class Node<T extends Node<T>>
{
 Node parent;
 Set<Node> childs = new HashSet<Node>();
 boolean addChild(Node<T> n) {
   n.setParent(this, 0);
   return addChild(n, 0);
 }
 boolean setParent(Node<T> n) {
   setParent(n, 0);
   return n.addChild(this, 0);
 }
 boolean addChild(Node<T> n, int unused) {
   return childs.add(n);
 }
 void setParent(Node<T> n, int unused) {
   parent = n;
 }
}

class Point extends Node<Point>
{
 int _x, _y;
 Point(int x, int y){
   _x = x;
   _y = y;
 }
 public String toString() {
   StringBuffer retVal = new StringBuffer("{Point:"
     + System.identityHashCode(this) +
     "{_x:" + _x + "}{_y:" + _y + "}{parent: "
     + System.identityHashCode(parent) + "}{childs{");
   for (Node n: childs) {
     retVal.append(n.toString());
   }
   retVal.append("}}");
   return retVal.toString();
 }
}

class Employee extends Node<Employee> {
 String _name;

 Employee(String name)
 {
   _name = name;
 }
}

class NodeMain {
 public static final void main(final String[] args) {
   Point p = new Point(10,10);
   p.addChild(new Point(20,20));
   Point p2 = new Point(11,11);
   p2.setParent(p);
   System.out.println(p);
   System.out.println(p2);
   Employee e = new Employee("hemal");
   // e.setParent(p2); // does not compile
 }
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.