Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3422326
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:12:12+00:00 2026-05-18T06:12:12+00:00

I have tree where all leafs have index, when tree is rescursively lodaded form

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:12:12+00:00Added an answer on May 18, 2026 at 6:12 am

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tree, which contains nodes, after I've created them, I take all
I have constructed a suffix trie, a tree containing all the suffixes of a
I have a category tree and i'd like to get all products that are
I have a red-black tree (binary tree, all the leaves are within 2 levels).
I have a tree(jstree) inside a div, so when a node gets expanded the
I have a tree table with two columns on a page. The first one
I have an assignment where I have a tree where all the parent nodes
I have a hierarchical query in Oracle 11g that gets me all my leaf
This is not a homework. Visually it looks like a tree, but all leafs
I have a table in my database which stores a tree structure. Here are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.