IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

July Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...Using DataDelphi Downloads  Print This     
  From the February 2009 Issue of Prestwood eMag
 
Delphi Using Data:
Modified Preorder Tree Traversal Algorithm
 
Posted 12 years ago on 12/12/2008
Take Away: I needed a way to show a hierarchical structure. 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.
 A file from our File Library ► Delphi Downloads

KB101684

Download Link:

 Download File Now

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`)
) 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 send me a private message.

Download Link:

 Download File Now


Comments

1 Comments.
Share a thought or comment...
Comment 3 of 3

Do you constantly run out of time to write an essay or article? I can advise you on a wonderful service that will help you with this.

Posted 106 days ago

Comment 2 of 3

Hi Daniel: I fixed the problem and the attachment is now showing up.

Posted 12 years ago

Comment 1 of 3

I could not upload the associated zip file. I am working with Mike to get the file attached to the posting.

If you need it before then, just respond to this post or email me direclty.

Posted 12 years ago
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P184A1
Enter key:
Article Contributed By Daniel Kram:
Visit Profile

 KB Article #101684 Counter
11882
Since 12/12/2008
Sales Website: www.prestwood.com Or visit our legacy sales site: 
legacy.prestwood.com


©1995-2020 Prestwood IT Solutions.   [Security & Privacy]