Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 6th, 2003, 08:49 AM
torkil torkil is offline
Another damn newb...
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Location: Bodø, Norway
Posts: 94 torkil User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
Traversing a non-binary tree i php

I'm having difficulties with the recursive calls.

I have this database with a tree structure that stores "id", "parent_id" and "name" for each element. The elements with parent_id=0 is at the root level.

Anyone able to help me traverse my tree and maybe also print it to screen?

I also wanted to do this in an effective way with minimum number of queries to mysql. Could it be dnone without recursion as well? Perhaps just select all rows from the table and then iterate through them one by one, placing each record inside an array that simulates the tree...

Well then there is the problem with traversing this array... It will need recursive calls too....... Argh.

Whats the more efficient way?
__________________
Torkil Johnsen

Never underestimate the power of stupid people in large groups...
---------------------------(òÓ,)----

Reply With Quote
  #2  
Old June 7th, 2003, 01:57 PM
csaba csaba is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: NYC
Posts: 79 csaba User rank is Private First Class (20 - 50 Reputation Level)csaba User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
These are going to be fairly simple observations, but...

First off, as a general thing, relational databases do not behave well as graphs, which is what a tree is, because and "relationship" only gets you one level.

Secondly, since you want to traverse the entire tree, it seems, that means going through your entire data structure (several times if it's in a database).

Third you can iterate just fine (doesn't need to be recursive). We start off finding the leaf nodes (has to be the leaf nodes because you don't have a facility for children).

Starting off. Find the leaf nodes. These are the ones such that no parent_id of any other row is equal to their id. There is a writeup of how to do this kind of query in the docs of the mysql web site at http://www.mysql.com/doc/en/JOIN.html
You'll have to adapt it for your own use.

After this, just examine each element, in turn, to get its parent. If you are hitting the database, it will be a lot of repeated searches. So, depending on size, you could build your own representation within PHP.

What we need is a graphical database.

Csaba Gabor

Reply With Quote
  #3  
Old June 18th, 2003, 03:36 PM
clam61 clam61 is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 151 clam61 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 36 sec
Reputation Power: 0
to traverse the tree you could use depth first search or breadth first search depending on how you want it displayed. depth first uses a stack while breadth first uses a queue. im not sure if php has these structures builtin, but you could create one using an array.

if you dont know what the hell depth first search or breadth first search is, then this is probably too much trouble to learn the algorithm and create the appropriate data structures.

a better way is to use celko nested sets. it is better in terms of optimality when displaying trees, although a bit more complicated when inserting and updating. check it out

http://www.sitepoint.com/print/1105

Reply With Quote
  #4  
Old June 18th, 2003, 05:15 PM
csaba csaba is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: NYC
Posts: 79 csaba User rank is Private First Class (20 - 50 Reputation Level)csaba User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
An array IS a queue right out of the box in PHP. That is to say, if you shove elements in:
$foo = array();
$foo[6] = "Hi";
$foo[3] = "Mom";
$foo[2] = "and";
$foo[1] = "Dad";

and now iterate through the array using a foreach, you get the elements out in the order they went in. Furthermore, if you delete an element by unset (say unset($foo[3]) the remaining elements stay in their originally entered order. So this is a superset of a queue's functionality.

PHP is also thoughtful enough to provide a method for getting to the last element of a hash table (which is what an array is in PHP) by means of array_splice(...) However, this reindexes the array, which tends to suggest that one cannot claim an out of the box stack by this method (I.e. can't trivially claim O(1) speed). However, if one assumes incremental numeric indeces, then knowledge of the size of the array is sufficient for a trivial implementation.

Csaba

Reply With Quote
  #5  
Old June 18th, 2003, 05:40 PM
clam61 clam61 is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 151 clam61 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 36 sec
Reputation Power: 0
that's great. so now you can write your recursive function.

Reply With Quote
  #6  
Old June 19th, 2003, 02:25 PM
clam61 clam61 is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 151 clam61 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 36 sec
Reputation Power: 0
ok, i wasnt sure how much you knew about algorithms and data structures. this is a simple depth first function, it could be made cleaner by adding some if statements in there so that declaring a stack and pushing the root would be apart of the function, but i hope this illustrates the idea. if you want breadth first then use a queue instead of a stack
Code:
begin function printTree
  while stack not empty
    temp node = pop stack;
    print temp node;
    push temp node's children on stack (if any);
    call printTree;
end printTree

declare an empty global stack object;
push the root on the stack;
call printTree;

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > Traversing a non-binary tree i php

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap