重新创建指针链表,以便从指针列表中获取指针。

huangapple go评论87阅读模式
英文:

Recreating LinkedList of pointers from list of pointers

问题

我有一个随机“指针”列表。

  • “A,B,C,D,E,F”

它们属于 N 个不同的链表

  • “A->C->D”,“B->E”,“F”

我只能从指针中获取“子节点”知道“父节点”,但反过来不行。

将它们排序到各自的链表中的当前实现如下。

如果可以的话,我会感谢您对优化下面代码的一些建议,或者如果我的方法是错误的。

        for (Pointer i: listOfPointers) {
            if (!i.hasParent())
                // 没有父节点,是根节点
                rootNodes.add(i.getId());
            else {
                // 父节点 -> 子节点
                parentChild.put(i.getParent().getId(),i.getId());
            }
        }
        // 遍历头部/根节点
        for (String i : rootNodes) {
            LinkedList<String> templist = new LinkedList<>();
            LinkedList<Map<String,String>> temp = new LinkedList<>();
            String tempnow = i;
            // 构建我们的链表,从头开始获取子节点,直到没有剩余节点
            do{
                if (templist.size() == 0)
                    System.out.println("父节点 - " + tempnow);
                else
                    System.out.println("子节点 - " + tempnow);
                templist.add(tempnow);
                if (parentChild.containsKey(tempnow))
                    tempnow = parentChild.get(tempnow);
                else
                    break;
            }while(true);
            linkedListHashMap.put(i, templist);
        }
英文:

I have a list of random "pointers"

  • "A,B,C,D,E,F"

They belong into N different linked lists

  • "A->C->D", "B->E", "F"

The only information I can get from the pointers is that the "child" knows the "parent" but not the other way around.

My current implementation to sort them into their own respective linked list is as below.

Would appreciate if i can get some pointers on optimizing the code below or if my approach to it is wrong.

        for (Pointer i: listOfPointers) {
            if (!i.hasParent())
                // no parent, its a root node
                rootNodes.add(i.getId());
            else {
                // parent -&gt; child
                parentChild.put(i.getParent().getId(),i.getId());
            }
        }
        // iterate through the head/roots
        for (String i : rootNodes) {
            LinkedList&lt;String&gt; templist = new LinkedList&lt;&gt;();
            LinkedList&lt;Map&lt;String,String&gt;&gt; temp = new LinkedList&lt;&gt;();
            String tempnow = i;
            //construct our linked list, start by the head and getting the child until we have nothing left
            do{
                if (templist.size() == 0)
                    System.out.println(&quot;Parent - &quot; + tempnow);
                else
                    System.out.println(&quot;Child - &quot; + tempnow);
                templist.add(tempnow);
                if (parentChild.containsKey(tempnow))
                    tempnow = parentChild.get(tempnow);
                else
                    break;
            }while(true);
            linkedListHashMap.put(i, templist);
        }

答案1

得分: 1

以下是您要翻译的内容:

我将创建一个Map,将每个Pointer映射到其父节点(实际上是边的Map),除了需要单独存储的根节点。之后,您可以通过反复遍历图形来重建List,直到不再找到映射。

public Collection&lt;List&lt;Pointer&gt;&gt; group(Collection&lt;Pointer&gt; pointers) {
    if(pointers.isEmpty()) {
        return Collections.emptyList();
    }

    List&lt;Pointer&gt; roots = new ArrayList&lt;&gt;();
    Map&lt;Pointer, Pointer&gt; parents = new HashMap&lt;&gt;(pointers.size());

    for(Pointer pointer : pointers) {
        if(pointer.hasParent()) {
            parents.put(pointer.getParent(), pointer);
        } else {
            roots.add(pointer);
        }
    }

    List&lt;List&lt;Pointer&gt;&gt; results = new ArrayList&lt;&gt;(roots.size());

    for(Pointer root : roots) {
        List&lt;Pointer&gt; list = new LinkedList&lt;&gt;();
        list.add(root);

        Pointer current = parents.get(root);
        while(current != null) {
            list.add(current);
            current = parents.get(current);
        }

        results.add(list);
    }

    return results;
}

对于您提供的输入:

Pointer a = new Pointer(&quot;A&quot;);
Pointer c = new Pointer(&quot;C&quot;, a);
Pointer d = a Pointer(&quot;D&quot;, c);

Pointer b = new Pointer(&quot;B&quot;);
Pointer e = new Pointer(&quot;E&quot;, b);

Pointer f = new Pointer(&quot;F&quot;);

Collection&lt;Pointer&gt; pointers = Arrays.asList(a, b, c, d, e, f);
Collection&lt;List&lt;Pointer&gt;&gt; grouped = group(pointers);
// 结果: A -&gt; C-&gt; D ; B -&gt; E ; F
英文:

I would create a Map which maps each Pointer by its parent (effectively a Map of edges), except the root nodes which need to be stored separately. Afterwards, you can reconstruct the List by repeatetly traversing the graph until no further mapping is found.

