Solved

N-ary Tree with K-ary nodes

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now