Created
February 10, 2022 02:13
-
-
Save liubin95/2b88dddf246ac1370b6bc22af59bd2fb to your computer and use it in GitHub Desktop.
java build tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Optional; | |
import java.util.StringJoiner; | |
import java.util.stream.Collectors; | |
/** | |
* BuildTree. | |
* | |
* @version 0.0.1 | |
* @serial 2021-07-19 : base version. | |
*/ | |
public class BuildTree { | |
public static void main(String[] args) { | |
List<Node> nodeList = new ArrayList<Node>() {{ | |
add(new Node(1, null)); | |
add(new Node(2, 1)); | |
add(new Node(3, 1)); | |
add(new Node(4, 2)); | |
add(new Node(5, 3)); | |
add(new Node(6, 5)); | |
}}; | |
Map<Integer, Node> temp = nodeList.stream().collect(Collectors.toMap(i -> i.id, i -> i)); | |
for (Node value : temp.values()) { | |
if (temp.containsKey(value.pid)) { | |
final Node parent = temp.get(value.pid); | |
parent.children.add(value); | |
} | |
} | |
final Optional<Node> first = temp.values().stream().filter(i -> i.pid == null).findFirst(); | |
System.out.println(first.orElse(null)); | |
} | |
static class Node { | |
Integer id; | |
List<Node> children; | |
Integer pid; | |
public Node(Integer id, Integer pid) { | |
this.id = id; | |
this.pid = pid; | |
children = new LinkedList<>(); | |
} | |
@Override | |
public String toString() { | |
return new StringJoiner(", ", Node.class.getSimpleName() + "[", "]") | |
.add("id=" + id) | |
.add("children=" + children) | |
.add("pid=" + pid) | |
.toString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment