public class RandomTreeUtils {
public static void main(String[] args) {
//
// /**
// * 随机种子1
// */
// char[] chars = new char[]{‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’,
// ‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’, ‘0’, ‘1’,
// ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’,
// ‘G’, ‘H’, ‘I’, ‘J’, ‘K’, ‘L’, ‘M’, ‘N’, ‘O’, ‘P’, ‘Q’, ‘R’, ‘S’, ‘T’,
// ‘U’, ‘V’, ‘W’, ‘X’, ‘Y’, ‘Z’};
//
// char[] chars = new char[]{‘0’, ‘1’,‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’};
//
char[] chars = new char[]{‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’, ‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’};
int treeDeeps = chars.length;
int key = 7;
// 最多生成7位数
if (treeDeeps > key) {
treeDeeps = key;
}
// 返回树形
ArrayList[] treeNodeList = treeNodeByChars(chars, treeDeeps);
// 循环输出 判断重复
loopTreeNode(treeNodeList, treeDeeps);
}
private static void loopTreeNode(ArrayList[] treeNodeList, int treeDeeps){
//树的子节点List
ArrayList list = treeNodeList[treeDeeps];
int count = 1;
Map map = new HashMap<>();
for(Node n : list){
String v = n.getNodeRouteString();
//出现重复校验
if(map.containsKey(v)){
System.out.println(count + "偶也,发现重复了 :" +n.getNodeRouteString());
}
count ++;
}
System.out.println("end .... " + count);
}
private static ArrayList[] treeNodeByChars(char[] chars, int treeDeeps){
//取值:位数 + 1
ArrayList[] treeNodeList = new ArrayList[treeDeeps + 1];
for(int i = 0 ; i < treeNodeList.length ;i++){
treeNodeList[i] = new ArrayList<>(64);
}
//创建跟节点.可以随便给个标识
Node root = new Node('#');
// 把跟节点加入树形集合的第一层
treeNodeList[0].add(root);
//从root(0) 到 第七层的 父节点需要处理,位数
for(int deep = 0 ; deep < treeDeeps; deep ++){
ArrayList currentNodeList = treeNodeList[deep];
ArrayList nextNodeList = treeNodeList[deep+1];
//遍历每一层的Node
for(int j = 0 ; currentNodeList != null && j < currentNodeList.size() ; j++){
Node currentNode = currentNodeList.get(j);
for(char v : chars){
//1. 如果路径已经使用过则不再使用
// TODO 需要处理:abcdefg 情况,连续字符的场景
if(currentNode.exist(v)){
continue;
}
//2.创建子节点
Node child = new Node(v,currentNode.getNodeRouteString(),cloneBooleanArr(currentNode.getRout()));
//3. 添加子节点
currentNode.getChildens().add(child);
//4. 添加自己节点路径
child.setRout(v);
//5. 将节点添加到下一辈List中
nextNodeList.add(child);
}
}
}
return treeNodeList;
}
public static boolean[] cloneBooleanArr(boolean[] re){
if(re == null || re.length == 0){
return new boolean[0];
}
boolean[] aa = new boolean[re.length];
for(int i = 0 ; i < re.length ; i++){
aa[i] = re[i];
}
return aa;
}
public static class Node{
public Node() {}
public Node(char value){
this.value = value;
}
public Node(char value,String pRoute,boolean[] rout) {
this.value = value;
this.rout = rout;
this.routeStr.append(pRoute);
}
/**
* 节点本身的值
*/
char value;
/**
* 子节点,最多有62个(就是种子的最大数目)
*/
List childens = new ArrayList<>(62);
/**
* 路径节点 路径有没有
*/
boolean[] rout = new boolean[128];
/**
* 路径顺序
*/
private StringBuilder routeStr = new StringBuilder();
public char getValue() {
return value;
}
public void setValue(char value) {
this.value = value;
}
public List getChildens() {
return childens;
}
/**
* 判断一个值是否存在,最小0是48, z:122, Z:90
* @param i
* @return
*/
public boolean exist(int i){
return rout[i];
}
/**
* 标识路径存在
* @param i
*/
public void setRout(int i){
rout[i] = true;
//添加路径
routeStr.append((char)i);
}
public boolean[] getRout() {
return rout;
}
public String getNodeRouteString() {
return this.routeStr.toString();
}
}
}