Java 将部门层次结构打印到表中
我有一个部门关系表,如下所示:Java 将部门层次结构打印到表中,java,sql,algorithm,hierarchy,Java,Sql,Algorithm,Hierarchy,我有一个部门关系表,如下所示: +---------------+----------------+ | Dept. | superior Dept. | +---------------+----------------+ | "00-01" | "00" | | "00-02" | "00" | | "00-01-01" | "00-01" | | "00-01-02" | "00-01
+---------------+----------------+
| Dept. | superior Dept. |
+---------------+----------------+
| "00-01" | "00" |
| "00-02" | "00" |
| "00-01-01" | "00-01" |
| "00-01-02" | "00-01" |
| "00-01-03" | "00-01" |
| "00-02-01" | "00-02" |
| "00-02-03" | "00-02" |
| "00-02-03-01" | "00-02-03" |
+---------------+----------------+
现在,我想按照它们的层次列出它们,如下所示:
+-----------+--------------+--------------+--------------+
| Top Dept. | 2-tier Dept. | 3-tire Dept. | 4-tier Dept. |
+-----------+--------------+--------------+--------------+
| 00 | | | |
| | 00-01 | | |
| | | 00-01-01 | |
| | | 00-01-02 | |
| | 00-02 | | |
| | | 00-02-01 | |
| | | 00-02-03 | |
| | | | 00-02-03-01 |
+-----------+--------------+--------------+--------------+
我可以使用下面的代码构建关系树:TreeNode.java
import java.util.LinkedList;
import java.util.List;
public class TreeNode {
public String value;
public List children = new LinkedList();
public TreeNode(String rootValue) {
value = rootValue;
}
}
import java.util.*;
public class PairsToTree {
public static void main(String[] args) throws Exception {
// Create the child to parent hash map
Map <String, String> childParentMap = new HashMap<String, String>(8);
childParentMap.put("00-01", "00");
childParentMap.put("00-02", "00");
childParentMap.put("00-01-01", "00-01");
childParentMap.put("00-01-02", "00-01");
childParentMap.put("00-01-03", "00-01");
childParentMap.put("00-02-01", "00-02");
childParentMap.put("00-02-03", "00-02");
childParentMap.put("00-02-03-01", "00-02-03");
// All children in the tree
Collection<String> children = childParentMap.keySet();
// All parents in the tree
Collection<String> values = childParentMap.values();
// Using extra space here as any changes made to values will
// directly affect the map
Collection<String> clonedValues = new HashSet();
for (String value : values) {
clonedValues.add(value);
}
// Find parent which is not a child to any node. It is the
// root node
clonedValues.removeAll(children);
// Some error handling
if (clonedValues.size() != 1) {
throw new Exception("More than one root found or no roots found");
}
String rootValue = clonedValues.iterator().next();
TreeNode root = new TreeNode(rootValue);
HashMap<String, TreeNode> valueNodeMap = new HashMap();
// Put the root node into value map as it will not be present
// in the list of children
valueNodeMap.put(root.value, root);
// Populate all children into valueNode map
for (String child : children) {
TreeNode valueNode = new TreeNode(child);
valueNodeMap.put(child, valueNode);
}
// Now the map contains all nodes in the tree. Iterate through
// all the children and
// associate them with their parents
for (String child : children) {
TreeNode childNode = valueNodeMap.get(child);
String parent = childParentMap.get(child);
TreeNode parentNode = valueNodeMap.get(parent);
parentNode.children.add(childNode);
}
// Traverse tree in level order to see the output. Pretty
// printing the tree would be very
// long to fit here.
Queue q1 = new ArrayDeque();
Queue q2 = new ArrayDeque();
q1.add(root);
Queue<TreeNode> toEmpty = null;
Queue toFill = null;
while (true) {
if (false == q1.isEmpty()) {
toEmpty = q1;
toFill = q2;
} else if (false == q2.isEmpty()) {
toEmpty = q2;
toFill = q1;
} else {
break;
}
while (false == toEmpty.isEmpty()) {
TreeNode node = toEmpty.poll();
System.out.print(node.value + ", ");
toFill.addAll(node.children);
}
System.out.println("");
}
}
}
PairsToTree.java
import java.util.LinkedList;
import java.util.List;
public class TreeNode {
public String value;
public List children = new LinkedList();
public TreeNode(String rootValue) {
value = rootValue;
}
}
import java.util.*;
public class PairsToTree {
public static void main(String[] args) throws Exception {
// Create the child to parent hash map
Map <String, String> childParentMap = new HashMap<String, String>(8);
childParentMap.put("00-01", "00");
childParentMap.put("00-02", "00");
childParentMap.put("00-01-01", "00-01");
childParentMap.put("00-01-02", "00-01");
childParentMap.put("00-01-03", "00-01");
childParentMap.put("00-02-01", "00-02");
childParentMap.put("00-02-03", "00-02");
childParentMap.put("00-02-03-01", "00-02-03");
// All children in the tree
Collection<String> children = childParentMap.keySet();
// All parents in the tree
Collection<String> values = childParentMap.values();
// Using extra space here as any changes made to values will
// directly affect the map
Collection<String> clonedValues = new HashSet();
for (String value : values) {
clonedValues.add(value);
}
// Find parent which is not a child to any node. It is the
// root node
clonedValues.removeAll(children);
// Some error handling
if (clonedValues.size() != 1) {
throw new Exception("More than one root found or no roots found");
}
String rootValue = clonedValues.iterator().next();
TreeNode root = new TreeNode(rootValue);
HashMap<String, TreeNode> valueNodeMap = new HashMap();
// Put the root node into value map as it will not be present
// in the list of children
valueNodeMap.put(root.value, root);
// Populate all children into valueNode map
for (String child : children) {
TreeNode valueNode = new TreeNode(child);
valueNodeMap.put(child, valueNode);
}
// Now the map contains all nodes in the tree. Iterate through
// all the children and
// associate them with their parents
for (String child : children) {
TreeNode childNode = valueNodeMap.get(child);
String parent = childParentMap.get(child);
TreeNode parentNode = valueNodeMap.get(parent);
parentNode.children.add(childNode);
}
// Traverse tree in level order to see the output. Pretty
// printing the tree would be very
// long to fit here.
Queue q1 = new ArrayDeque();
Queue q2 = new ArrayDeque();
q1.add(root);
Queue<TreeNode> toEmpty = null;
Queue toFill = null;
while (true) {
if (false == q1.isEmpty()) {
toEmpty = q1;
toFill = q2;
} else if (false == q2.isEmpty()) {
toEmpty = q2;
toFill = q1;
} else {
break;
}
while (false == toEmpty.isEmpty()) {
TreeNode node = toEmpty.poll();
System.out.print(node.value + ", ");
toFill.addAll(node.children);
}
System.out.println("");
}
}
}
import java.util.*;
公共类对树{
公共静态void main(字符串[]args)引发异常{
//创建子到父散列映射
Map childParentMap=newhashmap(8);
childParentMap.put(“00-01”、“00”);
childParentMap.put(“00-02”、“00”);
childParentMap.put(“00-01-01”、“00-01”);
childParentMap.put(“00-01-02”、“00-01”);
childParentMap.put(“00-01-03”、“00-01”);
childParentMap.put(“00-02-01”、“00-02”);
childParentMap.put(“00-02-03”、“00-02”);
childParentMap.put(“00-02-03-01”、“00-02-03”);
//树上所有的孩子
Collection children=childParentMap.keySet();
//树上所有的父母
集合值=childParentMap.values();
//在此处使用额外空间,因为对值所做的任何更改都将
//直接影响地图
Collection clonedValues=new HashSet();
for(字符串值:值){
克隆值。添加(值);
}
//查找不是任何节点的子节点的父节点。它是
//根节点
clonedValues.removeAll(儿童);
//一些错误处理
if(clonedValues.size()!=1){
抛出新异常(“找到多个根或未找到根”);
}
String rootValue=clonedValues.iterator().next();
树节点根=新树节点(根值);
HashMap valueNodeMap=新HashMap();
//将根节点放入值映射中,因为它将不存在
//在儿童名单上
valueNodeMap.put(root.value,root);
//将所有子项填充到valueNode映射中
for(字符串子项:子项){
TreeNode valueNode=新的TreeNode(子节点);
valueNodeMap.put(子级,valueNode);
}
//现在,映射包含树中的所有节点
//所有的孩子和孩子
//把他们和他们的父母联系起来
for(字符串子项:子项){
TreeNode childNode=valueNodeMap.get(子节点);
字符串parent=childParentMap.get(child);
TreeNode parentNode=valueNodeMap.get(父节点);
parentNode.children.add(childNode);
}
//按级别遍历树以查看输出
//打印这棵树会非常困难
//很想适应这里。
队列q1=new ArrayDeque();
队列q2=新的ArrayQueue();
q1.添加(根);
Queue toEmpty=null;
队列toFill=null;
while(true){
if(false==q1.isEmpty()){
toEmpty=q1;
toFill=q2;
}else if(false==q2.isEmpty()){
toEmpty=q2;
toFill=q1;
}否则{
打破
}
while(false==toEmpty.isEmpty()){
TreeNode节点=toEmpty.poll();
系统输出打印(node.value+“,”);
toFill.addAll(node.children);
}
System.out.println(“”);
}
}
}
但是我想不出来
了解如何将输出格式化为类似于表。或者是否有sql语句/存储过程(如)来执行此操作
编辑:为了方便起见,部门名称只是一个示例,可以是任意字符串。检查值计数
-
字符。最大-
字符数+1是表列数。值大小是行计数
您可以为表定义矩阵,例如String[][]
对于每个值,您可以找到列(计算字符串值中的-
字符)。
现在要查找行,您应该检查上一列以查找父字符串
如果找到了,只需在找到的父行之后插入新行,并向下移动所有剩余行
如果未找到父字符串,请首先插入父字符串(基于childParentMap)
如果parent为null,只需在末尾插入新行。import java.util.LinkedList;
import java.util.LinkedList;
import java.util.List;
public class TreeNode {
private String value;
private int depth;
private List children = new LinkedList<TreeNode>();
public TreeNode(String rootValue, int depth) {
value = rootValue;
this.depth = depth;
}
public boolean add(String parent, String child) {
if (this.value.equals(parent)) {
this.children.add(new TreeNode(child, depth + 1));
return true;
} else {
for (TreeNode node : children) {
if (node.add(parent, child)) {
return true;
}
}
}
return false;
}
public String print() {
StringBuilder sb = new StringBuilder();
sb.append(format());
for (TreeNode child : children) {
sb.append(child.print())
}
return sb.toString();
}
public String format() {
// Format this node according to depth;
}
}
导入java.util.List;
公共级树节点{
私有字符串值;
私有整数深度;
private List children=new LinkedList();
公共树节点(字符串根值,int深度){
值=根值;
这个。深度=深度;
}
公共布尔添加(字符串父项、字符串子项){
如果(此.value.equals(父级)){
添加(新的TreeNode(child,depth+1));
返回true;
}否则{
for(TreeNode节点:子节点){
if(node.add(父节点、子节点)){
返回true;
}
}
}
返回false;
}
公共字符串打印(){
StringBuilder sb=新的StringBuilder();
sb.append(format());
for(TreeNode儿童:儿童){
sb.append(child.print())
}
使某人返回字符串();
}
公共字符串格式(){
//根据深度设置该节点的格式;
}
}
首先打印Top Dept,然后在Top Dept上调用print()。这是一个基于数据的测试用例,它以表格的方式打印部门,试着运行它,看看会发生什么
package test;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class DeptHierachy {
public Map<String,List<String>> deptMap= new HashMap<>();
public static void main(String[] args) {
TreeNode tn=new TreeNode("00");
tn.addChildTo("00-01", "00");
tn.addChildTo("00-02", "00");
tn.addChildTo("00-01-01", "00-01");
tn.addChildTo("00-01-02", "00-01");
tn.addChildTo("00-01-03", "00-01");
tn.addChildTo("00-02-01", "00-02");
tn.addChildTo("00-02-03", "00-02");
tn.addChildTo("00-02-03-01", "00-02-03");
tn.print();
//System.out.println("max height=" + tn.maxHeigth());
}
public static class TreeNode {
public String value;
public List<TreeNode> children = new LinkedList<>();
public TreeNode(String rootValue) {
value = rootValue;
}
public boolean addChildTo(String childName, String parentName) {
if (parentName.equals(value)) {
for (TreeNode child:children) {
if (child.getValue().equals(childName)) {
throw new IllegalArgumentException(parentName + " already has a child named " + childName);
}
}
children.add(new TreeNode(childName));
return true;
} else {
for (TreeNode child:children) {
if (child.addChildTo(childName, parentName)) {
return true;
}
}
}
return false;
}
public String getValue() {
return value;
}
public void print() {
int maxHeight=maxHeigth();
System.out.printf("|%-20.20s",value);
for (int i=0;i<maxHeight-1;i++) {
System.out.printf("|%-20.20s","");
}
System.out.println("|");
for (TreeNode child:children) {
child.print(1,maxHeight);
}
}
public void print(int level, int maxHeight) {
for (int i=0;i<level;i++) {
System.out.printf("|%-20.20s","");
}
System.out.printf("|%-20.20s",value);
for (int i=level;i<maxHeight-1;i++) {
System.out.printf("|%-20.20s","");
}
System.out.println("|");
for (TreeNode child:children) {
child.print(level+1,maxHeight);
}
}
public int maxHeigth() {
int localMaxHeight=0;
for (TreeNode child:children) {
int childHeight = child.maxHeigth();
if (localMaxHeight < childHeight) {
localMaxHeight = childHeight;
}
}
return localMaxHeight+1;
}
}
}
封装测试;
导入java.util.HashMap;
导入java.util.LinkedList;
导入java.util.List;
导入java.util.Map;
公众阶级的堕落{
publicmap deptMap=newhashmap();
公共静态void main(字符串[]args){
TreeNode tn=新的TreeNode(“00”);
tn.addChildTo(“00-01”、“00”);
tn.addChildTo(“00-02”、“00”);
tn.addChildTo(“00-01-01”、“00-01”);
tn.addChildTo(“00-01-02”、“00-01”);
tn.addChildTo(“00-01-03”、“00-01”);
tn.addChildTo(“00-02-01”、“00-02”);
tn.addChildTo(“00-02-03”、“00-02”);
tn.addChildTo(“00-02-03-01”、“00-02-03”);
tn.print();
//System.out.println(“最大高度=+tn.maxHeigth());
}
公共静态类树节点{
公共字符串值;
public List children=new LinkedList();
公共树节点(字符串根值){
值=根值;
TopTier Tier2 Tier3 Tier4
00
00-01
00-01-01
00-01-02
00-01-03
00-02
00-02-01
00-02-03
00-02-03-01