I have a set of objects in a hierarchy. There is the upper “root” node and having child nodes, which, in turn, have child nodes, etc. I am trying to save this structure to the database using a nested collection model, where each "side" of each node is numbered to define a hierarchy, as in Managing hierarchical data in MySQL :
alt text http://dev.mysql.com/tech-resources/articles/hierarchical-data-4.png
My problem is calculating left and right values. I usually use RecursiveIteratorIterator to iterate through the hierarchy, but I cannot decide how to calculate the numbers without resorting to a recursive function that parses the index variable by reference.
Any ideas?
This is probably useless, but this is the (incorrect) code that I currently have:
$iterator = new RecursiveIteratorIterator(
new Node_List(array($root)),
RecursiveIteratorIterator::SELF_FIRST);
$i = 0;
foreach ($iterator as $node) {
$node->left = ++$i;
$node->right = ++$i;
}
As you can see, this will give something like this:
Node
Node
Node
Left and right meanings:
Node (1, 2)
Node (3, 4)
Node (5, 6)
When should they be:
Node (1, 6)
Node (2, 3)
Node (4, 5)
source
share