Using PostgreSQL, you can store the hierarchical path for each node in an array of smallints:
create table treedata (
id integer not null primary key,
node smallint not null,
name text not null
);
insert into treedata values (1, ‘{0}’, ‘Food’);
insert into treedata values (2, ‘{0,0}’, ‘Fruit’);
insert into treedata values (3, ‘{0,0,0}’, ‘Red’);
insert into treedata values (4, ‘{0,0,0,0}’, ‘Cherry’);
insert into treedata values (5, ‘{0,0,0,1}’, ‘Strawberry’);
insert into treedata values (6, ‘{0,0,1}’, ‘Yellow’);
insert into treedata values (7, ‘{0,0,1,0}’, ‘Banana’);
insert into treedata values (8, ‘{0,1}’, ‘Meat’);
insert into treedata values (9, ‘{0,1,0}’, ‘Beef’);
insert into treedata values (10, ‘{0,1,1}’, ‘Pork’);
To retrieve, just SELECT FROM treedata ORDER BY node ASC;
To retrieve a branch: SELECT FROM treedata WHERE node[1:3] = ‘{0,0,0}’;
This retrieves ‘Red’ and its children.
Updating is simply a matter of saying: UPDATE datatree set node[3] = 2 WHERE node[1:3] = ‘{0,0,0}’;
This puts ‘Red’ after ‘Yellow’.
Hope this helps.