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;
}
?>
Here's my own code, please let me know if there is a much better solution