public Collection&lt;List&lt;Pointer&gt;&gt; group(Collection&lt;Pointer&gt; pointers) {
    if(pointers.isEmpty()) {
        return Collections.emptyList();
    }

    List&lt;Pointer&gt; roots = new ArrayList&lt;&gt;();
    Map&lt;Pointer, Pointer&gt; parents = new HashMap&lt;&gt;(pointers.size());

    for(Pointer pointer : pointers) {
        if(pointer.hasParent()) {
            parents.put(pointer.getParent(), pointer);
        } else {
            roots.add(pointer);
        }
    }

    List&lt;List&lt;Pointer&gt;&gt; results = new ArrayList&lt;&gt;(roots.size());

    for(Pointer root : roots) {
        List&lt;Pointer&gt; list = new LinkedList&lt;&gt;();
        list.add(root);

        Pointer current = parents.get(root);
        while(current != null) {
            list.add(current);
            current = parents.get(current);
        }

        results.add(list);
    }

    return results;
}

For you given input:

Pointer a = new Pointer(&quot;A&quot;);
Pointer c = new Pointer(&quot;C&quot;, a);
Pointer d = new Pointer(&quot;D&quot;, c);

Pointer b = new Pointer(&quot;B&quot;);
Pointer e = new Pointer(&quot;E&quot;, b);

Pointer f = new Pointer(&quot;F&quot;);

Collection&lt;Pointer&gt; pointers = Arrays.asList(a, b, c, d, e, f);
Collection&lt;List&lt;Pointer&gt;&gt; grouped = group(pointers);
// result: A -&gt; C-&gt; D ; B -&gt; E ; F 

答案2

得分: 0

以下是您提供的内容的翻译:

虽然在原始列表的基础上构建其他数据结构的直接解决方案很简单,但我认为找到一种需要恒定数量的附加内存的解决方案会很有趣,就像这个解决方案一样:

boolean sorted = false;
while (!sorted) {
  sorted = true;
  for (Element element : elements) {
    Element parent = element.parent;
    if (parent != null) {
      Element grandParent = parent.parent;
      if (grandParent != null && parent.value > grandParent.value) {
        element.parent = grandParent;
        parent.parent = grandParent.parent;
        grandParent.parent = parent;
        sorted = false;
      }
    }
  }
}

// 现在所有列表的尾部都已排序,只有头部(第一个元素)可能被放错位置

for (Element element : elements) {
  Element parent = element.parent;
  Element child = null;
  while (parent != null && element.value > parent.value) {
    if (child != null)
      child.parent = parent;
    element.parent = parent.parent;
    parent.parent = element;
    child = parent;
    parent = element.parent;
  }
}

思路是,当所有列表都排序好时,每个元素可能不大于其父元素。因此,如果我们看到 A->B->C->...B > C,那么我们会将这个片段重新排列为 A->C->B->...。我们会一直进行这样的操作,直到没有这样的片段需要重新排列为止,这意味着所有列表的尾部都已排序,只有头部(第一个元素)可能被放错位置。

现在,我们寻找类似 A->B->... 的片段,其中 A > B。由于只有头部可能被放错位置,而且明显 A 被放错了,那么 A 就是一个头部,即 A 没有子元素。因此,我们可以从 A 开始迭代列表,找到 A 应该放置在列表的哪个位置,并将 A 移动到那个位置,无需更新 A 的子元素,因为已保证 A 没有子元素。

以下是对随机数据运行此算法的示例:

原始数据:
(请注意,由于排版限制,我无法在此处显示原始数据)

第一轮后:
(请注意,由于排版限制,我无法在此处显示第一轮后的数据)

第二轮后:
(请注意,由于排版限制,我无法在此处显示第二轮后的数据)

如果您有任何进一步的问题或需要其他帮助,请随时问我。

英文:

While there are straightforward solutions that build additional data structures on the top of original list, I think it would be interesting to find a solution that requires constant amount of additional memory, like this one:

<!-- language: lang-java -->

boolean sorted = false;
while (!sorted) {
  sorted = true;
  for (Element element: elements) {
    Element parent = element.parent;
    if (parent != null) {
      Element grandParent = parent.parent;
      if (grandParent != null &amp;&amp; parent.value &gt; grandParent.value) {
        element.parent = grandParent;
        parent.parent = grandParent.parent;
        grandParent.parent = parent;
        sorted = false;
      }
    }
  }
}

// Now the tails of all lists are sorted, only the heads
// (first elements) may be misplaced

for (Element element : elements) {
  Element parent = element.parent;
  Element child = null;
  while (parent != null &amp;&amp; element.value &gt; parent.value) {
    if (child != null)
      child.parent = parent;
    element.parent = parent.parent;
    parent.parent = element;
    child = parent;
    parent = element.parent;
  }
}

The idea is that when all lists are sorted, each element may not be greater than its parent. So if we see A-&gt;B-&gt;C-&gt;... and B &gt; C, then we rearrange this fragment as A-&gt;C-&gt;B-&gt;.... We do this until there are no such fragments to rearrange, which means that the tails of all lists are sorted and only the heads (first elements) may be misplaced.

