打印父子菜单

static class Tree_print_menu{

    static class MenuItem {
        public int id;
        public String name;
        public int parent = -1;//-1表示顶层

        public MenuItem() {
        }

        public MenuItem(int id, String name, int parent) {
            this.id = id;
            this.name = name;
            this.parent = parent;
        }
    }

    public void print(List menuItemList) {
        Map topNodeMap = new HashMap<>();
        Map sonNodeMap = new HashMap<>();
        for (MenuItem menuItem : menuItemList) {
            if (menuItem.parent == -1) {//top
                MTreeNode node = new MTreeNode(new HashSet<>(), null, menuItem);
                topNodeMap.put(menuItem.id, node);
            } else {
                MTreeNode node = new MTreeNode(new HashSet<>(), null, menuItem);
                sonNodeMap.put(menuItem.id, node);
            }
        }
        for (Map.Entry entry : sonNodeMap.entrySet()) {
            //int selfId=entry.getKey();
            MTreeNode node = entry.getValue();
            buildParentChain(node, sonNodeMap, topNodeMap);

        }
        //build over,print start
        for (Map.Entry entry : topNodeMap.entrySet()) {
            print0(entry.getValue(), 0);
        }
    }

    private void print0(MTreeNode node, int floor) {

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < floor; i++) {
            sb.append("    ");
        }
        System.out.println(sb.toString() + "-id[" + node.self.id + "] -pid[" + node.self.parent + "] -name[" + node.self.name + "]");

        for (MTreeNode mTreeNode : node.sonSet) {
            print0(mTreeNode, floor + 1);
        }
    }

    private void buildParentChain(MTreeNode selfNode, Map sonNodeMap,
                                  Map topNodeMap) {
        MTreeNode topFather = topNodeMap.get(selfNode.self.parent);
        if (topFather == null) {
            MTreeNode sonFather = sonNodeMap.get(selfNode.self.parent);
            sonFather.sonSet.add(selfNode);
            if (sonFather.father == null) {
                buildParentChain(sonFather, sonNodeMap, topNodeMap);
            }
        } else {
            topFather.sonSet.add(selfNode);
        }
    }

    public static class MTreeNode {
        public Set sonSet;
        public MTreeNode father;
        public MenuItem self;

        public MTreeNode(Set sonSet, MTreeNode father, MenuItem self) {
            this.sonSet = sonSet;
            this.father = father;
            this.self = self;
        }
    }
}
测试
Solution.Tree_print_menu solu=new Solution.Tree_print_menu();
LinkedList ipp=new LinkedList<>();
ThreeConsumer consumer=(id,pid,name)->{
    ipp.add(new Solution.Tree_print_menu.MenuItem(id,name,pid));
};
consumer.accept(1,-1,"menu");
consumer.accept(2,-1,"menu");
consumer.accept(3,1,"menu");
consumer.accept(4,1,"menu");
consumer.accept(5,3,"menu");
consumer.accept(6,3,"menu");
consumer.accept(7,6,"menu");
consumer.accept(8,4,"menu");
consumer.accept(9,4,"menu");
consumer.accept(10,5,"menu");
solu.print(ipp);
输出
-id[1] -pid[-1] -name[menu]
    -id[4] -pid[1] -name[menu]
        -id[9] -pid[4] -name[menu]
        -id[8] -pid[4] -name[menu]
    -id[3] -pid[1] -name[menu]
        -id[5] -pid[3] -name[menu]
            -id[10] -pid[5] -name[menu]
        -id[6] -pid[3] -name[menu]
            -id[7] -pid[6] -name[menu]
-id[2] -pid[-1] -name[menu]

你可能感兴趣的:(打印父子菜单)