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 6821251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:31:23+00:00 2026-05-26T21:31:23+00:00

How do i find the previous and next element of any given element in

  • 0

How do i find the previous and next element of any given element in a tree that has sublists?

Example

  1. A

    1.1 AA

    1.1.1 AAA

    1.1.2 BBB

    1.1.3 CCC

1.2 DD

How do i get the previous and next id’s of 1.1.3 CCC ? I only know the ID of CCC at the given moment.

The real example i am working with is a Category entity which has a recursive association to itself because it can contain SubCategories. If i am in a level 3 subcategory, i would like to know the ID of the previous and next Category.

In my View (webpage) i have a list of all the categories. When i click on a Category, i only know it’s own id, but would like to get the previous and next id aswell.

I’ve used the following methods but they dont work when there a more levels:

private void GetNextCategoryID(PagedData<ShowQuestionViewModel> questionsPaged)
    {

        List<Category> categories = db.Category.Where(y => y.parrent_id == null).ToList();

        categories.Sort(new CompareCategory());

        List<ShowCategoriesViewModel> scvm = Mapper.Map<List<Category>, List<ShowCategoriesViewModel>>(categories);

        for (int i = 0; i < scvm.Count; i++)
        {
            if (scvm[i].category_id == questionsPaged.CategoryID)
            {
                if (scvm[i].SubCategories != null && scvm[i].SubCategories.Count > 0)
                {                          
                        questionsPaged.NextCategory_ID = scvm[i].SubCategories.First().category_id;
                        break;                   
                }

                try
                {
                    questionsPaged.NextCategory_ID = scvm[i + 1].category_id;
                    break;
                }
                catch (ArgumentOutOfRangeException ex)
                {
                    questionsPaged.NextCategory_ID = 0;
                    break;
                }

            }
            else if (scvm[i].SubCategories != null)
            {
                for (int q = 0; q < scvm[i].SubCategories.Count; q++)
                {
                    if (scvm[i].SubCategories[q].category_id == questionsPaged.CategoryID)
                    {
                        try
                        {
                            questionsPaged.NextCategory_ID = scvm[i].SubCategories[q + 1].category_id;
                            break;
                        }
                        catch (ArgumentOutOfRangeException ex)
                        {
                            // Betyder at vi er kommet til den sidste kategori i rækken
                            // og at den endnu ikke har fundet en match

                            try
                            {
                                questionsPaged.NextCategory_ID = scvm[i + 1].category_id;
                                break;

                            }
                            catch (ArgumentOutOfRangeException eq)
                            {
                                // Dette betyder at den valgte underkategori kategori er den sidste i spørgeskemaet
                                questionsPaged.NextCategory_ID = 0;
                                break;
                            }

                        }
                    }
                }
            }

        }
    }

and

private void GetPreviousCategoryID(PagedData<ShowQuestionViewModel> questionsPaged)
    {
        List<Category> categories = db.Category.Where(y => y.parrent_id == null).ToList();
        categories.Sort(new CompareCategory());
        List<ShowCategoriesViewModel> scvm = Mapper.Map<List<Category>, List<ShowCategoriesViewModel>>(categories);

        for (int i = scvm.Count - 1; i >= 0; i--)
        {
            if (scvm[i].category_id == questionsPaged.CategoryID)
            {
                try
                {
                    if (scvm[i - 1].SubCategories != null)
                    {
                        int subcount = scvm[i - 1].SubCategories.Count;
                        questionsPaged.PreviousCategory_ID = scvm[i - 1].SubCategories[subcount - 1].category_id;
                        break;
                    }
                    else
                    {
                        questionsPaged.PreviousCategory_ID = scvm[i - 1].category_id;
                    }

                }
                catch (ArgumentOutOfRangeException ex)
                {
                    questionsPaged.CategoryID = scvm[i].category_id;
                    break;
                }
            }

            else if (scvm[i].SubCategories != null)
            {
                for (int x = 0; x < scvm[i].SubCategories.Count; x++)
                {
                    try
                    {
                        if (scvm[i].SubCategories[x].category_id == questionsPaged.CategoryID)
                        {

                            questionsPaged.PreviousCategory_ID = scvm[i].SubCategories[x - 1].category_id;
                            break;
                        }
                    }
                    catch (ArgumentOutOfRangeException qx)
                    {
                        questionsPaged.PreviousCategory_ID = scvm[i].category_id;
                    }
                }
            }
        }
    }

Thanks!

  • 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-26T21:31:24+00:00Added an answer on May 26, 2026 at 9:31 pm

    In theory, what you should do is this: with the id of the current category, traverse the tree until you find the node with that id. Once here, to find the successor (I’m assuming the successor is to the right) you must go back up the tree: if the parent has a right child, find the leftmost node of the subtree rooted at that child, otherwise, apply this reasoning recursively to the parent of the parent and so on.

    To find the predecessor, just do the above steps but check if the parent has a left child and find the righmost node of that subtree, or apply recursively to the parent of the parent.

    // finds the successor of node
    procedure successor(node) 
    {
        // assuming you have the node for the id that you know.
        // also I'm assuming you have some way to find its position 
        // in the child list
    
        // if the parent has a child to the right
        if(parent(node).children[position(node) + 1] != null)
        {
            return findLeftmost(parent(node).children[position(node) + 1]);
            // find the leftmost node in the subtree rooted 
            // at the next sibling to the  right
        }
        else
        {
            return successor(parent(node));
            // move up the tree
        }
    }
    
    
    // finds the leftmost leaf node of the (sub)tree rooted at node
    procedure findLeftmost(node)
    {
        if(node.children.length == 0) // leaf node
        {
            return node;
        }
        else
        {
            return findLeftmost(node.children[0]); 
            // here I'm assuming the children are in the list from left to right, 
            // such that children[0] is the leftmost child.
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using Java LinkedList how do you find out the element's next or previous
with eZ Publish, when given an ezContentObjectTreeNode object, how can I find its previous/next
I remapped [[ and ]] to find the previous and next pattern in the
I have these previous/next a elements that tie in with the functions of jQuery
I have a Time object and would like to find the next/previous month. Adding
I have a database, and I want to find out the previous and next
I can't think of a simple and elegant way to find previous and next
Is there a way to find the previous and next sibling controls in an
i tried to find it out with previous answers but i cannot do it
I have already read all the previous similar posts but I couldn't find a

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.