多叉树操作

需求一、查询当前群的所有子级群组

 

 

递归 遍历N叉树,获取数据

 /**
     * 获取子级群列表信息
     * @param roomId
     * @param inside
     * @return
     */
    @Override
    public TreeRoom getSubGroup(ObjectId roomId, int inside) {
        Room room = roomCoreDao.get(roomId);
        if(null == room){
            return null;
        }
        TreeRoom treeRoom = new TreeRoom(room.getId(),room.getLogo(),room.getJid(),room.getGroupId(),room.getName(),room.getType(),room.getInside(),room.getParentId(),null);
        return recursion(treeRoom,inside);
    }
    
    /**
     * 遍历多叉树节点(向下遍历)
     * @param treeRoom 根节点
     * @param inside -1不区分内外部群  0内部群 1外部群
     * @return
     */
    public TreeRoom recursion(TreeRoom treeRoom,int inside){
        List<TreeRoom> list = null;
        TreeRoom subRoom = null;
        List<Room> rooms = roomCoreDao.getRoomByParentId(treeRoom.getId());
        if (null != rooms && rooms.size()>0){
            list = new ArrayList<>(rooms.size());
            for (int i = 0; i < rooms.size(); i++) {

                if(rooms.get(i).getInside() == inside || inside == -1){
                    subRoom = new TreeRoom(rooms.get(i).getId(),rooms.get(i).getLogo(),rooms.get(i).getJid(),rooms.get(i).getGroupId(),rooms.get(i).getName(),rooms.get(i).getType(),rooms.get(i).getInside(),rooms.get(i).getParentId(),null);
                }
                if(null == subRoom){
                    break;
                }
                if(treeRoom.getSubRoom() == null){
                    list.add(subRoom);
                    treeRoom.setSubRoom(list);
                }else {
                    treeRoom.getSubRoom().add(subRoom);
                }
                // 递归处理子级群
                recursion(subRoom,inside);
            }
        }
        return treeRoom;
    }

 

 

返回数据

{
  "currentTime": 1637218489567,
  "data": {
    "groupId": "100000005371",
    "id": "618b747e55bae6217cca507b",
    "inside": 0,
    "jid": "e349b7bc809c46e1b8614687a04e5201",
    "name": "太极马包谷官方群",
    "subRoom": [
      {
        "groupId": "100000002330",
        "id": "618ce53889180154e0733bd0",
        "inside": 1,
        "jid": "c686735712924e47b09fd2f57262227c",
        "name": "马太极主群",
        "parentId": "618b747e55bae6217cca507b",
        "subRoom": [
          {
            "groupId": "100000005301",
            "id": "6195f86b06eccf39ceb04ad2",
            "inside": 1,
            "jid": "42a71b84483b422d9e641d254bfdec3d",
            "name": "马太极分群1",
            "parentId": "618ce53889180154e0733bd0",
            "type": 12
          },
          {
            "groupId": "100000007758",
            "id": "6195f8a806eccf39ceb04ad6",
            "inside": 1,
            "jid": "366fba389cac4370941fde3885d94ecf",
            "name": "马太极分群3",
            "parentId": "618ce53889180154e0733bd0",
            "type": 12
          }
        ],
        "type": 11
      },
      {
        "groupId": "100000005717",
        "id": "618cecb43c48063a72efde44",
        "inside": 0,
        "jid": "01e9ff3f159c4134a9327d536cf9611b",
        "name": "马太极主群2",
        "parentId": "618b747e55bae6217cca507b",
        "subRoom": [
          {
            "groupId": "100000006522",
            "id": "618ced113c48063a72efde49",
            "inside": 0,
            "jid": "9a13f53c573e4c418704052ca8c4e8aa",
            "name": "马太极分群2",
            "parentId": "618cecb43c48063a72efde44",
            "subRoom": [
              {
                "groupId": "100000006107",
                "id": "618ced5b3c48063a72efde4e",
                "inside": 0,
                "jid": "5f3decd59f5446f5a7934fa36b957cbe",
                "name": "马太极支群2",
                "parentId": "618ced113c48063a72efde49",
                "subRoom": [
                  {
                    "groupId": "100000009504",
                    "id": "618ced803c48063a72efde52",
                    "inside": 0,
                    "jid": "c7ef71e5912444839a9e783b827c6349",
                    "name": "马太极子群2",
                    "parentId": "618ced5b3c48063a72efde4e",
                    "type": 14
                  }
                ],
                "type": 13
              }
            ],
            "type": 12
          }
        ],
        "type": 11
      }
    ],
    "type": 2
  },
  "resultCode": 1
}

 

