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:

http://groups.google.com/group/borland.public.delphi.objectpascal/browse_thread/thread/8bd20a203e20eaa2
http://www.delphi3000.com/articles/article_2740.asp?SK=
http://www.evolt.org/article/Four_ways_to_work_with_hierarchical_data/17/4047/index.html

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`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=latin1;

-- some working records
/*!40000 ALTER TABLE `tblcategories` DISABLE KEYS */;
INSERT INTO `tblcategories` (`idtblcategories`,`Description`,`lft`,`rgt`) VALUES
(11,'Food',1,18),
(12,'Fruit',2,11),
(13,'Red',3,6),
(14,'Cherry',4,5),
(15,'Yellow',7,10),
(16,'Banana',8,9),
(17,'Meat',12,17),
(18,'Beef',13,14),
(19,'Pork',15,16);
/*!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
Image