其他分享
首页 > 其他分享> > tree

tree

作者:互联网

package com.util;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 86150
 * Atree
 * 2021/3/31 21:19
 */
@Data
public class Atree {
    public Atree(String id, String name) {
        this.id = id;
        this.name = name;
    }

    private String id;
    private String name="";
    private List<Atree> childes = new ArrayList<>();
    private Atree parent;
    private boolean flag = false;

    public void addChild(Atree child) {
        childes.add(child);
        child.setParent(this);
    }

    public void removeChild(Atree chile) {
        childes.remove(chile);
    }


    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Atree> getChildes() {
        return childes;
    }

    public Atree getParent() {
        return parent;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    @Override
    public String toString() {
        return "Atree{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", flag=" + flag +
                ", childes=" + childes +
                '}';
    }
}

package com.util;

import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * 86150
 * TreeTest
 * 2021/3/31 21:35
 */
public class TreeTest {
    Atree root = new Atree("0","0");
    @Before
    public void build() {

        Atree c1 = new Atree("1","1");
        Atree c2 = new Atree("2","2");
        Atree c3 = new Atree("3","3");
        Atree c12 = new Atree("12","12");
        Atree c13 = new Atree("13","13");
        Atree c14 = new Atree("14","14");
        Atree c06 = new Atree("06","06");

        Atree c22 = new Atree("22","22");
        Atree c21 = new Atree("21","21");
        Atree c23 = new Atree("23","22");
        Atree c24 = new Atree("24","24");

        Atree c33 = new Atree("33","33");
        Atree c34 = new Atree("34","34");
        Atree c35 = new Atree("35","35");

        root.addChild(c1);
        root.addChild(c2);
        root.addChild(c3);

        c1.addChild(c12);
        c1.addChild(c13);
        c13.addChild(c14);
        c13.addChild(c06);

        c2.addChild(c22);
        c2.addChild(c21);
        c22.addChild(c23);
        c22.addChild(c24);

        c3.addChild(c33);
        c33.addChild(c34);
        c33.addChild(c35);
    }

    @Test
    public void test() {
        List<Atree> list = new ArrayList<>();
        TreeUtil.getAllLeaf(root, list);
        System.out.println(list);
        System.out.println(root);

        TreeUtil.flag(list, "1");
        System.out.println(root);

        TreeUtil.divide(root);
        System.out.println(root);

    }


}

package com.util;

import java.awt.print.PrinterAbortException;
import java.util.Iterator;
import java.util.List;

/**
 * 86150
 * TreeUtil
 * 2021/3/31 21:26
 */
public class TreeUtil {

    public static void getAllLeaf(Atree root, List<Atree> leafs) {
        if (root.getChildes().isEmpty()) {
            leafs.add(root);
            return;
        }
        for (Atree childe : root.getChildes()) {
            getAllLeaf(childe, leafs);
        }
    }

    public static void flag(List<Atree> leafs, String name) {
        for (Atree leaf : leafs) {
            String leafName = leaf.getName();
            if (leafName.contains(name)) {
                leaf.setFlag(true);
                flagParentAndBrother(leaf);
            }
        }
    }

    private static void flagParentAndBrother(Atree leaf) {
        if (leaf.getParent() != null) {
            Atree parent = leaf.getParent();
            setParentFlag(parent, true);
            List<Atree> brothers = parent.getChildes();
            for (Atree brother : brothers) {
                brother.setFlag(true);
            }
            flagParentAndBrother(parent);
        }
    }

    private static void setParentFlag(Atree parent, boolean b) {
        parent.setFlag(b);
        if (parent.getParent() != null) {
            setParentFlag(parent.getParent(), b);
        }
    }

    public static void divide(Atree root) {
        if (!root.isFlag()) {
            root = null;
        }
        List<Atree> childes = root.getChildes();
        Iterator<Atree> iterator = childes.iterator();
        while (iterator.hasNext()) {
            Atree child = iterator.next();
            if (!child.isFlag()) {
                iterator.remove();
            }
        }
        childes.forEach(c -> divide(c));

    }

}

标签:Atree,addChild,void,tree,new,root,public
来源: https://www.cnblogs.com/albertXe/p/14604325.html