Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Create a tree from list of strings

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Create a tree from list of strings

    paths = [
    "nodeA1",
    "nodeA1/nodeB1/nodeC1",
    "nodeA1/nodeB1/nodeC1/nodeD1/nodeE1",
    "nodeA1/nodeB1/nodeC2",
    "nodeA1/nodeB2/nodeC2",
    "nodeA3/nodeB2/nodeC3"
    ]


    you have list of strings there. how can i create a tree by system.out.println from all those paths.

    nodea1
    ----nodeb1
    -----------nodec1
    -------------------noded1
    ------------------------------nodee1
    -----------nodec2
    -----nodeb2
    -----------nodec2
    nodea3
    -------nodeb2
    ------------nodec3



    alogrithm am trying

    package project2;
     
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
     
    public class Exercise {
        String krikor = "lolig";
     
        public Exercise() {
     
            List<Node> nodelist = new ArrayList();
            String[] jo = new String[4];
            jo[0] = "Elie>2>4";
            jo[1] = "Elie>3>1>2";
            jo[2] = "Krikor>Elie>7";
            jo[3] = "Krikor>10";
            int f = jo.length;
            int p = 0;
            while (p < f) {
                String[] split = jo[p].split(">");
                int x = split.length;
                int m = 0;
                while (m < x) {
                    System.out.println(split[m]);
                    m = m + 1;
                    // here I will add each value into arraylist of nodes.and i will add their children as well.and their level
     
                }
                p = p + 1;
            }
     
     
        }
     
        public void setKrikor(String krikor) {
            this.krikor = krikor;
        }
     
        public String getKrikor() {
            return krikor;
        }
    }

    package project2;
     
    import java.util.ArrayList;
    import java.util.List;
     
    public class Node {
        String name;
        int level;
        public ArrayList<Node> childs = new ArrayList<Node>();
     
        public void setName(String name) {
            this.name = name;
        }
     
        public String getName() {
            return name;
        }
     
        public void setChilds(ArrayList<Node> childs) {
            this.childs = childs;
        }
     
        public ArrayList<Node> getChilds() {
            return childs;
        }
     
        public void setLevel(int level) {
            this.level = level;
        }
     
        public int getLevel() {
            return level;
        }
    }


    this is best i can go for. anyone with big help?


  2. #2
    Junior Member
    Join Date
    May 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Create a tree from list of strings

    case works like this

    1/2/3
    2/3/4
    1/6/7

    tree willlook

    1
    ---2
    ------3
    ---6
    -------7
    2
    ---3
    ------4


    someone can help me with coding this?

Similar Threads

  1. Creat a tree from list of strings
    By ayri in forum Loops & Control Statements
    Replies: 0
    Last Post: May 10th, 2010, 10:50 AM
  2. Data Structures(Binary Search Tree to AVL Tree)ASAP
    By jfAdik in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 5th, 2010, 03:58 AM
  3. Strings
    By Leeds_Champion in forum Algorithms & Recursion
    Replies: 3
    Last Post: November 3rd, 2009, 10:09 PM
  4. Strings
    By BeSwift21 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 13th, 2009, 07:02 PM
  5. Replies: 2
    Last Post: June 19th, 2008, 03:58 AM