N level tree traversing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpeid
Forum Newbie
Posts: 1
Joined: Sat Feb 12, 2011 3:00 am

N level tree traversing

Post by phpeid »

Hello everybody,

i am working on n level category tree structure, for your information the category tree structure image is attached with this post.

Let's consider, each of the category have hundreds( n level ) of category in each, i want to traverse each category without using recursive functions, reason being recursive functions are very slow, is there any way to do this??

Thanks,
phpeid
Attachments
tree_structure.png
tree_structure.png (4.89 KiB) Viewed 505 times
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: N level tree traversing

Post by Weirdan »

Couple of notes:
  1. Recursive functions are not slow, what could be slow is how you using them. For example, running queries like 'select * from nodes where parent_id=$current_id' is certainly not a good idea in a recursive function.
  2. Your 'tree' is not a tree, since tree is an acyclic graph, while you have cycles in your structure.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: N level tree traversing

Post by Darhazer »

Post Reply