构造帖子的树形回复

需求: 回复分为两类, 一类是回复帖子的回复(这里记为0), 一类是回复 “回复” 的回复(这里记为1), 现在需要把这些回复构造成一颗树形回复, 也就是, '0’回复在树的第一层, '1’回复都在树的下层.

如图所示:
在这里插入图片描述r代表树的根节点, 1,2,3代表’0’回复, 4, 5代表回复1的回复

下面是代码

//树类
import java.util.LinkedList;
import java.util.List;

public class ReplyTree
{
    private ReplyVO reply;
    private List<ReplyTree> children;

    public ReplyTree()
    {
        this.reply = new ReplyVO();
        this.children = new LinkedList<>();
    }

    public ReplyTree(ReplyVO reply)
    {
        this.reply = reply;
        this.children = new LinkedList<>();
    }

    public ReplyVO getReply()
    {
        return reply;
    }
    public void setReply(ReplyVO reply)
    {
        this.reply = reply;
    }
    public List<ReplyTree> getChildren()
    {
        return children;
    }
    public void setChildren(List<ReplyTree> children)
    {
        this.children = children;
    }

    /**
     * 向parent代表的节点下插入一个子节点
     * @param parent
     * @param reply
     * @return
     */
    public ReplyTree add(ReplyTree parent, ReplyVO reply)
    {
        ReplyTree child = new ReplyTree(reply);
        parent.children.add(child);
        return child;
    }

    /**
     * 在parent的子结点中寻找reply是指定的reply的节点
     * @param parent
     * @param reply
     * @return
     */
    public ReplyTree findChild(ReplyTree parent, ReplyVO reply)
    {
        Integer id = reply.getReply().getId();
        for (ReplyTree child : parent.getChildren())
        {
            if (child.getReply().getReply().getId() == id)
            {
                return child;
            }
        }
        return null;
    }
}

将list转为树的工具类

import com.lost_found.VO.ReplyTree;
import com.lost_found.VO.ReplyVO;
import com.lost_found.dao.UserMapper;
import com.lost_found.pojo.Reply;
import com.lost_found.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Component
public class TreeUtil
{
    @Autowired
    UserMapper userMapper;

    public static TreeUtil treeUtil;
    
    @PostConstruct
    public void init()
    {
        treeUtil = this;
    }

    public static ReplyTree getTree(List<Reply> replyList)
    {
        ReplyTree parent = new ReplyTree();
        List<Reply> list = new ArrayList<>(replyList);
        TrasnferToTree(parent, replyList, list);

        return parent;
    }

    /**
     * 将list转化为树
     * @param parent
     * @param replyList
     * @param list
     */
    public static void TrasnferToTree(ReplyTree parent, List<Reply> replyList, List<Reply> list)
    {
        if (list.isEmpty())
            return;

        //因为需要边迭代边删除, 所以使用迭代器
        Iterator<Reply> iterator = replyList.iterator();
        while (iterator.hasNext())
        {
            Reply reply = iterator.next();
            User user = treeUtil.userMapper.selectByPrimaryKey(reply.getUserId());
            ReplyVO replyVO = new ReplyVO(reply, user.getHeadPortrait(), user.getNickName(), reply.getType());

            //是回复帖子的回复, 而且现在的父节点为null, 则说明应该插入该节点
            if (reply.getType() == 0 && parent.getReply().getReply() == null)
            {
                parent.add(parent, replyVO);
                list.remove(reply);
                if (isLeaf(replyVO, replyList))
                    continue;
                TrasnferToTree(parent.findChild(parent, replyVO), replyList, list);
            }
            //如果是回复回复的回复且该节点的回复确实是回复父节点的回复, 则插入
            else if (parent.getReply().getReply() != null && reply.getType() == 1 && reply.getReplyId() == parent.getReply().getReply().getId())
            {
                parent.add(parent, replyVO);
                list.remove(reply);
                TrasnferToTree(parent.findChild(parent, replyVO), replyList, list);
            }
        }
    }

    /**
     * 判断是否是叶子节点
     * @param replyVO
     * @param replyList
     * @return
     */
    public static boolean isLeaf(ReplyVO replyVO, List<Reply> replyList)
    {
        boolean flag = true;
        for (Reply reply : replyList)
        {
            if (reply.getReplyId() == replyVO.getReply().getId())
                flag = false;
        }
        return flag;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaScript中,可以使用对象或数组来构造树形结构数据。其中,对象表示树的节点,通过属性来表示节点的值和子节点。数组表示节点的集合,通过索引来表示节点的位置。 例如,可以使用以下代码构造一个树形结构的数据: ```javascript const tree = { value: 'A', children: [ { value: 'B', children: [ { value: 'D', children: [] }, { value: 'E', children: [] } ] }, { value: 'C', children: [ { value: 'F', children: [] } ] } ] }; ``` 在这个例子中,树的根节点是'A',它有两个子节点'B'和'C'。节点'B'有两个子节点'D'和'E',节点'C'有一个子节点'F'。每个节点都有一个value属性表示节点的值,以及一个children属性表示子节点的集合。 通过这种方式,我们可以灵活地构造树形结构的数据,并且方便地进行遍历和操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [【JS】数据结构之树结构](https://blog.csdn.net/qq_45677671/article/details/127905453)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [JavaScript实现树结构(一)](https://blog.csdn.net/qq_42198495/article/details/108014346)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值