廣(寬)度優(yōu)先算法 BFS


import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author TheRaging
 * @Package cn.zjky.sme.conmon.util
 * @date 2022/2/7 15:14
 * @description
 */
@Component
public class TreeUtils<E> {


    /**
     * 通過反射獲取泛型的屬性值
     */
    private Long  getParentId(E e,String parentName) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Class clazz = e.getClass();
        //得到所有的屬性
        Field[] tableFields = clazz.getDeclaredFields();

        Long parentId = new Long(0);
        for (int i = 0; i < tableFields.length; i++) {
            //獲取屬性的名字
            String name = tableFields[i].getName();
            // 獲取屬性類型
            String type = tableFields[i].getGenericType().toString();
            //判斷類型 進(jìn)行強(qiáng)轉(zhuǎn)
            if(name.equals(parentName)){
                //將屬性名字的首字母大寫
                name = name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toUpperCase());
                //整合出 getXXX() 屬性這個方法
                Method m = clazz.getMethod("get"+name);
                //如果設(shè)置的類型是我需要的
               if(type.equals("class "+Long.class.getName())){
                   parentId = (Long) m.invoke(e);
                }else if(type.equals("class "+long.class.getName())){
                   parentId = (Long) m.invoke(e);
               }else if(type.equals("class "+Integer.class.getName())){
                   parentId = Long.valueOf(m.invoke(e).toString());
               }else if(type.equals("class "+int.class.getName())){
                   parentId =new Long((long)m.invoke(e));
               }else{
                   throw  new ClassCastException(parentName+":"+type+",支持的類型:Long,long,Integer,int");
               }
               //中斷循環(huán)
               break;
            }
        }

        if(parentId == null){
            throw  new NullPointerException("沒有找到"+parentName+"屬性");
        }
        return parentId;
    }




    /**
     * @param: list 集合 所有的數(shù)據(jù)
     * @param: parentname  代表父ID的屬性名
     * @param: idName 代表id的屬性名
     * @description: 
     * @return: java.util.List<E>
     * @author: TheRaging
     * @date: 2022/2/7 
     */
    public List<E> getAllByid(List<E> list ,Long id, String parentname ,String idName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        //list為null
        if(list.size()<1){
            return list;
        }
        //E 保證的有id和parentid
        E first = null;
        for(E e : list){
            Long ids = this.getParentId(e,idName);
            if(ids.equals(id)){
                //終止循環(huán)
                first = e;
                break;
            }
        }
        if(first == null ){
            throw new NullPointerException("集合中不存在id:"+id+"的數(shù)據(jù)");
        }

        //廣(寬)度優(yōu)先算法 BFS
        List<E> result = new ArrayList<>();
        Stack<E> head  = new  Stack();
        head.push(first);
        while (!head.isEmpty()){
            //先彈出頭部
            E e = head.pop();
            //查詢有幾個子節(jié)點(diǎn)
            for (E obj: list) {
               if(getParentId(obj, parentname).equals(getParentId(e, idName))){
                   //有子節(jié)點(diǎn) 壓進(jìn)去棧里邊
                   head.push(obj);
                   //子節(jié)點(diǎn)添加進(jìn)集合里邊
                   result.add(obj);
               }
            }
        }
        return result;
    }

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容