Can be used in Multilevel Marketing with binary tree (can be modified accordingly to be used for n Tree)

Introduction

We are now and then faced with the problem with storing data corresponding to Binary Tree and querying it. We generally rely on Recursion (hence costly) to query tree structure. Here is a simple data structure change in order query tree structure using simple SQL statement - no Recursion required.

Background

There are situations when we want to store Nodes of binary tree in database with reference to its parent together with position (in this case, Left Node or Right Node of Parent). The problem arises when we want to query database to get upline node or downline Node w.r.t. a certain node, or we want to get the count of nodes present at each level in the b-tree.

You can think of an implementation of this code sample in creating data structure to keep members with their hierarchy for Multi-Level Marketing company.

Using the Code

Note: The field naming convention used below is a little awkward but it goes like - first 5 chars in field name correspond to table name, next 2 chars define data type, next 2 chars define Constraint, renaming letters are the name of field.

Could u please help me on this that How to calculate the total number of pair under particular parent node according to pattern 1:2 or 2:1,1:1 ratio day to day maximum up to 5 pair capping daily and daily commission for every pair.

by using below stored procedure I can count the total number of pairs under particular node in 2:1,1:1 ratio means first pair is completed when two node added to the left side of given parent node and one node added right side of given parent node after that all pairs are completed when one node added left side and one node added right side of parent node (1:1 ratio)

example if i execute my stored procedure as follows it would return following.

EXEC count_pairs 'Ramesh123'

3

so there is 3 pairs as shown in my figure.

when we execute my stored procedure for ParentID 'sonu' it would return following.

EXEC count_pairs 'sonu'

2

so there is 2 pairs as shown in my figure.

My problem is to find the query which can return the total number of pair under particular node any given node node. day by day maximum 5 pairs in a day please any one can suggest us

Very good article. I am trying to figure out how to insert a node in the tree. Can you provide me with a query so that either added node is on the right or left side, but the tree should remain balanced.

I have wasted a ton of time in Binary trees and in my view they are not to be used in serious/heavy tasks, so you are welcome to see a skeleton of one heavy-duty approach: hashing as first level and millions of b-trees as second in order to blur-the-objects around.

There B-tree of order 3 is implemented in plain C, without delete operation. Talking of speed performance: it screams.

Dear Anup Kumar Yadav, can we use the same data structure for 3 nodes under one parent? i means L R and C (center node). will it be suitable to use by modifying little things in queries ? can you guide me which queries will need to modify to achieve that target ?

Select * From tbl_BinaryTree_Hierarchy where (Hiera_IN_FK_ParentCode=1 or
(Hiera_IN_FK_ParentCode is Null And Hiera_IN_FK_ChildCode=1))

Or

Select * From tbl_GraphPosition_GrPos
Left outer join tbl_BinaryTree_Hierarchy on GrPos_IN_xx_Position = Hiera_IN_xx_TreePosition
and (Hiera_IN_FK_ParentCode=1 or (Hiera_IN_FK_ParentCode is Null And Hiera_IN_FK_ChildCode=1))

Hello sir, i m totally new in this MLM project.
well i have done the placement of the nodes correctly.

well my requirement is the user can registered as a 1 head, 3 heads, 7 heads and 11 headers

and can place where he wants.
and the payouts like if header 1 or 3 then (total new pairs * 500) and for
7 or 11 ( (total new pairs * 1000) and update their field.
*how i can get the total new pairs also...??

*i have to do daily calculation from the root at 11:55 pm
I would like to know how can i do daily binary calculation ?
or any suggestion that how can i get to know about how many nodes has been added from each

node and how much commission has to be paid them after calculate the pairs.

More is it is good approach to use auto increment column name-> Nodes_IN_PK_Code in

What do you mean by 1/3/7/11 Head ? You need to add a Timestamp or DateTime Column to keep the joining date and query the data base with Date range in Where Clause to get joining for the specified period.