【问题标题】:Converting a delimited file into a Tree将分隔文件转换为树
【发布时间】:2016-02-15 21:06:33
【问题描述】:

我正在将分隔文件转换为有序树结构。下面是一个示例输入...

1.2.3.4.5
1.3.2.4.5
1.2.4.5.6

我需要能够将其转换为如下输出(在可搜索的树结构中)...

1
-2
--3
---4
----5
--4
---5
----6
-3
--2
---4
----5

我对解决这个问题的想法是......

  1. 迭代文本文件并创建一个表示每一行的数组列表
  2. 使用 Collections.sort() 对数组列表进行排序
  3. 使用 TreeMap 将“基本”记录存储为键(在本例中为 1)和字符串数组列表以包含所有记录
  4. 迭代 TreeMap 的键并将其 arrayList 转换为包含表示每个条目的节点的 LinkedHashSet
  5. 迭代树的键并打印每个节点购买其索引值

我认为一切正常,但是当我开始测试这种方法时,我发现我的输出如下所示......

1
-2
--3
---4
----5
--4
---5
----6
-3
--2

可以看出,3/2/xx 下的节点不存在,这是由于我用来为我的节点值 (Node(3, 4)) 构建 LinkedHashSet 的逻辑将被忽略,因为它是一个重复的节点。我以为我的方向是正确的,但现在我可以看到我的逻辑显然有缺陷。有没有人对这样的方法有任何建议?我当前的代码如下...

TreeBuilder.java

public class TreeBuilder {

 public static void main(String[] args) {

    // Get a list of records
    List<String> data = new ArrayList<String>();
    data.add("1.2.3.4.5");
    data.add("1.3.2.4.5");
    data.add("1.2.4.5.6");

    Collections.sort(data);

    // Build the "Base" tree
    TreeMap<String, List<String>> tree = buildBaseTree(data);

    // Build the target tree structure
    TreeMap<String, LinkedHashSet<Node>> finalTree = convertListToSet(tree);

    printRecords(finalTree);

 }

 public static void printRecords(
        TreeMap<String, LinkedHashSet<Node>> recordTree) {

    System.out.println("---------Records---------");

    for (Map.Entry<String, LinkedHashSet<Node>> entry : recordTree
            .entrySet()) {

        System.out.println(entry.getKey());

        // Print out the structured data
        StringBuilder stringBuilder = new StringBuilder();
        Iterator<Node> iterator = entry.getValue().iterator();
        while (iterator.hasNext()) {

            Node node = iterator.next();
            for (int i = 0; i < node.index; i++) {
                stringBuilder.append("-");
            }

            System.out.println(stringBuilder.toString() + node.value);

            // "reset" the builder
            stringBuilder.setLength(0);
        }
    }

 }

 private static TreeMap<String, LinkedHashSet<Node>> convertListToSet(
        TreeMap<String, List<String>> tree) {

    TreeMap<String, LinkedHashSet<Node>> finalMap = new TreeMap<String, LinkedHashSet<Node>>();
    LinkedHashSet<Node> linkedHashSet = new LinkedHashSet<Node>();

    // Iterate the entry set
    for (Map.Entry<String, List<String>> entry : tree.entrySet()) {

        List<String> records = entry.getValue();
        for (String record : records) {
            String[] recordArray = record.split("\\.");

            for (int i = 1; i < recordArray.length; i++) {
                Node node = new Node(i, Integer.parseInt(recordArray[i]));
                linkedHashSet.add(node);
            }
        }

        finalMap.put(entry.getKey(), linkedHashSet);

        // reset our linkedHashSet
        linkedHashSet = new LinkedHashSet<Node>();

    }

    System.out.println("Final map " + finalMap);

    return finalMap;
 }

 /**
  * Builds a tree with base record keys and a list of records for each key.
  * 
  * @param data
  * @return
  */
 private static TreeMap<String, List<String>> buildBaseTree(List<String> data) {

    TreeMap<String, List<String>> tree = new TreeMap<String, List<String>>();
    List<String> recordList = null;

    // First find all base records
    for (String record : data) {

        String[] baseEntry = record.split("\\.");
        if (!tree.containsKey(baseEntry[0])) {
            recordList = new ArrayList<String>();
            tree.put(baseEntry[0], recordList);
        }
    }

    // Now place all sub-records in each base record
    for (String record : data) {

        String[] baseEntry = record.split("\\.");
        tree.get(baseEntry[0]).add(record);
    }

    System.out.println("------------------Base Tree---------------");
    System.out.println(tree);
    System.out.println("------------------------------------------");

    return tree;
 }

