在Java中从表创建树数据结构

在Java中从表创建树数据结构,java,data-structures,tree,Java,Data Structures,Tree,给定以下CSV,它表示一个通用层次结构(想想:邮政编码匿名化,例如,在第二步中,邮政编码46072变成460**): 我首先通过解析来创建一个数组数组 现在,我想将其转换为树表示: * / \ A* B* / | \ / | | \ A1 A2 A3 B1 B2 B3 B4 如您所见,它是一棵树,每

给定以下CSV,它表示一个通用层次结构(想想:邮政编码匿名化,例如,在第二步中,邮政编码46072变成460**):

我首先通过解析来创建一个数组数组

现在,我想将其转换为树表示:

                *

        /                \

      A*                 B*

   /  |   \         /   |   |   \

 A1   A2   A3     B1   B2   B3  B4
如您所见,它是一棵树,每个节点都有任意数量的子节点

我有以下课程:

表格表格行表格单元格以及节点。 显然,一个表有多行,而这些行又有多个单元格。 树有根节点,节点有各种操作,如addChild(node)getParent()getChildren()

我试图弄清楚,如何在我的表上迭代,以跨越一棵树,如上图所示。到目前为止,我只是把自己弄糊涂了


非常感谢您的帮助

好的。因此,我的回答基于以下假设:

  • 矩阵最右边的列始终具有相同的值
  • 所有行的列数都完全相同
  • 同一列中的相同值位于连续行中。也就是说,不会有“A*”行后跟“B*”再后跟“A*”。两个“A*”必须是连续的
  • 在最左边的列上,所有值都是唯一的
  • 我不知道您的表、树和节点可以或不能做什么。因此,我使用了一个基本的2d数组(您也说过它是解析后的数组),并且只使用了一个基本节点作为我的树结构

    其思想是从头部矩阵递归工作。递归和树很好地结合在一起

    树的定义是将最右边的列中的值作为其根值,其子级的创建方式与删除最右边的列的方式相同,并将矩阵分割为多个部分,以便这些部分最右边的列具有相同的值。矩阵

    A1, A*, *
    A2, A*, *
    A3, A*, *
    B1, B*, *
    B2, B*, *
    B3, B*, *
    B4, B*, *
    
    分为一个值“*”和两个子矩阵:

    A1, A*
    A2, A*
    A3, A*
    
    B1, B*
    B2, B*
    B3, B*
    B4, B*
    
    A*
    矩阵也是如此,它的子矩阵是单单元矩阵,
    A1
    A2
    A3
    ,在这一点递归结束

    因此,假设您创建了一个表示层次结构生成器的类,并且其中有一个名为
    data
    的2D数组,那么您将有一个不带参数的很好的公共方法,该方法调用一个“脏”私有方法,该方法的参数表示当前子矩阵的矩阵边界

    public Node<String> createTree() {
        return this.createTree(0,data.length-1,data[0].length-1);
    }
    

    你的CSV总是只有3列吗?右边的列总是“*”吗?如果不是,请显示一个情况并非如此的用例,以及在这种情况下,您在根节点中的期望值。列的数量可能会有所不同。例如,邮政编码为460724607*,460**,46***,4****,*-就我所知,右边的栏始终是代表任何东西的符号或术语。不过请注意,层次结构的工作方式是由CSV指定的。也就是说,它也可以是:白种人,白人,人类,比如说,人类是排在最右边的人。太棒了,正是我需要的。
    public Node<String> createTree() {
        return this.createTree(0,data.length-1,data[0].length-1);
    }
    
    private Node<String> createTree( int firstRow, int lastRow, int lastCol) {
    
        // Recursion end. If we are at the leftmost column, just return a simple
        // node with the value at the current row and column.
        if ( lastCol == 0 ) {
            return new Node<String>( data[firstRow][0] );
        }
    
        // Create a node with the value of the top-right cell in our range.
        Node<String> result = new Node<String>(data[firstRow][lastCol]);
    
        // The next column from the right will have the values for the child nodes.
        // Split it into ranges (start row -> end row) and recursively build
        // the tree over the sub-matrix that goes column 0 -> lastCol-1 over each
        // range of rows.
    
        int childFirstRow = firstRow;
        String childVal = data[firstRow][lastCol-1];
    
        for( int candidateRow = firstRow; candidateRow <= lastRow; candidateRow ++ ) {
            // If the next value in the column is different from what we had so far, it's
            // the end of a row range, build the child tree, and mark this row as
            // the beginning of the next range.
            if ( ! data[candidateRow][lastCol-1].equals(childVal) ) {
                result.addChild(createTree( childFirstRow, candidateRow - 1, lastCol - 1));
                childFirstRow = candidateRow;
                childVal = data[childFirstRow][lastCol-1];
            }
            // In the special case of the last row, it's always the end of a range.
            if ( candidateRow == lastRow ) {
                result.addChild(createTree(childFirstRow,lastRow,lastCol - 1));
            }
        }
    
        return result;
    }