473,466 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

building tree from a list of nodes

1 New Member
Hi. There's this tree I need to build, and I'm having problems with it.

I have to read the nodes from a file, which are sorted inorder, and have marker nodes to show where branches end. For example, I have two text files here, with what the trees should look like under them.
Expand|Select|Wrap|Line Numbers
  1. nodeA          nodeA
  2. nodeB          nodeB
  3. nodeC          nodeC
  4. endnodeC       endnodeC
  5. nodeD          endnodeB
  6. endnodeD       nodeD
  7. endnodeB       endnodeD
  8. endnodeA       endnodeA
  9.     A              A
  10.     |             / \
  11.     B            B   D
  12.    / \           |
  13.   C   D          C
  14.  
My node class is simply
Expand|Select|Wrap|Line Numbers
  1. public class node {
  2.     String s;
  3.     ArrayList<node> children;
  4.     public node(String s) {
  5.         this.s = s;
  6.         children = null;
  7.     }
  8.     //other functions
  9. }
  10.  
As for the tree, I'm having trouble building it.
I read the nodes from the txt file into an ArrayList of nodes. Here's my tree class so far.
Expand|Select|Wrap|Line Numbers
  1. //n is the list of nodes read from file
  2. public tree(ArrayList<node> n) {
  3.     //make tree
  4.     root = buildTree(n);
  5. }
  6.  
  7. //return root of new tree
  8. public node buildTree(ArrayList<node> n) {
  9.     node newNode = null;
  10.     while(!n.isEmpty()) {
  11.         String nodeName = n.get(0).toString();
  12.  
  13.         //check if marker node
  14.         if(nodeName.startsWith("end")) return null;
  15.  
  16.         newNode = new node(nodeName);
  17.         n.remove(0);
  18.  
  19.         //recurse
  20.         newNode.addChild(buildTree(n));
  21.     }
  22.     return newNode;
  23. }
  24.  
However, when I run it, root is still null.
I'm wondering if this is even the correct way to go about building this tree. Any advice?
Mar 27 '10 #1
1 15345
jkmyoung
2,057 Recognized Expert Top Contributor
Here's the mistake:
if(nodeName.startsWith("end")) return null;
The first endnode will cascade null down the entire tree.

You want:
if(nodeName.startsWith("end")) {
n.remove(0);
return null;
}
Mar 29 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Jeffrey Silverman | last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree structure from that list. Basically, I want to turn this: $list = array( array( 'id' => 'A', 'parent_id' => null, 'value'...
2
by: Stephan Tobies | last post by:
Hi everyone, I am looking for a good data structure that could be used to represent families of trees with shared sub-trees and copy-on-write semantics. On a very abstract level, I would like...
14
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for...
12
by: pillepop2003 | last post by:
Hey! Can anyone give me a hint, how this problem is best implemented: I have a table of users (see below), where every user has one "superior user" (= parent node), this should be a fully...
19
by: Christian Fowler | last post by:
I have a VERY LARGE pile of geographic data that I am importing into a database (db of choice is postgres, though may hop to oracle if necessary). The data is strictly hierarchical - each node has...
25
by: prabhat143 | last post by:
Hi, Given a singly linked, null terminated list, how can it be converted to tree? Each node in the list has three attributes: it's ID, it's parent ID and of course, the next node it's pointing...
1
by: David Hirschfield | last post by:
I've written a tree-like data structure that stores arbitrary python objects. The objective was for the tree structure to allow any number of children per node, and any number of root nodes...and...
4
by: Henry | last post by:
Does anybody have a real-world sample of buiding a treeview control using data from database tables? All the sample code I have found either builds the treeview manually or uses a file directory...
2
by: UJ | last post by:
I have an app that I've already written that works just great. It's a window's explorer like app for our data. Problem is, to build the treeview takes too long (30 secs and upward for less than...
4
by: Amit Bhatia | last post by:
User-Agent: OSXnews 2.081 Xref: number1.nntp.dca.giganews.com comp.lang.c++:817424 Hi, I have posted this post also for the thread "vector of lists" but since it is about something else...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.