Help using ListIterator to traverse a tree

Need help traversing a tree.
I have built the tree and now I am trying to traverse it to print it out. The main tree is a LinkedList, and each object on the list has a LinkedList of "children" which may or may not be empty.
My thought was to have a local ListIterator in each LinkedList and use hasNext() and next() to return the objects. So I build the tree, then I try to reset the local iterator for everyone in the tree. But it's not working. If I initialize the ListIterator in the LinkedList class, I get a "ConcurrentModificationException" when I try to access the list. Or if I don't initialize the ListIterator but try to set it later, I get a "NullPointerException" at the same point.
My questions are these:
Should I be able to keep a local ListIterator in the LinkedList?
And if so, am I doing it right?
Should I be able to "reset" the ListIterator after having created it?
And if so, am I doing that right?
Is there a different way I should be traversing my tree?
And, my burning question about this whole ListIterator thing: It appears from the online docs that a ListIterator is an Interface and not an Object. How can the listIterator method return an Interface?
Here's my code:
// the tree class
public class PartTree
     private Part          rootPart;
     private File          dataModel;          
     private PartList     theTree;          
     private Part          currentPart;
     public PartTree(String dmPath, String rootPartType)
               throws FileNotFoundException, IOException
          rootPart     = new Part();
          dataModel     = new File(dmPath);
          lostKids     = new PartList();
          theTree          = new PartList();
          rootPart.setName(rootPartType);
          rootPart.setChildList(theTree);
          refresh();
     public void refresh()
               throws FileNotFoundException, IOException
          theTree = new PartList();
          String            line;          
          Part           ptr;          
          try
               BufferedReader in =
                     new BufferedReader(new FileReader(dataModel));
               line     = new String(in.readLine());
               line = in.readLine();
               while(null != line)
                     ptr = new Part(line);
                     insertPart(ptr);
                     line = in.readLine();
          catch(FileNotFoundException e)
               throw new FileNotFoundException(e.getMessage());
          theTree.resetTreeIter();
          currentPart = rootPart;
// the linked list class
public class PartList extends LinkedList
     private Iterator listIter;               
     //private Iterator listIter = listIterator(0);     
     public synchronized void resetTreeIter()
          listIter = listIterator(0);
          Part ptr;
          Iterator it = listIterator(0);
          while( it.hasNext() )
               ptr = (Part)it.next();
               if( ptr.hasKids() )
                    ptr.getChildList().resetTreeIter();
     public synchronized Part nextPart()
          if( listIter.hasNext() )
               return (Part)listIter.next();
          return null;
}I know I am getting to the resetTreeIter method because when I replaced all the code there with a new exception, that got displayed.
Thanks for any help,
Connie

Much though I hate the Visitor pattern, this does look like a good use of it. If you define an interface public interface PartVisitor
    public void visit(Part part);
} and add a method to Part public void acceptPreOrder(PartVisitor visitor)
    visitor.visit(this);
    if (hasKids())
        Iterator it = getChildList().iterator();
        while (it.hasNext())
            it.next().acceptPreOrder(visitor);
}Then to print them, you merely say topLevelPart.accept(
    new PartVisitor()
        public void visit(Part part)
            System.out.println(part);
    });/

