?
Solved

N-ary Tree with K-ary nodes

Posted on 2012-12-23
4
Medium Priority
?
770 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
  • 2
4 Comments
 
LVL 85

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 85

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

Technology Partners: 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!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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

862 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