Solved

N-ary Tree with K-ary nodes

Posted on 2012-12-23
4
721 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 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 26

Accepted Solution

by:
dpearson earned 500 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JDeveloper 12c for 32 bit 4 84
factorial example 4 39
spring jars download 1 26
ejb on wildfly 5 19
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

786 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