Now we look for fragments looking like A-&gt;B-&gt;... where A &gt; B. As only the heads may be misplaced, and A is obviously misplaced, then A is a head, i.e. there are no children of A. So we may iterate through the list starting at A, find where in this list A should be placed, and move A there, without necessity to update the children of A as A guaranteed to have no children.

Here is an example run on random data:

Original:
78-&gt;77-&gt;2-&gt;39-&gt;67-&gt;78-&gt;98-&gt;
86-&gt;30-&gt;71-&gt;90-&gt;86-&gt;97-&gt;98-&gt;30-&gt;88-&gt;31-&gt;67-&gt;19-&gt;36-&gt;5-&gt;57-&gt;16-&gt;
7-&gt;16-&gt;19-&gt;12-&gt;74-&gt;98-&gt;55-&gt;60-&gt;38-&gt;25-&gt;2-&gt;37-&gt;45-&gt;92-&gt;3-&gt;52-&gt;24-&gt;43-&gt;41-&gt;84-&gt;95-&gt;73-&gt;77-&gt;19-&gt;91-&gt;15-&gt;29-&gt;60-&gt;
17-&gt;94-&gt;25-&gt;
34-&gt;80-&gt;
51-&gt;
68-&gt;23-&gt;1-&gt;89-&gt;5-&gt;17-&gt;67-&gt;35-&gt;97-&gt;26-&gt;57-&gt;38-&gt;
89-&gt;84-&gt;
58-&gt;
8-&gt;5-&gt;87-&gt;43-&gt;8-&gt;
72-&gt;60-&gt;
41-&gt;49-&gt;28-&gt;92-&gt;84-&gt;
41-&gt;33-&gt;15-&gt;8-&gt;55-&gt;40-&gt;16-&gt;58-&gt;13-&gt;86-&gt;35-&gt;16-&gt;77-&gt;71-&gt;
53-&gt;13-&gt;

After the first loop:
78-&gt;2-&gt;39-&gt;67-&gt;77-&gt;78-&gt;98-&gt;
86-&gt;5-&gt;16-&gt;19-&gt;30-&gt;30-&gt;31-&gt;36-&gt;57-&gt;67-&gt;71-&gt;86-&gt;88-&gt;90-&gt;97-&gt;98-&gt;
7-&gt;2-&gt;3-&gt;12-&gt;15-&gt;16-&gt;19-&gt;19-&gt;24-&gt;25-&gt;29-&gt;37-&gt;38-&gt;41-&gt;43-&gt;45-&gt;52-&gt;55-&gt;60-&gt;60-&gt;73-&gt;74-&gt;77-&gt;84-&gt;91-&gt;92-&gt;95-&gt;98-&gt;
17-&gt;25-&gt;94-&gt;
34-&gt;80-&gt;
51-&gt;
68-&gt;1-&gt;5-&gt;17-&gt;23-&gt;26-&gt;35-&gt;38-&gt;57-&gt;67-&gt;89-&gt;97-&gt;
89-&gt;84-&gt;
58-&gt;
8-&gt;5-&gt;8-&gt;43-&gt;87-&gt;
72-&gt;60-&gt;
41-&gt;28-&gt;49-&gt;84-&gt;92-&gt;
41-&gt;8-&gt;13-&gt;15-&gt;16-&gt;16-&gt;33-&gt;35-&gt;40-&gt;55-&gt;58-&gt;71-&gt;77-&gt;86-&gt;
53-&gt;13-&gt;

After the second loop:
84-&gt;89-&gt;
5-&gt;8-&gt;8-&gt;43-&gt;87-&gt;
2-&gt;39-&gt;67-&gt;77-&gt;78-&gt;78-&gt;98-&gt;
13-&gt;53-&gt;
17-&gt;25-&gt;94-&gt;
28-&gt;41-&gt;49-&gt;84-&gt;92-&gt;
8-&gt;13-&gt;15-&gt;16-&gt;16-&gt;33-&gt;35-&gt;40-&gt;41-&gt;55-&gt;58-&gt;71-&gt;77-&gt;86-&gt;
60-&gt;72-&gt;
34-&gt;80-&gt;
51-&gt;
5-&gt;16-&gt;19-&gt;30-&gt;30-&gt;31-&gt;36-&gt;57-&gt;67-&gt;71-&gt;86-&gt;86-&gt;88-&gt;90-&gt;97-&gt;98-&gt;
58-&gt;
1-&gt;5-&gt;17-&gt;23-&gt;26-&gt;35-&gt;38-&gt;57-&gt;67-&gt;68-&gt;89-&gt;97-&gt;
2-&gt;3-&gt;7-&gt;12-&gt;15-&gt;16-&gt;19-&gt;19-&gt;24-&gt;25-&gt;29-&gt;37-&gt;38-&gt;41-&gt;43-&gt;45-&gt;52-&gt;55-&gt;60-&gt;60-&gt;73-&gt;74-&gt;77-&gt;84-&gt;91-&gt;92-&gt;95-&gt;98-&gt;

huangapple
  • 本文由 发表于 2020年10月26日 19:15:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64536051.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定