0

I have the following n-ary tree. The value in the nodes are of the form names {count}. The count value of any node is the number of paths that include the node.

enter image description here

I need to print all paths(from root to leaf) in the tree until the count of that node becomes zero. Once the count of the node is zero, then path only up to its predecessor is printed(provided their count is greater than 0)

The 6 paths for the above graph would be
3-5-1-2-4-6
3-5-1-2
3-5-1-7
3-5-2
3-1-4
3-7

[EDIT] Tried the following. Called the function toString(indent,node) using a loop that runs root.count number of times. But don't know how to stop after a path(3-5-1-2-4-6) has no more childs. Instead it prints 3-5-1-2-4-6-7-2-1-4-7.

public String toString(String indent, MyTree node) {
       String output="";
       while(node.count>0){
           //output = indent + node.toString() + "\n";
           output = indent + node.name;
           node.count--;
           //if(node.count==0) break;
           String childsOutput = "";
           for (MyTree child : node.childs) {
               childsOutput += toString(indent + " ", child);
           }
           return output + childsOutput;
       }
return output;
}

Thanks in advance.

GvanJoic
  • 213
  • 2
  • 14
  • You need to provide either some code showing what you have tried, or a question indicating which part of the problem is unclear to you. People are not just going to write the code for you without any indication of effort on your part. – Travis Nov 09 '14 at 16:21
  • @Travis: Updated the question with the code. Can you help me now. – GvanJoic Nov 09 '14 at 20:11

1 Answers1

1
public  void path_Extraction(Node root)
{

         int i=0;

         while(root.childs.size()!=0)
        {   
            Node childs=root.childs.get(0);
            while(childs.count!=0)
            {   ArrayList<Node> path=new ArrayList<Node>();
                ArrayList<Node> remove=new ArrayList<Node>();
                i++;
                extract(childs,path,remove);
                paths.put(i,path);
                Removing_node.remove.put(i, remove);
            }

            }
        }


 public void extract(Node childs,ArrayList<Node> path,ArrayList<Node> remove)
{
    if(childs.count>1)
    {
        if(childs.childs.size()>0)
        {
            extract(childs.childs.get(0),path,remove);
            childs.count--;
            if(childs.count==0)
            {

                childs.parent.childs.remove(childs);
                childs.parent=null;
                path.add(childs);       
                remove.add(childs);
            }
            else
            {

                path.add(childs);
            }

        }
    }
    else
    {
        if(childs.childs.size()>0)
        {
            extract(childs.childs.get(0),path,remove);
            childs.count--;

            childs.parent.childs.remove(childs);
            childs.parent=null;
            path.add(childs);
            remove.add(childs);
        }
        else
        {
            (childs.count)--;

            childs.parent.childs.remove(childs);
            childs.parent=null;
            path.add(childs);
            remove.add(childs);

        }
        }
    }

Please See to it , it will helpful for you because i did that earlier.Assuming you have a Class of Node and its fields :). Have a great day ahead.

Kunal
  • 57
  • 2
  • 11