trasversing through binary tree

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
i m making an MLM site in which a member can have two members under
himself,One member on left side and another on right side. Just like
binary tree . Now i want to trasverse through the tree using php and
mysql so that i can show the binary tree created this to user. Pls
suggested me best possible way to do it. Simply using sql queries
without joins and functions is very tedious and time consuming . so
plz suggest me shortest possible script which takes less execution

Re: trasversing through binary tree

sarika wrote:
Quoted text here. Click to load it

First you need to get your database set up correctly.  There are
different ways of doing so; check comp.databases.mysql.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: trasversing through binary tree

Jerry Stuckle wrote:
Quoted text here. Click to load it

To traverse a binary tree in Mysql alone either requires as many joins
as there are branches, or recursive calls to the database.

Or just load the whole thing into PHP and do all the smarts there.

This is an application specific issue, and ultimately its more about
computing algorithms than PHP.

Which way you do this depends on what you want to do. Loading all the
data is ineffective if you just want to display a small subset, but if
you want to display the whole tree, it's as bad/good as any other
solution, for example :-)

The general principle should be to extract only the data you need, using
SQL in its most convoluted ways sometimes, and the use php to sort it out.

That makes for complicated SQL and complicated php: the simpler coding,
is a recursive call to 'walk the tree' with as many calls to the
database as there are branches. Simple to write, time consuming to process.

For small trees, this may not matter much though.

Finally, As Jerry has hinted at, depending on exactly the shape of the
thing, it may be possible to design the database so that any given tree
walk is effectively a march up an index.

For example, if you want to quickly view all the descendants of a given
parent, you could, in principle make up a TABLE called that parent, and
list the IDs off all the descendants in it. You trade insertion speed
for search speed. Insertion speed is very low, as any entry needs to be
added t many of these 'index tables' and they all have to be calculated.

However it is a valid strategy for fast access to 'write once, read
many' data sets.

Re: trasversing through binary tree

Quoted text here. Click to load it

Not strictly true. Although (AFAIK) MySQL does not yet implement START
WITH / CONNECT BY clauses (but its on the wish list), the
functionality could be implemented within PL/SQL so that a single
database call need be made from PHP. Alternatively, this article: /

Shows how to manipulate trees using flat-file indexing.


Re: trasversing through binary tree

Quoted text here. Click to load it

Hi thanks for replying .
what i need is to traverse using the breadth first alogorithm
About my database i have taken only one table which is having
following fields
where memberID is a simple id assigned to members , node meand left or
right,nodeid is under which node id member joined.

plz can u tell me the script for travering breadth wise

Site Timeline