Similar Messages

  • Advanced:   How to traverse a tree representation in PL/SQL (procedure)?

    I am looking to write a method that will create a collection of records, each of which represents a node in a tree.
    TYPE t_all_folders IS TABLE OF get_all_folders%ROWTYPE INDEX BY PLS_INTEGER;
    v_all_folders t_all_folders;
    so first need help in figuring out what the cursor 'get_all_folders' would look like
    I have a folder structure represented in a database that is used basically to show visually
    (with a front end app) a folder structure (much like a folder structure in a file system).
    So each row has an entry in the 'folders' table like do:
    table folder:
         column           column
         folder_id          name
         1                    folder1               <= say this is a root folder
         2                    folder2               <= say this is a root folder
         3                    folder3               <= say this is a root folder
         4                    folder1a          <= all below are child folders..
         5                    folder1b
         6                    folder1c
         7                    folder1aa
         8                    folder1ab
         There is nothing in this table that indicates a hiearchy.
    The hiearchy is represented by another single table with two columns
    (I cannot change this, it is what it is)
    There is no left node or right node (not like a tree), just imagine sub folders.
    table: parent_child
         column          column
         parent_id     child_id
    such that visually when the tables are queried and the UI uses a folder icon to
    represent each row:
    it would look like this:
         folder1                              1
              - folder1a                         2
                   -folder1aa                    3
                   - folder1ab                    4
              - folder1b                         5
                   - folder1ba                    6
                   - folder1bb                    7
              - folder1c                         8
         folder2                              9
         folder3                              10
    I am attempting to create a query that will add to a collection folder records in the
    order above (1..10)
    In other words traverse the tree depth first going from:
              folder1 -> folder1a -> folder1aa -> folder1ab ->(back out to next level) folder1b -> folder1ba -> folder1bb -> folder1c
              then add folder2 (and traverse down that hiearch if needed)
              and then add folder3 to the colleciton and traverse down that hiearchy if there is one
              and continue adn so on.
              The requirement is to have them added to the collection in that order and when I iterate through the collection,
              they would of course need to be pulled out in that order (so use vararray with a counter to iterate through
              after the collection has been created.
    After the collection has been created, I have to iterate in that specific order to create records in another table where there is a column that requires an integer value that is the 1... order they come out of the collection
    and then have to iterate again and do something else in that order (and then other things - all the while needing in that order).
    Edited by: user12200443 on Nov 19, 2012 11:49 AM

    awesome, thanks for the help.
    put this in 'schema.sql' and run to create a reference schema and data for the example
    drop sequence seq_folders;
    CREATE SEQUENCE seq_folders
    INCREMENT BY 1
    MINVALUE 1
    START WITH 1
    CACHE 1000;
    drop table folders;
    create table folders (
         folder_id number not null,
         name varchar2(20) not null
    drop table parent_child;
    create table parent_child (
         parent_id number not null,
         child_id number not null);
    -- creation order (in order to have parent)
    -- folder1
    -- folder2
    -- folder3
    -- folder1a
    -- folder1b
    -- folder1c
    -- folder1aa
    -- folder1ab
    -- folder1ac
    -- folder1aaa
    -- folder1aba
    -- folder1aab
    -- folder1abb
    -- folder1aac
    -- folder1abc
    -- Visual hiearchy
    -- folder1                              1
    --      folder1a                         2
    --           folder1aa               3
    --                folder1aaa          4
    --                folder1aab          5
    --                folder1aac          6
    --           folder1ab               7
    --                folder1aba          8
    --                folder1abb          9
    --           folder1ac               10
    --      folder1b                         11
    --      folder1c                         12
    -- folder2                              13
    -- folder3                              14
    --- insert folders
    insert into folders(folder_id, name) values(seq_folders.nextval, 'folder1');
    insert into folders(folder_id, name) values(seq_folders.nextval, 'folder2');
    insert into folders(folder_id, name) values(seq_folders.nextval, 'folder3');
    insert into folders(folder_id, name) values(seq_folders.nextval, 'folder1a');
    insert into folders(folder_id, name) values(seq_folders.nextval, 'folder1b');
    insert into folders(folder_id, name) values(seq_folders.nextval, 'folder1c');
    insert into folders(folder_id, name) values(seq_folders.nextval, 'folder1aa');
    insert into folders(folder_id, name) values(seq_folders.nextval, 'folder1ab');
    insert into folders(folder_id, name) values(seq_folders.nextval, 'folder1ac');
    insert into folders(folder_id, name) values(seq_folders.nextval, 'folder1aaa');
    insert into folders(folder_id, name) values(seq_folders.nextval, 'folder1aba');
    insert into folders(folder_id, name) values(seq_folders.nextval, 'folder1aab');
    insert into folders(folder_id, name) values(seq_folders.nextval, 'folder1abb');
    insert into folders(folder_id, name) values(seq_folders.nextval, 'folder1aac');
    insert into folders(folder_id, name) values(seq_folders.nextval, 'folder1abc');
    commit;
    -- setup hiearchy
    insert into parent_child(parent_id, child_id) values (0, (select folder_id from folders where name = 'folder1'));
    insert into parent_child(parent_id, child_id) values (0, (select folder_id from folders where name = 'folder2'));
    insert into parent_child(parent_id, child_id) values (0, (select folder_id from folders where name = 'folder3'));
    -- 1a,1b,1c
    insert into parent_child(parent_id, child_id) values ((select folder_id from folders where name ='folder1'), (select folder_id from folders where name = 'folder1a'));
    insert into parent_child(parent_id, child_id) values ((select folder_id from folders where name ='folder1'), (select folder_id from folders where name = 'folder1b'));
    insert into parent_child(parent_id, child_id) values ((select folder_id from folders where name ='folder1'), (select folder_id from folders where name = 'folder1c'));
    -- aa,ab,ac
    insert into parent_child(parent_id, child_id) values ((select folder_id from folders where name ='folder1a'), (select folder_id from folders where name = 'folder1aa'));
    insert into parent_child(parent_id, child_id) values ((select folder_id from folders where name ='folder1a'), (select folder_id from folders where name = 'folder1ab'));
    insert into parent_child(parent_id, child_id) values ((select folder_id from folders where name ='folder1a'), (select folder_id from folders where name = 'folder1ac'));
    -- aaa,aba,aab
    insert into parent_child(parent_id, child_id) values ((select folder_id from folders where name ='folder1aa'), (select folder_id from folders where name = 'folder1aaa'));
    insert into parent_child(parent_id, child_id) values ((select folder_id from folders where name ='folder1aa'), (select folder_id from folders where name = 'folder1aab'));
    insert into parent_child(parent_id, child_id) values ((select folder_id from folders where name ='folder1aa'), (select folder_id from folders where name = 'folder1aac'));
    -- aba,abb,abc
    insert into parent_child(parent_id, child_id) values ((select folder_id from folders where name ='folder1ab'), (select folder_id from folders where name = 'folder1aba'));
    insert into parent_child(parent_id, child_id) values ((select folder_id from folders where name ='folder1ab'), (select folder_id from folders where name = 'folder1abb'));
    insert into parent_child(parent_id, child_id) values ((select folder_id from folders where name ='folder1ab'), (select folder_id from folders where name = 'folder1abc'));
    commit;
    then run this to get the error message
    WITH joined_data     AS
         SELECT     f.folder_id,     f.name,     pc.parent_id
         FROM     folders     f
         JOIN     parent_child     pc ON pc.child_id = f.folder_id
    SELECT     j.*,     ROWNUM     
    AS r_num
    FROM joined_data     j
    START WITH     parent_id =0
    CONNECT BY     parent_id= PRIOR child_id
    ORDER SIBLINGS BY     name;
    thanks for the help, hopefully I can find a way to read the rows/record into a data structure (does not have to be a single sql statement - can be anything I can do in PL/SQL.
    Edited by: user12200443 on Nov 19, 2012 5:55 PM

  • Traversing a tree with breadthFirstEnumeration

    Hi all,
    I'm trying to traverse a DefaultMutableNode tree using breadthFirstEnumeration. However, while traversing I need to remove some branches and continue with the Enumeration.
    Do you know any way to preserve the Enumeration after removing a branch???
    For example:
    for(Enumeration en = trial.breadthFirstEnumeration(); en.hasMoreElements() ;) {
                   DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)en.nextElement();
                   if((String)currentNode.getUserObject() == "2") {
                        ((DefaultMutableTreeNode)(currentNode)).removeAllChildren();
                   System.out.println((String)currentNode.getUserObject());
    This code traverses a tree with depth 3 and branching factor 3. In the meanwhile it removes node 2's children.
    However the otuput is
    0 1 2 3 4 5 6
    instead of: 0 1 2 3 4 5 6 10 11 12
    I'd be very grateful if someone can help me.
    Regards,
    Pesho

    This is how it usually is done.
    Start with an empty map, e.g. HashMap. The map has e.g. company name as key, and company as value.
    Start traversing, and do a lookup towards the map when you find a company reference. Use the company within the map if there is one, and create a new company and place it in the map if it doesn't exist. Populate the companies within the map with information when you bump into them during traversal.
    The map will contain fully populated companies when the traversal is completed, and all companies will reference the same instances since you always are checking the map.
    Kaj

  • How can I traverse a tree item

    It seems unable to do many things using tree item in forms 6i. For instance, how
    can I traverse a tree, how to implement drag-drop function within it. Many intel-
    ligent functions are hard to accomplish in a tree item without trigger when-mouse-
    enter, when-mouse-leave, when-mouse-move. If some expert can give the resolution,
    I'll be very thankful.

    Touch the event, touch Edit in upper right corner of the Events Details panel shown, scroll up in the Edit panel shown, and touch Delete Event.

  • WHAT IS THE USE OF APPL_EVENT = 'X'  IN TREES?

    WHAT IS THE USE OF APPL_EVENT = 'X'  IN TREES?

    Hi,
    This option generally specified whether the screen events need to be called or not..
    IF 'X' the PBO and PAI are called
    otherway around they are not called.
    Hope this would be helpful
    Regards
    Narin Nandivada

  • Help using jar file!

    Help using jar file!
    Hello
    I have created a jar file by using
    jar cvmf Manifest.txt myjar.jar 1.class Mydirectory
    In 1.java file :I have used
    JEditorPane editor;
    editor.setPage(getClass().getResource("/Mydirectory/default.htm"));
    If I am only giving myjar.jar file to the client without Mydirectory then also it is working fine by showing default.htm in JeditorPane.
    But my problem is I want to use
    Runtime.getRuntime().exec("cmd /c start IEXPLORE"+targetstr) ;
    targetstr will be the filename with path of the default.htm
    If I am giving myjar.jar file with Mydirectory to the client it is working but I don't want to give Mydirectory to the client .
    What should I do?
    Is there any solution for this?
    Using another jar file which will include mydirectory can solve this problem?
    Is there any othe concept in java so that I wll be able to hide Mydirectory from client?
    Please help.

    It seems like you could extract the .htm file from the jar, either with Runtime.exec or using the Jar API classes.

  • How to traverse a tree with a search

    I have a tree that has many leaves or nodes.
    I would like to have a search button where I put in a leaf or node name and it will go to the first occurrence on the tree and a next button that will take me to the next occurrence on the tree.
    The table I have looks like this.
    table name = PASS_NODE
    ID number,
    PARENT_ID NUMBER,
    NODE_NAME VARCHAR2(20)
    the sql code for the tree is:
    select "ID" id,
    "PARENT_ID" pid,
    CASE
    WHEN id = :P1_ID
    THEN '<span class="t1000url">'
    || node_name
    || ' >>'
    || '</span>'
    ELSE node_name
    END name,
    'f?p=&APP_ID.:1:&SESSION.::NO::P1_ID:'||id LINK,
    null a1,
    null a2
    from "#OWNER#"."PASS_NODE"
    order by NODE_NAME
    In the search text field the user will put in the NODE_NAME . I would like the search to traverse the tree by NODE NAME .
    Any ideas?

    I figured this out. In the "Search" process logic it was able to assign the value of the ID to the P1_ID page item.

  • Help using file sharing with different users accounts on same Macbook

    Hi, I just wanted a little help using the file sharing option. My wife and I had different users accounts on the same MacBook, but when I am trying to share my baby's picture from my documents folder for my wife to have the same picture, I can't find the way to do that; I am not been able to find my pictures on my wife's account. Is there any way that I could share a folder from my account and my wife to have the same files at the same time on her account?
    I mean, something like the smart play list on the itunes?
    Thank you

    You can't do that directly unless you change permissions on your whole Documents folder which is not a good idea. Your wife can see your Documents folder from her account by going to Users/yourusername/Documents. However, this folder has no read privileges for others except yourself by default so she won't be able to open it.
    rather than changing permissions on your Documents folder, you can place the pictures you want to share in Public folder in your home directory or in Users/Shared folder.

  • How to assign search help using ovs for select options for ALV in web dynpr

    how to assign search help using ovs for select options for ALV in web dynpro

    Hi,
    refer http://wiki.sdn.sap.com/wiki/display/WDABAP/InputhelpofObjectValueSelectioninWDABAP
    http://www.****************/Tutorials/WebDynproABAP/OVS/page1.htm
    and http://wiki.sdn.sap.com/wiki/display/Snippets/WebDynproAbap-OVSsearch+help
    Thanks,
    Chandra

  • How can we use "tooltip " option in heirarchical tree item in oracle 11g?

    how can we use "tooltip " option in heirarchical tree item properties in oracle 11g forms?

    hi user11973188
    how can we use "tooltip " option in heirarchical tree item properties in oracle 11g forms?isn't it exist in the tree item's property itself... ?!
    Regards,
    Abdetu...

  • How to create Search Help using more than 1 table

    Hi all,
    I need to create a search help using more than 1 table.
    Eq:-   Itable1 contains the data and Table2 contains the description of a field.
    In my search help i require A field from Table1 and For the corresponding field description from Table2.

    Hi,
    You can do this with the help of collective search help.
    Collective search helps:- Combination of elementary search helps. When we need to fetch data based on multiple selection criteriau2019s. More than one tables are Selection from multiple tables 
    Steps for creating collective search help.
    1) Enter the search help name and click on create.
    2) Choose Collective search help radio button option as the search help type.
    3) Enter the search help parameters.
    Note that there is no selection method to be entered for a collective search help.
    4) Instead of the selection method, we enter the included search helps for the collective search help.
    5)We need to assign parameters for each of the included search helps.
    6) Complete the parameter assignment by clicking on the push button.
    7) Collective search help offers the user to obtain F4 help using any of the included search helps.
    Hope this will help you:
    Reagrds:
    Alok

  • Traversing a tree

    Hello,
    How do I traverse a tree in such a manner that I visit all the nodes of the tree and at the sametime prepare a array/list containing all the nodes?
    An example will be highly appreciated.
    Thank you.

    Here is an example of what I have done. Can anyone tell me, how to save the nodes which satisfy the condition in an array and return the
    array to the calling program?
    public getNodeList(String nodeName, treeNode root) {
            int i;
            if (root.getNodeName().equals(nodeName)){
                //Save the node in a list
            int childCount = root.getChildCount();
            for (i=0;i<childCount;i++){
                aqsWB.treeNode childNode = (aqsWB.treeNode)this.getChildAt(i);
                nodeFound = childNode.getNodeList(nodeName,  childNode);
        }

  • Just brought a ipad2 need some help using it.

    Need some help using ipad2

    Also, Good Instructions http://www.tcgeeks.com/how-to-use-ipad-2/
    Apple - iPad - Guided Tours
    http://www.apple.com/ipad/videos/
    Watch the videos see all the amazing iPad apps in action. Learn how to use FaceTime, Mail, Safari, Videos, Maps, iBooks, App Store, and more.
    You can download this guide to your iPad.
    iPad User Guide for iOS 5
    http://itunes.apple.com/us/book/ipad-user-guide-for-ios-5/id470308101?mt=11
     Cheers, Tom

  • I need helping using iAds in my application.

    I need helping using iAds in my application. I currently am not using any storyboards. I am using Sprite builder for my UI.
    I attatched an image ot show all the different file name I have.
    Everyone is being used & they all work fully.
    The "iAdViewController.h & .m" files are just example codes I looked up and was messing with so that my iAd can work.

    I wouldn't even be able to use the Mathscript node in an executable? 
    What I am trying to do is make a user configurable data stream. 
    They tell me how many bytes are in the stream and what parameters they
    want to be put in to it.  Currently I have to make vi's that are
    called dynamicaly to make the parameters.   Then recompile
    the code and send it to them.  This is somewhat of how the config
    file is set up so I know how to make the data.
    Data_Type  foo
    Bytes 30
    parameter_name        
    function           
       byte#          format
    sync              
    foo_sync            
    29               int
    time                              
    foo_time             
    1,2,3,4       double
    If I can't use MathScript to allow the user to make there own functions
    is there another way that I might be able to do this so I do not have
    to recompile the code atleast?  Were I might just be able to make
    the new function and send that to them.
    Any Idea would be great.

  • Help, used ipod will lock up mitunes when I try to add songs from library will stop after 20-30- songs How do I fix this?

    Help, used ipod will lock up mitunes when I try to add songs from library will stop after 20-30- songs How do I fix this?

    Need more info to be of any assistance
    Which version of iTunes are you using?
    What is the iPod Software version as shown in the iTunes summary page?
    Which model of iPod classic are you using?

Maybe you are looking for

  • Cost center report with Vendor name

    Hi All, I need a cost center report which shows Vendor name and number. I tried with report S_ALR_87013611 but it is not displaying Vendor name for the expenses which is posted in MIGO. It is showing the offset A/c as GR/IR clearing a/c. Any other re

  • Can iCloud serve two iPhone users, or do both users have to login with the same Apple ID?

    My wife and I want to use iCloud to sync our calendars.  Do we both need to use the same Apple id tp accomplish this, or can iCluod support two users with seperate Apple ID's?  I can't find a way to do it seperately.

  • Why doesn't my Notification Center work?

    Recently I updated my iPhone to iOS 5.0.1.When I was on iOS 5.0.0 my Notification was working properly.But now,when I updated it,it stopped working!I try to swipe it from the black area from the clock area,i try to swipe it in Landscape mode,but it n

  • Traditional Chinese character display as square mark in workflow tab page

    Customer applied 11i ATG RUP7(6241631), then run the AD Administration utility to maintain multilingual tables, and no errors when upgrading EBS. Some tab page title and button text display as square mark. Please help! Thanks in adv, Jackie

  • PO EDI Problem

    Hi, I want to save the file to my local machine when the output is issued. I want to use EDI. Please help me in configuration. I have configured the partner profile in WE20. I have created the File Port and specified the directory path as d drive. I