数据结构-Java树形结构两种组装算法对比

相信大家对树结构都不陌生。这里不做冗余介绍,如果有不懂的请自行google
学习。这里只介绍我们在实际中会经常用到的组装树的简单算法,仅供参考,欢迎讨论。

JAVA实现

基本对象

public class Node implements Serializable {

    private long id;
    private Long parentId;
    private List<Node> childs;

    public Node(long id, Long parentId, List<Node> childs) {
        this.id = id;
        this.parentId = parentId;
        this.childs = childs;
    }

    public Node(long id, Long parentId) {
        this.id = id;
        this.parentId = parentId;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public List<Node> getChilds() {
        return childs;
    }

    public void setChilds(List<Node> childs) {
        this.childs = childs;
    }

    public void addChild(Node node){
        if(childs == null){
            childs = new ArrayList<>();
        }
        childs.add(node);
    }
}

第一种
这种也是最常用的方式:

private static List<Node> getTree1(List<Node> nodes) {
        List<Node> res = new ArrayList<>();
        Map<Long, Node> catalogMap = new HashMap<>();
        for (Node c : nodes) {
            if (c.getParentId() == null) {
                res.add(c);
            }
            catalogMap.put(c.getId(), c);
        }
        for (Map.Entry entry : catalogMap.entrySet()) {
            Node parent = catalogMap.get(entry.getKey());
            if (parent != null) {
                parent.addChild((Node) entry.getValue());
            } else {
                res.add((Node) entry.getValue());
            }
        }
        return res;
    }

第二种
以空间换时间,增加一个记录父节点的parentMap,二次循环遍历这个parentMap,来减少遍历次数。不过JVM维护parentMap本身也需要耗费时间,这种方式要分情况使用。

private static List<Node> getTree2(List<Node> nodes) {
        List<Node> res = new ArrayList<>();
        Map<Long, List<Node>> parentMap = new HashMap<>();
        Map<Long, Node> catalogMap = new HashMap<>();
        for (Node c : nodes) {
            if (c.getParentId() == null) {
                res.add(c);
            } else {
                parentMap.compute(c.getParentId(), new BiFunction<Long, List<Node>, List<Node>>() {
                    @Override
                    public List<Node> apply(Long aLong, List<Node> nodes) {
                        if (nodes == null) {
                            nodes = new ArrayList<>();
                        }
                        nodes.add(c);
                        return nodes;
                    }
                });
            }
            catalogMap.put(c.getId(), c);
        }
        for (Map.Entry entry : parentMap.entrySet()) {
            Node parent = catalogMap.get(entry.getKey());
            if (parent != null) {
                parent.setChilds((List<Node>) entry.getValue());
            } else {
                res.addAll((Collection<? extends Node>) entry.getValue());
            }
        }
        return res;
    }

执行时间

经测试,在list大小小于等于50W时,第一种快。在list大小大于50W时,第二种快。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前鼻音太阳熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值