If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > Hierarchy design - how to support sub tree searches?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-07-09, 15:17
davoutuk davoutuk is offline
Registered User
 
Join Date: Aug 2008
Posts: 9
Hierarchy design - how to support sub tree searches?

I have a design problem that requires that a hierarchy support sub tree searching.

For example, take the following hierarchy....
Code:
World
   |--- India
   |--- USA
   |--- Europe
             |--- UK
             |      |--- London
             |      |--- Manchester
             |      |--- Edinburgh
             |--- France
             |--- Germany
There is a search requirement where if a parent node is selected then the search results should return all child nodes under that parent node, across all underlying subordinate levels. For example, selecting 'Europe' as the search parameter would return 'UK', 'London',' Manchester', 'Edinburgh', 'France' and 'Germany' as the results.

My table design is...
Code:
CREATE TABLE `geographic_location` (
  `GeoLocationIntID` int(11) NOT NULL auto_increment,
  `GeoLocationTitle` varchar(50) NOT NULL,
  `GeoLocationAbbrev` varchar(20) NOT NULL,
  `GeoLocationParentIntID` int(11) default '-1',
  `GeoLocationDisplayOrder` int(11) default '0',
  PRIMARY KEY  (`GeoLocationIntID`),
  UNIQUE KEY `GeoLocationTitle` (`GeoLocationTitle`)
)
I'm wondering how best to cater for this sub tree search requirement. One idea involves adding two new fields...
  • 'alias' - a unique short alpha code for a location
  • 'aliasPath' - a concatentation of all the alias values for an entry and all its parents
For instance, with...
Code:
World (alias: 'w')
   |--- Europe (alias: 'e')
             |--- UK (alias: 'uk')
             |      |--- London (alias: 'ldn')
.. then the aliasPath for London would 'w/e/uk/ldn'

Using this aliasPath would allow me to use LIKE style searches to find all subordinate nodes .

Thoughts?
Reply With Quote
  #2 (permalink)  
Old 07-07-09, 15:53
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,246
Categories and Subcategories may have what you want
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 07-08-09, 13:22
davoutuk davoutuk is offline
Registered User
 
Join Date: Aug 2008
Posts: 9
Thanks...

I like the 'Modified Preorder Tree Traversal' idea - I'm going to use that.
Reply With Quote
  #4 (permalink)  
Old 07-08-09, 13:48
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
That method works will with large, fairly static datasets. Otherwise, use the adjacency model.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #5 (permalink)  
Old 07-09-09, 03:41
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
OP: If you want more information and thoughts about your idea, it is known as Materialised Paths.
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On