我的组织结构如下:
T1
|'''' ''''|
T2 T3
|
T4
在数据库中存储为-
+----+---------+-----------+-----------+
| ID | TEAM_ID | PARENT_ID | TEAM_NAME |
+----+---------+-----------+-----------+
| 1 | 1 | 1 | T1 |
| 2 | 2 | 2 | T2 |
| 3 | 2 | 1 | T2 |
| 4 | 3 | 1 | T3 |
| 5 | 3 | 3 | T3 |
| 6 | 4 | 4 | T4 |
| 7 | 4 | 2 | T4 |
| 8 | 4 | 1 | T4 |
+----+---------+-----------+-----------+
我想根据上表中提供的平面数据重新构建上述树。
我目前的做法是-
Map<Long, List<TeamHierarchy>> tree = new HashMap<>();
for (TeamHierarchy n : flatTeamStructure) {
if (n.getParentTeamId() == n.getTeamId()) {
if (!tree.containsKey(n.getParentTeamId())) {
tree.put(n.getParentTeamId(), new ArrayList<TeamHierarchy>());
}
} else {
if (!tree.containsKey(n.getParentTeamId())) {
tree.put(n.getParentTeamId(), new ArrayList<TeamHierarchy>());
}
tree.get(n.getParentTeamId()).add(n);
}
}
这并不完全正确,因为我在T1的孩子中也得到了T4。
我只想有直子。没有递归的任何建议都会有所帮助。
最佳答案
我不确定这是否是最有效的方法,但是它应该可以工作。我会尝试将每个团队ID映射到其适当的父对象。这里的困难是您的表包含多余的信息,因此您必须能够清除它。
这个想法是从根开始构建树,如果发现更深的树,则递归地修改父树。这是一个简单的独立程序示例,应该可以帮助您。
public class TestTree {
private static List<Entry> entries = new ArrayList<Entry>();
public static void main(String[] args) throws Exception {
// simulate the DB entries
entries.add(new Entry(1, 1, 1, "T1"));
entries.add(new Entry(2, 2, 2, "T2"));
entries.add(new Entry(3, 2, 1, "T2"));
entries.add(new Entry(4, 3, 1, "T3"));
entries.add(new Entry(5, 3, 3, "T3"));
entries.add(new Entry(6, 4, 4, "T4"));
entries.add(new Entry(7, 4, 2, "T4"));
entries.add(new Entry(8, 4, 1, "T4"));
// the root is the one entry with no parent other than self
int root = 1;
// map all relationships to the root
Map<Integer, Integer> tree = new HashMap<Integer, Integer>(); // ID -> parent ID
buildTree(tree, root);
System.out.println(tree);
// From this Map, it should be pretty obvious how to build the tree.
}
private static void buildTree(Map<Integer, Integer> tree, int parentId) {
boolean dirty = false;
for(Entry entry : entries) {
if(entry.parentId == parentId && entry.teamId != parentId) {
tree.put(entry.teamId, parentId);
dirty = true;
}
}
if(dirty) {
// Continue building the tree from each node that was updated
for(Integer nodeId : tree.keySet()) {
if(tree.get(nodeId) == parentId) buildTree(tree, nodeId);
}
}
}
private static class Entry {
int id;
int teamId;
int parentId;
String teamName;
Entry(int id, int teamId, int parentId, String teamName) {
this.id = id;
this.teamId = teamId;
this.parentId = parentId;
this.teamName = teamName;
}
}
更新资料
对于讨厌的递归方法(如果您的树太深了,以至于炸毁了方法调用堆栈):
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
public class TestTree {
private static List<Entry> entries = new ArrayList<Entry>();
public static void main(String[] args) throws Exception {
// simulate the DB entries
entries.add(new Entry(1, 1, 1, "T1"));
entries.add(new Entry(2, 2, 2, "T2"));
entries.add(new Entry(3, 2, 1, "T2"));
entries.add(new Entry(4, 3, 1, "T3"));
entries.add(new Entry(5, 3, 3, "T3"));
entries.add(new Entry(6, 4, 4, "T4"));
entries.add(new Entry(7, 4, 2, "T4"));
entries.add(new Entry(8, 4, 1, "T4"));
// the root is the one entry with no parent other than self
int root = 1;
// map all relationships to the root
Map<Integer, Integer> tree = new HashMap<Integer, Integer>(); // ID -> parent ID
Stack<Integer> stack = new Stack<Integer>();
stack.push(root);
do {
int parentId = stack.pop();
if(buildTree(tree, parentId)) {
// Continue building the tree from each node that was updated
for(Integer nodeId : tree.keySet()) {
if(tree.get(nodeId) == parentId) stack.push(nodeId);
}
}
} while(!stack.isEmpty());
System.out.println(tree);
// From this Map, it should be pretty obvious how to build the tree.
}
private static boolean buildTree(Map<Integer, Integer> tree, int parentId) {
boolean dirty = false;
for(Entry entry : entries) {
if(entry.parentId == parentId && entry.teamId != parentId) {
tree.put(entry.teamId, parentId);
dirty = true;
}
}
return dirty;
}
private static class Entry {
int id;
int teamId;
int parentId;
String teamName;
Entry(int id, int teamId, int parentId, String teamName) {
this.id = id;
this.teamId = teamId;
this.parentId = parentId;
this.teamName = teamName;
}
}
}
关于java - Java中将组织结构平移到树状 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38528636/