【问题标题】:Construct N-ary tree from ArrayList in java在 java 中从 ArrayList 构造 N-ary 树
【发布时间】:2017-10-21 09:13:43
【问题描述】:

我正在尝试从 ArrayList 构造 N 叉树。N 叉树表示为带有子指针和兄弟指针的二元树。 这里是Node类,每个节点元素携带的数据应该通过前序遍历打印出来。

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;

    }   

}

这是类树。

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);

    }

在主程序中,我将根设置为一个节点,其 parentID = 0 并且节点的数组列表很好。为了添加 Tree 节点,我必须知道哪个是父节点,哪个是新节点.当调用 preorder 函数时,它会在以下位置崩溃: preorder(root.child);我认为创建树存在一些问题。有什么想法吗?

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)

【问题讨论】:

    标签: java arraylist tree binary-tree


    【解决方案1】:

    对 addTreeNode 和 find_parentNode 方法进行了小修改。请尝试以下操作:

    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;
    }
    

    在main方法中,下面是用例:

        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);   
    

    【讨论】:

      猜你喜欢
      • 2014-04-18
      • 1970-01-01
      • 2014-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多