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
Solved

N-ary Tree with K-ary nodes

Posted on 2012-12-23
4
728 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 27

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

840 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