package com.hulinjun.springbootdemo.util;
import java.util.*;
public class GuoRuiUtil {
public static void main(String[] args) {
/*List<Map<String, String>> map1 = create(2, 5,3);
List<Map<String,String>> o = new ArrayList<>();
o.addAll(map1);
List<Map<String, String>> map2 = create(3, 5,2);
o.addAll(map2);
//假设出现交叉
//o.addAll(create(200000004,20));
String initId = "6";*/
// List<Map<String, String>> map1 = create(2, 3,3);
List<Map<String,String>> o = new ArrayList<>();
// o.addAll(map1);
// List<Map<String, String>> map2 = create(3, 5,2);
// o.addAll(map2);
//假设出现交叉
//o.addAll(create(200000004,20));
Map m = new HashMap();
m.put("FROM","13800001538726"+"");
m.put("TO","13800012502252"+"");
o.add(m);
Map m1 = new HashMap();
m1.put("FROM","13800012502252"+"");
m1.put("TO","13800006454281"+"");
o.add(m1);
Map m2 = new HashMap();
m2.put("FROM","13310000001234"+"");
m2.put("TO","13320000000091"+"");
o.add(m2);
Map m3 = new HashMap();
m3.put("FROM","13800006454281"+"");
m3.put("TO","13800002096649"+"");
o.add(m3);
Map m4= new HashMap();
m4.put("FROM","13320000000091"+"");
m4.put("TO","13320000000091"+"");
o.add(m4);
Map m5 = new HashMap();
m5.put("FROM","13800002096649"+"");
m5.put("TO","13320000000091"+"");
o.add(m5);
Map m6 = new HashMap();
m6.put("FROM","13800023928215"+"");
m6.put("TO","13310000001234"+"");
o.add(m6);
Map m7 = new HashMap();
m7.put("FROM","13800009355365"+"");
m7.put("TO","13800023928215"+"");
o.add(m7);
Map m8 = new HashMap();
m8.put("FROM","13800003107424"+"");
m8.put("TO","13800009355365"+"");
o.add(m8);
Map m9 = new HashMap();
m9.put("FROM","13800003107426"+"");
m9.put("TO","13800012502252"+"");
o.add(m9);
Map m10 = new HashMap();
m10.put("FROM","13800003107426"+"");
m10.put("TO","13800003107427"+"");
o.add(m10);
String initId = "13800006454281";
Map<String,ListNode> d = new HashMap<>();
find(initId,o,d);
ListNode listNode = d.get(initId);
List<String[]> strings = listNode.toList();
for (String[] string : strings) {
System.out.println(Arrays.toString(string));
}
}
static void find(String initId , List<Map<String,String>> o,Map<String,ListNode> d){
for (int i = o.size() -1; i >= 0 ; i--) {
Map<String, String> m = o.get(i);
String from = m.get("FROM");//获取到父节点
String to = m.get("TO");//获取到子节点
if(from.equals(to))continue;//如果重复会出现闭环
if(d.get(from)==null)d.put(from,new ListNode(from));//备份
if(d.get(to)==null)d.put(to,new ListNode(to));//备份
i = findInner(initId, o, d, i, from, to);
i = findInner(initId, o, d, i, to, from);
}
}
private static int findInner(String initId, List<Map<String, String>> o, Map<String, ListNode> d, int i, String from, String to) {
if (initId.equals(from)) {//如果找到初始ID
//从dataMap里捞出变量 将他的next节点设置为to //添加一条链子
d.get(from).add(d.get(to));
o.remove(i);//移出数据
i--;
//开始递归
find(to, o, d);
}
return i;
}
static List<Map<String,String>> create(int initId ,int size, int addNum){
List<Map<String,String>> list = new ArrayList<>();
boolean flag = false;
for (int i = 0; i < size; i++) {
Map m = new HashMap();
if(!flag){
m.put("FROM",initId+"");
initId*=addNum;
m.put("TO",initId+"");
}else{
m.put("TO",initId+"");
initId*=addNum;
m.put("FROM",initId+"");
}
flag = !flag;
list.add(m);
}
return list;
}
}
class ListNode{
private String id;
private List<ListNode> nexts = new ArrayList<>() ;
public ListNode(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<ListNode> getNext() {
return nexts;
}
public void setNext(List<ListNode> nexts) {
this.nexts = nexts;
}
public void add(ListNode next){
nexts.add(next);//添加一条链子
}
public List<String[]> toList(){
LinkedList<String>nodess = new LinkedList<>();
List<String[]> result = new ArrayList<>();
try {
nodess.add(this.getId());//一条链子都没有
exec(nodess,this);
result = new ArrayList<>();
for (String nodesInfo : nodess) {
System.out.println(nodesInfo);
result.add(nodesInfo.split("==>"));
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public void exec(LinkedList<String> list,ListNode node){
int size = node.nexts.size();
if(size > 0){
//先取出原来的数据
String s = list.removeLast();
for (int i = 0; i < size; i++) {
ListNode listNode = node.nexts.get(i);
list.add(s+"==>"+listNode.getId());//加入数据
exec(list,listNode);
}
}
}
}
最后
以上就是落后魔镜最近收集整理的关于网状数据结构的全部内容,更多相关网状数据结构内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复