Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 2 of 2 total
Thread Modified Preorder Tree Traversal Algorithm
Fri, Dec 12 2008 3:05 PMPermanent Link

Daniel Kram
I needed a way to show a hierarchical structure and found a few informative sites:


And what most were saying, was the, Modified Preorder Tree Traversal Algorithm, was a preferred way of doing hierarchical sets.

I thought I would share the code with you.

You will need to modify for the DB connectivity of course. Mine DB connectivity starts at application startup and passes the DB object around, but,
you may benefit from the logic.

The frmCategories.pas/dfm is where the actual tree resides and the calls to the DB class occur.
The CategoriesDBUnit.pas is the unit where all the work is performed.
This is the Create for the tables (I am using both MySQL version 5 and Elevate DB version 2)

-- Definition of table `tblcategories`

DROP TABLE IF EXISTS `tblcategories`;
CREATE TABLE `tblcategories` (
 `idtblcategories` int(10) unsigned NOT NULL auto_increment,
 `Description` varchar(45) NOT NULL,
 `lft` int(10) unsigned NOT NULL,
 `rgt` int(10) unsigned NOT NULL,
 PRIMARY KEY  USING BTREE (`idtblcategories`),
 UNIQUE KEY `ndxDescription` (`Description`)

-- some working records
/*!40000 ALTER TABLE `tblcategories` DISABLE KEYS */;
INSERT INTO `tblcategories` (`idtblcategories`,`Description`,`lft`,`rgt`) VALUES
/*!40000 ALTER TABLE `tblcategories` ENABLE KEYS */;

You can add and remove from the tree, however, I only allow one "root" or starting node.

If you have any questions, feel free to email.
Fri, Dec 12 2008 3:07 PMPermanent Link

Daniel Kram
Not sure where the zip file is from the last post, let me try again...

Attachments: TreeTraversalAlgorithm.zip