Vinmark Posted February 21, 2010 Share Posted February 21, 2010 uhm i am currently working on a Multilevel Marketing System. i'm using php. it is a genealogy that is 2x2 matrix. every member can have 2 downlines. my problem is how can i count all his total downlines per side. example chart. SIDE A SIDE B member1 member2 member3 member4 member5 member6 member7 member8 NULL NULL NULL NULL NULL NULL NULL total Downline at Side A : 4 total Downline at Side B : 3 this is my table memberid | parentid | child_a | child_b sorry for my bad english. can you give me any idea on how to this? and code this then post it here for help again? pls? Quote Link to comment Share on other sites More sharing options...
gamblor01 Posted February 21, 2010 Share Posted February 21, 2010 I am not 100% positive what you are asking for, but it appears that your data is effectively a binary tree. You said that each member can have two downlines which is incredibly similar to a statement such as "each node can have a maximum of two children." If that is the case then the code to do this is really quite simple -- in fact, any search through a binary tree should work. You might want to look up an in-order search (or pre-order or post-order if you prefer). I think you simply need to get the root of the tree and then initialize 2 counters: - one counter for the number of non-null nodes starting with root.left (aka root->left) - a second counter for the number of non-null nodes starting with root.right (aka root->right) I haven't done much data-structure/OOP coding in PHP but in Java, a simple recursive algorithm that does a return like: if (this != null) { return 1 + findMembers(this.left) + findMembers(this.right); } else { return 0; } should do the trick. That's about all I can offer you. Quote Link to comment Share on other sites More sharing options...
Vinmark Posted March 5, 2010 Author Share Posted March 5, 2010 how can i count those nodes until it become null? i'm wondering that i can loop and get all the nodes and pass it to an array until it gets null but how. T_T Quote Link to comment Share on other sites More sharing options...
greatstar00 Posted March 5, 2010 Share Posted March 5, 2010 passing them to array, you will waste memory imagine if there are like 180000 members (which is market america got currently after 18 years) and at least like 90k members so, u will have 90k array? each array hold a member's name, or id, the bottom is less memory but the top few, they got a huge array wat a waste if you jsut use 2 array with 90k members per array you will need to find out which member belongs which side (any member beyong 2nd level) this is more complicated i think this is the system looks like market america (they already got the system working very well) what i can come up is have a class, and some recursive function this is like of like a tree let's assume this in c++, cause i know more in c++ you have this struct struct node{ int total; node *previous; //points his upline node *left; //points to his left downline node *right; //points to his right downline } void counting(node n){ while(n->left=NULL){ n->previous->total++; //give the upline 1 downline counting countTotal(n); counting(n->left); } while(n->right !=NULL){ n->previous->total++; //give the upline another downline counting countTotal(n); counting(n->right); } } void countTotal(n){ while(n->previous!=NULL){ n->previous->previous->total=n->previous->previous->total+n->previous->total; //sum up the upline's upline's total countTotal(n->previous); } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.