需求二、模糊搜索展示群组信息,并附带它的父级群组信息

 

 

先模糊查询群组信息,再向上遍历补全父级节点数据

 /**
     * 向上遍历补全父级数据
     * @param
     * @return
     */
    public List<TreeRoom> upTraversal(List<TreeRoom> treeRooms){
        TreeRoom parentRoom = null;
        Room parent = null;
        List<TreeRoom> list = null;
        if(null != treeRooms && treeRooms.size() > 0){
            for (int i = 0; i < treeRooms.size(); i++) {
                if(treeRooms.get(i).getType() != 2){ //判断不是祖级
                    parent = roomCoreDao.getRoomById(treeRooms.get(i).getParentId());
                    parentRoom = new TreeRoom(parent.getId(),parent.getLogo(),parent.getJid(),parent.getGroupId(),parent.getName(),parent.getType(),parent.getInside(),parent.getParentId(),null);
                    list = new ArrayList<>(1);
                    list.add(new TreeRoom(treeRooms.get(i).getId(),treeRooms.get(i).getLogo(),treeRooms.get(i).getJid(),treeRooms.get(i).getGroupId(),treeRooms.get(i).getName(),treeRooms.get(i).getType(),treeRooms.get(i).getInside(),treeRooms.get(i).getParentId(),treeRooms.get(i).getSubRoom()));
                    parentRoom.setSubRoom(list);
                    treeRooms.remove(i);
                    treeRooms.add(i,parentRoom);
                    if(parent.getType() != 2){
                        upTraversal(treeRooms);
                    }
                }
            }
        }
        return treeRooms;
    }

对于同一个祖级节点下的同名群组,存在数据重复的问题,因此要去重

{
      "groupId": "100000005371",
      "id": "618b747e55bae6217cca507b",
      "inside": 0,
      "jid": "e349b7bc809c46e1b8614687a04e5201",
      "name": "太极马包谷官方群",
      "type": 2
    },
    {
      "groupId": "100000005371",
      "id": "618b747e55bae6217cca507b",
      "inside": 0,
      "jid": "e349b7bc809c46e1b8614687a04e5201",
      "name": "太极马包谷官方群",
      "subRoom": [
        {
          "groupId": "100000002330",
          "id": "618ce53889180154e0733bd0",
          "inside": 1,
          "jid": "c686735712924e47b09fd2f57262227c",
          "name": "马太极主群",
          "parentId": "618b747e55bae6217cca507b",
          "type": 11
        }
      ],
      "type": 2
    },
    {
      "groupId": "100000005371",
      "id": "618b747e55bae6217cca507b",
      "inside": 0,
      "jid": "e349b7bc809c46e1b8614687a04e5201",
      "name": "太极马包谷官方群",
      "subRoom": [
        {
          "groupId": "100000005717",
          "id": "618cecb43c48063a72efde44",
          "inside": 0,
          "jid": "01e9ff3f159c4134a9327d536cf9611b",
          "name": "马太极主群2",
          "parentId": "618b747e55bae6217cca507b",
          "type": 11
        }
      ],
      "type": 2
    }

可以看到,当搜索“太极”时,这三条数据其实都是一个祖级节点。需要合并成一条数据。

就此打住,这个实现复杂不说,后台处理大量数据也影响性能。因此增加群层级的类,保存到数据库,直接查询比较简单快捷。

 

posted @ 2021-11-18 15:04  别动我的猫  阅读(232)  评论(0编辑  收藏  举报