Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

N-ary Tree with K-ary nodes

Posted on 2012-12-23
4
Medium Priority
?
753 Views
Last Modified: 2013-03-24
I have a tree structure representing several nodes with different types  -
					A
			B				C
		B1	B2	B3		C1	C2	C3
	B11	B22	B33

Open in new window


Here B and C - Object Type A
Children of B are B1, B2 and B3 - Object Type B
Children of C are C1, C2 and C3 - Object Type C
Children of B1 are B11, B22 and B33 - Object Type D
Similar I have N objects at each level representing a certain Object Type.

If I were push these nodes into a tree structure, because my objects are of different type, how I can sure, I push them in right sequence ?

I currently have my root class with a List of List of List of N List objects.
0
Comment
Question by:jagadeesh_motamarri
[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
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 38717947
Here B and C - Object Type A
Children of B are B1, B2 and B3 - Object Type B
Children of C are C1, C2 and C3 - Object Type C

So a type A object can have children of type B or type C
Can a  type A object have children of both types?


how I can sure, I push them in right sequence ?

What makes a sequence right or not right?
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 38718626
Can a  type A object have children of both types?
Yes

What makes a sequence right or not right?
It is just that I have to push them in the same order I mentioned above. Meaning, Root of my tree is of Type A and then push Type B and C and then D etc.
0
 
LVL 84

Expert Comment

by:ozo
ID: 38720471
then if you don't push them in order the node would have no where to go, so you can be sure.
0
 
LVL 28

Accepted Solution

by:
dpearson earned 1500 total points
ID: 38720552
If I understand correctly what you're trying to do then I think you could use a class like this:

	private static class CheckedList extends ArrayList<Object> {
		private final List<Class> m_T ;

		public CheckedList(List<Class> t) {
			m_T = t ;
		}

		@Override
		public boolean add(Object o) {
			boolean isAllowed = false ;
			for (Class c : m_T) {
				if (c.isInstance(o))
					isAllowed = true ;
			}

			if (!isAllowed)
				throw new IllegalArgumentException("Object " + o + " is not of a valid type for this list") ;

			return super.add(o) ;
		}
	}

Open in new window


The idea is that this CheckedList takes a list of types that it will accept.  So for the A list you could create a CheckedList with the classes B and C.

Then if you try to add something (like D) to a list that can't accept it (like A) it will throw an exception.  Other than the add method, it will work just like an ordinary list.

You can build up the full tree using these CheckedLsts - initializing each layer with the appropriate types that layer would accept.

Doug
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

610 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