• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Implementing N-Ary tree data structure in Java

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am writing a program to convert the SOAP message to a tree structure. The tree that I am going to create should be a N-Ary tree. I have created a class like the below one which is a data structure that acts as a node.Each element in the SOAP message is taken as an object.Complex elements( one object ) will maintain link to simple elements (objects)

Class nAryTree
{
String name;
String type;
String value;
nAryTree nodes[] = new nAryTree[100];

**** some functions goes on here ***
}

I have taken an array to point to simple elements in an array which is set at a size of 100. Assume a complex element movie which has name,language as the only two elements

<movie>
<name>Titanic</name>
<language>english</language>
</movie>

I am wasting lot of memory here as I created 100 objects in a class but has only two nodes.

Can somebody suggest me how can I better implement this without declaring arrays? I am interested to create my own data structure not any collections.So please suggest me how can I modify above class to manage the memory in an efficient way.

Thanks in advance,
Srinivas.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use a java.util.LinkedList or java.util.ArrayList instead of an array.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you really need a tree at all? The structure of XML nests attributes inside other attributes, but that may match an object structure better.Is there any way you can get the XML into objects like that? Then a List<Movie> implementation can make its use easy.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Srinivas Redd:
I am wasting lot of memory here as I created 100 objects in a class but has only two nodes.


Actually you have only created an array that can store 100 objects. Each element of that array is still null.

But I agree with Garrett - a List would be much better:

I also agree with Campbell. If the structure is fixed and limited, you can map the XML tree onto objects and only store those in a List.
 
reply
    Bookmark Topic Watch Topic
  • New Topic