递归树索引的顺序?

发布于 2024-10-03 16:51:00 字数 161 浏览 0 评论 0原文

我有一棵树,其中所有叶子都有索引,当树从数据库中递归加载时,数据库将按索引对树进行排序。首先它获取按索引排序的根节点等。现在我需要实现用户如何通过按向上/向下箭头图标对这些索引进行排序的操作。当用户按下时,索引应该采用其自己索引下的索引,当按下向上箭头时,反之亦然。我只是不知道实现这种功能的最佳方法是什么。

I have tree where all leafs have index, when tree is rescursively lodaded form database, database will order tree by indexes. First it gets root nodes sorted by index and so on. Now I need to implement action how user can sort these indexes by pressing up/down arrow icons. When user presses down then index should take index that is under it's own index and when up arrow is pressed it should do vice versa. I just don't know what would be best way to implement this kind of functionality.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾铮苏瑾 2024-10-10 16:51:00

由于你的问题有点模糊,这个答案假设你知道你在数据库方面做什么(如果不知道的话,我会推荐 hibernate for java),下面的代码旨在为你提供一些实现解决方案的想法。

//If I have understood your question, you want two nodes to swap position in the tree structure
public static swapNode(Node parent, Node child)
{
    Long superId = parent.getParentId();
    child.parentId(superId);
    parent.setParentId(child.getId());
    child.setId(parentId);
    //update children lists of parent and child
    //update parent ids of children lists

    //save changes to database
}

//create tree structure from database. Assumes nodes have been loaded from a database
//table where each row represents a node with a parent id column the root node which has parent id null)
//invoke this with all nodes and null for parentId argument
public static List<Node> createNodeTree(List<Node> allNodes, Long parentId)
{
    List<Node> treeList = new ArrayList<Node>();
    for(Node node : nodes)
    {
        if(parentIdMatches(node, parentId))
        {
            node.setChildren(createNodeTree(allNodes, node.getId()));
            treeList.add(node);
        }
    }
    return treeList;
}

private static boolean parentIdMatches(Node node, Long parentId)
{
    return (parentId != null && parentId.equals(node.getParentId())) 
        || (parentId == null && node.getParentId() == null);
}

//The objects loaded from the database should implement this interface
public interface Node
{
    void setParentId(Long id);
    Long getParentId();
    Long getId();
    List<Node> getChildren();
    void setChildren(List<Node> nodes);
}

Since your question is a bit vague, this answer assumes you know what you are doing when it comes to the database stuff (I would reccommend hibernate for java if not) and the following code is meant to give you some ideas for implementing your solution.

//If I have understood your question, you want two nodes to swap position in the tree structure
public static swapNode(Node parent, Node child)
{
    Long superId = parent.getParentId();
    child.parentId(superId);
    parent.setParentId(child.getId());
    child.setId(parentId);
    //update children lists of parent and child
    //update parent ids of children lists

    //save changes to database
}

//create tree structure from database. Assumes nodes have been loaded from a database
//table where each row represents a node with a parent id column the root node which has parent id null)
//invoke this with all nodes and null for parentId argument
public static List<Node> createNodeTree(List<Node> allNodes, Long parentId)
{
    List<Node> treeList = new ArrayList<Node>();
    for(Node node : nodes)
    {
        if(parentIdMatches(node, parentId))
        {
            node.setChildren(createNodeTree(allNodes, node.getId()));
            treeList.add(node);
        }
    }
    return treeList;
}

private static boolean parentIdMatches(Node node, Long parentId)
{
    return (parentId != null && parentId.equals(node.getParentId())) 
        || (parentId == null && node.getParentId() == null);
}

//The objects loaded from the database should implement this interface
public interface Node
{
    void setParentId(Long id);
    Long getParentId();
    Long getId();
    List<Node> getChildren();
    void setChildren(List<Node> nodes);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文