 private static List<String> readData(String file) {

    BufferedReader bufferedReader = null;
    try {
        bufferedReader = new BufferedReader(new FileReader(new File(file)));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    List<String> data = new ArrayList<String>();

    // Get a list of all the records
    String line = null;
    try {
        while ((line = bufferedReader.readLine()) != null) {
            data.add(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Sort the list so its ordered
    System.out.println("-------------Sorted Data Set-----------");
    Collections.sort(data);
    for (String record : data) {
        System.out.println(record);
    }
    System.out.println("---------------------------------------");

    return data;
 }
}

Node.java

public class Node implements Comparable<Node> {

 int index;
 int value;

 public Node(int index, int value) {
    this.index = index;
    this.value = value;
 }

 public int getIndex() {
    return index;
 }

 @Override
 public String toString() {
    return "Node [index=" + index + ", value=" + value + "]";
 }

 public void setIndex(int index) {
    this.index = index;
 }

 public int getValue() {
    return value;
 }

 public void setValue(int value) {
    this.value = value;
 }

 @Override
 public int compareTo(Node o) {

    Node otherNode = (Node) o;

    if (this.index > otherNode.index)
        return 1;

    if (this.index < otherNode.index) {
        return -1;
    }

    return 0;
 }

 @Override
 public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + index;
    result = prime * result + value;
    return result;
 }

 @Override
 public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Node other = (Node) obj;
    if (index != other.index)
        return false;
    if (value != other.value)
        return false;
    return true;
 }

}

【问题讨论】:

  • 我无法完全满足您的要求。树是否应该将列表视为有序并为以公共属性开头的列表使用公共节点结构?如果是这样,您无法在插入之前对列表进行排序,因为现有顺序很重要。假设这些是您的要求,我将添加一个答案。

标签: java list sorting treeset delimited


【解决方案1】:

不必太复杂。您所需要的只是SortedMapSortedMap 实例,并且只有一个技巧:递归地对其进行参数化以实现类型安全(如果需要)。

package com.acme;

import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.TreeMap;

public class Main {

    public static void main(String[] args) {
        List<String> rows = new ArrayList<>();
        rows.add("1.2.3.4.5");
        rows.add("1.3.2.4.5");
        rows.add("1.2.4.5.6");

        MyTreeMap root = new MyTreeMap();
        for (String row : rows) {
            MyTreeMap n = root;
            String[] cells = row.split("\\.");
            for (String cell : cells) {
                MyTreeMap child = n.get(cell);
                if (child == null) {
                    n.put(cell, child = new MyTreeMap());
                }
                n = child;
            }
        }

        print(root, "", "-");
    }

    static void print(MyTreeMap m, String indentationStr, String indentationStrAddition) {
        for (Entry<String, MyTreeMap> o : m.entrySet()) {
            System.out.println(indentationStr + o.getKey());
            print(o.getValue(), indentationStr + indentationStrAddition, indentationStrAddition);
        }
    }

    /**
     * This is just a construct that helps us to parameterize recursively.
     */
    static class MyTreeMap extends TreeMap<String, MyTreeMap> {private static final long serialVersionUID = 1L;}
}

【讨论】:

  • 抱歉延迟回复您,这正是我想要的。我通读了几遍才明白这里发生了什么,但对于其他觉得困惑的人来说,Brian 提供的方法非常类似于构建二叉树。这个概念基本上是从输入数据的每一列的根开始,遍历当前树,如果一个分支没有子,则添加它。谢谢布赖恩!
【解决方案2】:

如果我了解您的问题,您想从共享公共前缀的整数列表中创建搜索树。如果是这样的话,那么我认为你可以用更少的代码来实现这一点:

private static class TreeNode {

    private final Map<Integer, TreeNode> children = new HashMap<>();

    public void insert(List<Integer> values) {
        if (!values.isEmpty()) {
            children.putIfAbsent(values.get(0), new TreeNode());
            children.get(values.get(0)).insert(values.subList(1, values.size()));
        }
    }

    public void print(int level) {
        for (Map.Entry<Integer, TreeNode> entry : children.entrySet()) {
            System.out.print(String.join("", Collections.nCopies(level, "-")));
            System.out.println(entry.getKey());
            entry.getValue().print(level + 1);
        }
    }
}

我不确定您是否打算对整数列表进行排序。如果是这样,您可以在代码中的适当位置添加Collections.sort

【讨论】:

    猜你喜欢
    • 2017-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    • 2016-07-19
    • 2021-04-26
    • 1970-01-01
    • 2015-04-21
    相关资源
    最近更新 更多