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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:07:59+00:00 2026-06-05T06:07:59+00:00

Good day pals! Currently I’m studying this database model design called Modified Pre-order Tree

  • 0

Good day pals! Currently I’m studying this database model design called Modified Pre-order Tree Traversal (MPTT). After seeing the drawbacks of using Common Table Expressions (CTE) because of its poor quality performance, I found recommendations of using MPTT. But before I can use the benefit of MPTT, I need to redesign my database table by adding ‘right’ and ‘left’ node values. To do that I need to make a program that will automate and update the values of the each data in the table. My problem is I can’t make a program that will automate the node values. I’m trying to convert a php language to a C# code, but I couldn’t make it. One of my weakness in programming is creating ‘recursive’ methods.

I’m using this link as my reference.
Hierarchical database model

And here’s the code I’m trying to convert to C#

<?php 
function rebuild_tree($parent, $left) {   
    // the right value of this node is the left value + 1   
    $right = $left+1;   

    // get all children of this node   
    $result = mysql_query('SELECT title FROM tree '.   
                           'WHERE parent="'.$parent.'";');  
    while ($row = mysql_fetch_array($result)) {   
        // recursive execution of this function for each   
        // child of this node   
        // $right is the current right value, which is   
        // incremented by the rebuild_tree function   
        $right = rebuild_tree($row['title'], $right);   
    }   

    // we've got the left value, and now that we've processed
    // the children of this node we also know the right value   
    mysql_query('UPDATE tree SET lft='.$left.', rgt='.   
                 $right.' WHERE title="'.$parent.'";');   

    // return the right value of this node + 1   
    return $right+1;   
}   
?>
  • 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-06-05T06:08:02+00:00Added an answer on June 5, 2026 at 6:08 am

    Here’s my own code, please let me know if there is a much better solution

        int right = 0;
    
        public int rebuild_tree(int parent, int left)
        {
            // the right value of this node is the left value + 1
            right = left + 1;
    
            // get all children of this node
            command = conn.CreateCommand();
            command.CommandText = "SELECT cat_id FROM tbl_RefDataCategory_mst WHERE parent_id=@parent";
            command.Parameters.Add("parent", SqlDbType.Int).Value = parent;
    
            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    int temp = Convert.ToInt32(reader["cat_id"]);
                    right = rebuild_tree(temp, right);
                }
    
                command = conn.CreateCommand();
                command.CommandText = "UPDATE tbl_RefDataCategory_mst SET lft=@l, rgt=@r WHERE cat_id=@p";
                command.Parameters.Add("l", SqlDbType.Int).Value = left;
                command.Parameters.Add("r", SqlDbType.Int).Value = right;
                command.Parameters.Add("p", SqlDbType.Int).Value = parent;
    
                command.ExecuteNonQuery();
            }
    
            return right + 1;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day! I've a model that is binded from JSON request, say like this:
Good day. Having read about Model 2 architecture I got confused about some points.
Good day. I'm confused about storing the embedded database in a Java EE application.
Good day everyone, this is one of those areas that is a little over
Good day! I want to show all the pictures in the folder, but this
Good day, I have a hibernate mapping which goes something like this <class name=Person>
Good day. I'd like to ask a question. Why TextBox control Txt in this
Good day! I am a newbie on creating database... I need to create a
Good day! I've found this solution here: VS2010 Web Publish command line version of
Good day, I'm puzzled a bit with this: In[1]:= f[x_]:=With[{xx=x},f[xx_]:=ff[xx]] DownValues[f] f[1] DownValues[f] Out[2]=

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.