> I have a hierarchical set of data stored in tree format.  The tree may or
> may not be a binary tree ... i.e. each node could have 0 thru N sub-nodes.
> Now, I need to calculate the length of the longest branch, or otherwise, the
> depth of the tree.  My comp. sci. days are a long way behind me, so does
> anybody know how this would be calculated?

Here is an example of how I would do it in Oracle (assuming you have
the tree stored in a table in an Oracle database):

create table t (
   parent varchar2(20)
   ,title varchar2(20)
   ,left number
   ,right number
)
/
insert into t values (
null, 'Food', 1, 18  
);
insert into t values (
'Food', 'Fruit', 2, 11  
);
insert into t values (
'Fruit', 'Red', 3, 6 
);
insert into t values (
'Red', 'Cherry', 4, 5  
);
insert into t values (
'Fruit', 'Yellow', 7, 10  
);
insert into t values (
'Yellow',  'Banana', 8, 9  
);
insert into t values (
'Food',    'Meat', 12, 17  
);
insert into t values (
'Meat' ,   'Beef', 13, 14  
);
insert into t values (
'Meat' ,   'Pork', 15 , 16  
);

select max(depth)
from (
  select level as depth from t
  connect by parent = prior title
  start with parent is null
)

The above query returns 4.
-- 
Eddie Awad.
http://awads.net/

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Logware (www.logware.us): a new and convenient web-based time tracking 
application. Start tracking and documenting hours spent on a project or with a 
client with Logware today. Try it for free with a 15 day trial account.
http://www.houseoffusion.com/banners/view.cfm?bannerid=67

Message: http://www.houseoffusion.com/lists.cfm/link=i:4:211562
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to