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 3 of 3

Thread: Tree problem

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

    Default Tree problem

    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>Elie>6";
            int f = jo.length;
            int p1 = -1;
     
            while(f > 0)
            {
            f = f - 1;
                String[] split = jo[f].split(">");
                int x = split.length;
                int l = x;
                int o = l;
                while (x > 0) {
                x = x - 1;
     
                    Node krikor = new Node();
                    krikor.setName(split[x]);
                    krikor.setLevel(x);
                    if(o!=l)
                    {
                    krikor.getChilds().add(nodelist.get(p1));
     
                    }
                    o = o - 1;
                    nodelist.add(krikor);
                    p1 = p1 + 1;
                }
            }
            fix(nodelist);
            int size = nodelist.size();
            int sizes =0;
            while(sizes < size)
            {
                 if(nodelist.get(sizes).getLevel() == 0)
                 print(nodelist.get(sizes),"");
                  sizes = sizes + 1;
            }
     
     
        }
        public void fix(List<Node> nodelist )
        {
            int i = 0;
            while(i < nodelist.size())
            {
                int o= 0;
                while(o < nodelist.size())
                {
                    if(nodelist.get(i).getName().equals(nodelist.get(o).getName()))
                    {
                    move(nodelist.get(i),nodelist.get(o),nodelist);
                    nodelist.remove(o);
                        System.out.println("entered here fix ");
                    o = o - 1;
                    }
                    o = o + 1;
                }
                i = i + 1;
            }
        }
        public void move(Node i, Node o,List<Node> nodelist  )
        {
           int po = 0;
            int f = 0;
            System.out.println("entered here move ");
            while(f < i.getChilds().size())
            {
                int o1= 0;
                while(o1 <  o.getChilds().size())
                {
                    if(nodelist.get(f).getName().equals(nodelist.get(o1).getName()))
                    {
                    move(nodelist.get(f),nodelist.get(o1),nodelist);
                  //  nodelist.remove(o1);
                   // o1 = o1 - 1;
                    po = 1;
                    }
                    o1 = o1 + 1;
                }
     
            f = f +1;
            }
     
           if(po == 0)
            {
                i.getChilds().add(copy(o));
             }
     
        }
        public Node copy(Node m)
        {
            Node o = new Node();
            o.setName(m.getName());
            o.setParent(m.getParent());
            int op = m.getChilds().size();
            while(op > 0)
            {
                op = op - 1;
                o.getChilds().add(copy(m.getChilds().get(op)));
            }
     
     
            return o;
        }
        public void print(Node node,String m)
        {
        System.out.println(m+""+node.getName());
        String o = m + "-------";
        if(node.getChilds().size() != 0)
        {
              int i = node.getChilds().size();
             while(i > 0)
             {
             i = i - 1;
            print(node.getChilds().get(i),o);
            }
        }
        }
     
        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>();
        String parent="";
        boolean flag = false;
        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;
        }
     
        public void setParent(String parent) {
            this.parent = parent;
        }
     
        public String getParent() {
            return parent;
        }
     
        public void setFlag(boolean flag) {
            this.flag = flag;
        }
     
        public boolean isFlag() {
            return flag;
        }
    }


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

    Default Re: Tree problem

    I want to print it

    Elie
    -------2
    -----------4
    -------3
    -----------1
    -----------------2
    krikor
    --------elie
    --------------7
    --------------6

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

    Default Re: Tree problem

    I am getting stack overflow error in move. Also some conditions is wrong.Can someone help me with algorithm here?

Similar Threads

  1. 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
  2. t:tree 2 an
    By smackdown90 in forum Web Frameworks
    Replies: 0
    Last Post: January 27th, 2010, 12:56 PM
  3. Problem with binary tree
    By Exoskeletor in forum Object Oriented Programming
    Replies: 2
    Last Post: January 8th, 2010, 01:03 PM
  4. B+ Tree
    By mikesir87 in forum Java Theory & Questions
    Replies: 0
    Last Post: November 20th, 2009, 10:52 AM
  5. B+ Tree implementation
    By programmer in forum Java Theory & Questions
    Replies: 2
    Last Post: November 15th, 2009, 05:47 PM