对一棵树进行由下至上的操作

我这里遇到的问题是,我能获取到所有最末端叶子节点的count属性值。想要对整棵树的各个节点的count字段赋值,这个值就是每个子节点求和。思考了一天,最终用栈的特性解决了。在树的类里加了个visited属性进行辅助遍历。希望能帮助到大家。话不多说,直接上代码

package com.zdww.rocketmq.client.server.model;

import com.alibaba.fastjson.JSON;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @Description TODO
 * @Author whl
 * @Date 2020/1/13 15:59
 **/
public class Test {

    public static void main(String[] args) {

        Tree build1 = Tree.builder().code(1).children(new ArrayList<>()).build();
        Tree build2 = Tree.builder().code(2).children(new ArrayList<>()).build();
        Tree build5 = Tree.builder().code(5).children(new ArrayList<>()).build();
        Tree build3 = Tree.builder().code(3).children(new ArrayList<>()).build();
        Tree build4 = Tree.builder().code(4).children(new ArrayList<>()).build();
        Tree build6 = Tree.builder().code(6).children(new ArrayList<>()).build();
        Tree build7 = Tree.builder().code(7).children(new ArrayList<>()).build();

        build5.getChildren().add(build7);
        build5.getChildren().add(build6);
        build2.getChildren().add(build4);
        build2.getChildren().add(build3);
        build1.getChildren().add(build2);
        build1.getChildren().add(build5);

        System.out.println("===========树组装完成==============");
        Map<Integer, Integer> map = new HashMap<>(4);
        map.put(3, 2);
        map.put(4, 3);
        map.put(6, 9);
        map.put(7, 7);
        System.out.println("===========为树的最末端节点准备count值==============");
        Stack<Tree> stack = new Stack<>();
        stack.push(build1);
        System.out.println("===========使用栈的特性对树由下至上的为每个节点的count赋值==============");
        while (!stack.isEmpty()) {
            Tree pop = stack.peek();
            if (CollectionUtils.isEmpty(pop.getChildren())) {
                pop.setCount(map.get(pop.getCode()));
            }
            if (!CollectionUtils.isEmpty(pop.getChildren()) && !pop.isVisited()) {
                pop.setVisited(true);
                stack.addAll(pop.getChildren());
            } else {
                stack.pop();
                if (!CollectionUtils.isEmpty(pop.getChildren())) {
                    pop.setCount(pop.getChildren().stream().mapToInt(Tree::getCount).sum());
                }
            }
        }

        System.out.println(JSON.toJSONString(build1));

    }

}

package com.zdww.rocketmq.client.server.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * @Description TODO
 * @Author whl
 * @Date 2020/1/15 11:04
 **/

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Tree {

    //节点code
    private Integer code;
    //父节点
    private Integer pCode;
    //计数
    private Integer count;
    //访问过
    private boolean visited = false;

    private List<Tree> children;


}

发布了1 篇原创文章 · 获赞 0 · 访问量 9
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览