I will try to make myself as clear as possible. Based from Adjacency List Model: http://articles.sitepoint.com/article/hierarchical-data-database
I need a way to balance this tree
0
/ \
1 2
/ / \
3 4 5
\ \
6 7
to something like:
0
/ \
1 2
/ \ / \
3 4 5 6
/
7
Based from the sample Code:
<?php
function display_children($parent, $level) {
$result = mysql_query('SELECT title FROM tree '.
'WHERE parent="'.$parent.'";');
while ($row = mysql_fetch_array($result)) {
echo str_repeat(' ',$level).$row['title']."\n";
display_children($row['title'], $level+1);
}
}
?>
I modified the code so it can output a flat html table like this:
$super_parent = '0000' left Node entries into flat list:
____________________________________________________
| No. | Date of Entry | Account ID | Placement|
------------------------------------------------------
| 1 | 2010-08-24 11:19:19 | 1111a | a |
| 2 | 2010-08-24 11:19:19 | 22221a_a | a |
| 3 | 2010-08-24 11:19:19 | 33_2aa | b |
| 4 | 2010-08-24 11:19:19 | 33_2Ra | a |
| 5 | 2010-08-24 11:19:19 | 22221a_b | b |
| 6 | 2010-08-24 11:19:19 | 33_2ba | a |
| 7 | 2010-08-24 11:19:19 | 33_2bb | b |
------------------------------------------------------
But I need a way to reorganize all this into a balanced tree without moving or rotating the parent. Although I can think of creating a duplicate table in the db and do a second query to display or create another Binaray tree, I thought it may be possible to reorganize a flat tree like this into:
0
/ \
1 2
/ \ / \
3 4 5 6
/
7
From left to right. 0 represents the parent or super_parent 0000.
The reason I would like to do this is so I can create a virtual tree from the original tree that will be the basis of another algorithm in my project.
Thanks in advance.
Bob