java递归 tree_java 实现递归实现tree(2)

import com.google.common.collect.Lists;

import org.springframework.cglib.beans.BeanCopier;

import java.util.*;

import java.util.stream.Collectors;

public class tset {

private static Map> map = new HashMap<>();

static {

map.put("2", Lists.newArrayList("A", "B", "C"));

map.put("3", Lists.newArrayList("D", "E", "F"));

map.put("4", Lists.newArrayList("G", "H", "I"));

map.put("5", Lists.newArrayList("J", "K", "L"));

map.put("6", Lists.newArrayList("M", "N", "O"));

map.put("7", Lists.newArrayList("P", "Q", "R", "S"));

map.put("8", Lists.newArrayList("T", "U", "V"));

map.put("9", Lists.newArrayList("W", "X", "Y", "Z"));

}

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

Map> nodeMap;

while (true) {

nodeMap = new HashMap<>();

String num = scanner.next();

char[] chars = num.toCharArray();

LinkedList list = new LinkedList<>();

if (chars.length == 1) {

list.addAll(map.get(String.valueOf(chars[0])).stream().map(Node::new).collect(Collectors.toList()));

} else {

for (int i = 0; i < chars.length - 1; i++) {

char aChar = chars[i];

List collect0 = nodeMap.computeIfAbsent(String.valueOf(aChar), a -> map.get(String.valueOf(aChar)).stream().map(Node::new).collect(Collectors.toList()));

if (i == 0) {

list.addAll(collect0);

}

List collect = map.get(String.valueOf(chars[i + 1])).stream().map(Node::new).collect(Collectors.toList());

nodeMap.put(String.valueOf(chars[i + 1]), collect);

for (Node node : collect0) {

node.children = collect;

}

}

}

String name;

String preName;

List result = new ArrayList<>();

while (!list.isEmpty()) {

Node node = list.removeFirst();

name = node.name;

preName = node.preName != null ? node.preName + name : name;

if (node.children != null) {

for (Node node1 : node.children) {

Node newNode = new Node();

BeanCopier beanCopier = BeanCopier.create(node1.getClass(), newNode.getClass(), false);

beanCopier.copy(node1, newNode, null);

newNode.preName = preName;

if (newNode.children != null) {

list.add(newNode);

} else {

result.add(preName + newNode.name);

}

}

} else {

result.add(name);

}

}

System.out.println(result);

}

}

public static class Node {

String name;

String preName;

List children;

Node() {

}

Node(String name) {

this.name = name;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPreName() {

return preName;

}

public void setPreName(String preName) {

this.preName = preName;

}

public List getChildren() {

return children;

}

public void setChildren(List children) {

this.children = children;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值