Baobab is a library to save tree structured data in a relational database.
Currently there is only a PHP implementation working with MySQL, but it shouldn’t be too difficult to port to other languages or databases (the hard work is done via pure SQL queries and the code is mostly a support to them).
The technique used is  Joe Celko’s  nested set model, modified so that a table can hold more than one tree, to help with cases such as storing threads of a forum (each thread is a tree and they all have an identical structure).
In fact when the library often asks you for a “forest name”, because each table created can hold more than one tree.
We have more than one hundred tests to ensure the library is doing The Right Thing (™), a straightforward thread safe API and a clean documentation.
Each node in the graph has two numbers (left and right) assigned
to it during a  depth-first search of the tree: we assign the left (lft) value
the first time we pass by and the right (rgt) value the following time.
Well, the nested set model is
all about assigning this numbers and maintain them coherent whenever a node is
inserted, moved or deleted.
With this numbers in place we gain various benefits. The tree structure of the data is maintained in a relational database and we’re able to do some really fast searches. Normally slow operations like finding the path between two nodes, knowing all the descendants of a node or discover if a node is ancestor of another are blazing fast. Too, the horizontal order is preserved without the need of others attributes.
Some simple properties of this data structure ...
This is just an introduction, if you want to know more about nested set models I suggest you to read  Trees and hierarchies in SQL for smarties and/or  SQL for smarties, both written by Joe Celko. Online you could read a couple  more resources.
Baobab leverages the works of administering such table. In particular moving or
inserting after a particular node can be pretty complicated, and Baobab does the
hard work for you.
If you feel like so, you can use Baobab for all the tree changing tasks (the most tedious queries) and write your own queries to search what you want in the most optimized way for your schema.
However Baobab provides functions for the laziest programmers.
Here are the functions the Baobab class provide
All of the *_index() functions accept negative numbers too, and all the functions that modify the tree preserve the lft/rgt consistency.
|||Wikipedia on Nested set model|
|||Joe Celko’s Trees and hierarchies in SQL for smarties|
|||Joe Celko’s SQL for smarties: advanced SQL programming|
|||Managing Hierarchical Data in MySQL|