简体   繁体   中英

Construct N-ary tree from ArrayList in java

I am trying to construct N-ary tree from an ArrayList.The N-ary tree is represented as binary with child and sibling pointers. Here is the class Node, each node element carries data that should be print out with preorder traversal.

public class Node {


    public String data;
    public int ID,parentID;

    public Node child,sibling;  



    public Node(){}

    public Node(String data,int ID,int parentID)
    {
        this.data = data;
        this.ID = ID;
        this.parentID = parentID;

    }   

}

This is the class tree.

public class NTree
{
    public Node root;

    public NTree(){}    
    public void addTreeNode(Node parent, Node newChild)
    {

        if(parent.child == null)
         {
            parent.child = newChild;            
            return;
         }

        Node temp = parent.child;

        while(temp.sibling != null)
        {
            temp = temp.sibling;
        }
        temp.sibling = newChild;        

    }


    public Node find_parentNode(ArrayList<Node> nodes ,int parentID)
    {
        for(int i= 0;i<nodes.size();i++)
        {
            if(nodes.get(i).parentID == parentID)
                return nodes.get(i);
        }

        return null;
    }

    public  void preorder(Node root)
    {

           if (root == null) return;

             System.out.println(root.data);
             preorder(root.child);          
             preorder(root.sibling);

    }

In the main program, I have set the root as one node that has parentID = 0 and array list of Nodes is fine.In order to add Tree node, I must have which is the parent node and which is the new node.When the preorder function is called it crashes at the: preorder(root.child); which I think there is some problem with the creation of the tree. Any ideas?

for(int i = 0; i < list_nodes.size(); i++)
{                                                                   
      Node parent = tree.find_parentNode(list_nodes, list_nodes.get(i).parentID);                                       
      tree.addTreeNode(parent, list_nodes.get(i));              
}
tree.preorder(tree.root);



. ComException in thread "main" java.lang.StackOverflowError
    at java.io.FileOutputStream.write(Unknown Source)
    at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
    at java.io.BufferedOutputStream.flush(Unknown Source)
    at java.io.PrintStream.write(Unknown Source)
    at sun.nio.cs.StreamEncoder.writeBytes(Unknown Source)
    at sun.nio.cs.StreamEncoder.implFlushBuffer(Unknown Source)
    at sun.nio.cs.StreamEncoder.flushBuffer(Unknown Source)
    at java.io.OutputStreamWriter.flushBuffer(Unknown Source)
    at java.io.PrintStream.write(Unknown Source)
    at java.io.PrintStream.print(Unknown Source)
    at java.io.PrintStream.println(Unknown Source)
    at solution.NTree.preorder(NTree.java:77)
    at solution.NTree.preorder(NTree.java:78)

Did small modification in addTreeNode and find_parentNode method. Please try below:

public void addTreeNode(Node parent, Node newChild)
{

    if(parent==null){
        return;
    }
    if(parent.child == null)
     {
        parent.child = newChild;            
        return;
     }

    Node temp = parent.child;

    while(temp.sibling != null)
    {
        temp = temp.sibling;
    }
    temp.sibling = newChild;        

}


public Node find_parentNode(ArrayList<Node> nodes ,int parentID)
{
    for(int i= 0;i<nodes.size();i++)
    {
        if(nodes.get(i).ID == parentID)
            return nodes.get(i);
    }

    return null;
}

In main method, below is the use case:

    NTree tree = new NTree();
    tree.root = new Node("A", 1, 0);
    ArrayList<Node> list_nodes = new ArrayList<Node>();
    list_nodes.add(tree.root);
    list_nodes.add(new Node("B", 2, 1));
    list_nodes.add(new Node("C", 3, 1));
    list_nodes.add(new Node("D", 4, 1));
    list_nodes.add(new Node("E", 5, 3));
    list_nodes.add(new Node("F", 6, 3));

    for (int i = 0; i < list_nodes.size(); i++) {
        Node parent = tree.find_parentNode(list_nodes, list_nodes.get(i).parentID);
        tree.addTreeNode(parent, list_nodes.get(i));
    }
    tree.preorder(tree.root);   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM