# Storing Hierarchical Data in a Database, Part 3

*Automating the Tree Traversal*

Now that youâ€™ve seen some of the handy things you can do with this table, itâ€™s time to learn how we can automate the creation of this table. While itâ€™s a nice exercise the first time and with a small tree, we really need a script that does all this counting and tree walking for us.

Letâ€™s write a script that converts an adjacency list to a modified preorder tree traversal table.

`<?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; Â

} Â

?>

This is a recursive function. You should start it with `rebuild_tree('Food',1);`

The function then retrieves all children of the â€˜Foodâ€™ node.

If there are no children, it sets its left and right values. The left value is given, 1, and the right value is the left value plus one. If there are children, this function is repeated and the last right value is returned. That value is then used as the right value of the â€˜Foodâ€™ node.

The recursion makes this a fairly complex function to understand. However, this function achieves the same result we did by hand at the beginning of this section. It walks around the tree, adding one for each node it sees. After youâ€™ve run this function, youâ€™ll see that the left and right values are still the same (a quick check: the right value of the root node should be twice the number of nodes).

*Adding a Node*

How do we add a node to the tree? There are two approaches: you can keep the parent column in your table and just rerun the `rebuild_tree()`

function â€” a simple but not that elegant function; or you can update the left and right values of all nodes at the right side of the new node.

The first option is simple. You use the adjacency list method for updating, and the modified preorder tree traversal algorithm for retrieval. If you want to add a new node, you just add it to the table and set the parent column. Then, you simply rerun the `rebuild_tree()`

function. This is easy, but not very efficient with large trees.

The second way to add, and delete nodes is to update the left and right values of all nodes to the right of the new node. Letâ€™s have a look at an example. We want to add a new type of fruit, a â€˜Strawberryâ€™, as the last node and a child of â€˜Redâ€™. First, weâ€™ll have to make some space. The right value of â€˜Redâ€™ should be changed from 6 to 8, the 7-10 â€˜Yellowâ€™ node should be changed to 9-12 etc. Updating the â€˜Redâ€™ node means that weâ€™ll have to add 2 to all left and right values greater than 5.

Weâ€™ll use the query:

`UPDATE tree SET rgt=rgt+2 WHERE rgt>5; Â `

UPDATE tree SET lft=lft+2 WHERE lft>5;

Now we can add a new node â€˜Strawberryâ€™ to fill the new space. This node has left 6 and right 7.

`INSERT INTO tree SET lft=6, rgt=7, title='Strawberry';`

If we run our `display_tree()`

function, weâ€™ll see that our new â€˜Strawberryâ€™ node has been successfully inserted into the tree:

`Food Â `

Â Fruit Â

Â Â Red Â

Â Â Â Cherry Â

Â Â Â Strawberry Â

Â Â Yellow Â

Â Â Â Banana Â

Â Meat Â

Â Â Beef Â

Â Â Pork

*Disadvantages*

At first, the modified preorder tree traversal algorithm seems difficult to understand. It certainly is less simple than the adjacency list method. However, once youâ€™re used to the left and right properties, it becomes clear that you can do almost everything with this technique that you could do with the adjacency list method, and that the modified preorder tree traversal algorithm is much faster. Updating the tree takes more queries, which is slower, but retrieving the nodes is achieved with only one query.

## Conclusion

Youâ€™re now familiar with both ways to store trees in a database. While I have a slight preference for the modified preorder tree traversal, in your particular situation the adjacency list method might be better. Iâ€™ll leave that to your own judgement.

One last note: as Iâ€™ve already said I donâ€™t recommend that you use the title of a node to refer to that node. You really should follow the basic rules of database normalization. I didnâ€™t use numerical ids because that would make the examples less readable.

*Further Reading*

More on Trees in SQL by database wizard Joe Celko:

http://searchdatabase.techtarget.com/tip/1,289483,sid13_gci537290,00.html

Two other ways to handle hierarchical data:

http://www.evolt.org/article/Four_ways_to_work_with_hierarchical_data/17/4047/index.html

Xindice, the â€˜native XML databaseâ€™:

http://xml.apache.org/xindice/

An explanation of recursion:

http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_9_5.html

*If you enjoyed reading this post, youâ€™ll love Learnable; the place to learn fresh skills and techniques from the masters. Members get instant access to all of SitePointâ€™s ebooks and interactive online courses, like PHP & MySQL Web Development for Beginners.*