|
|
Comments and Discussions
|
|
 |
|

|
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 ?
|
|
|
|

|
Yes imran we can.
AnupKumarYadav
Delhi,India
|
|
|
|

|
Hello sir, I need help that how could we get the 4 levels of nodes
from a node, like Under Node 3 has many levels but i need only
4 levels nodes only.
hope u can understand what i need.
With regards
vik
|
|
|
|

|
Select B.Nodes_VC_xx_NodeData, Hiera_IN_xx_NodeLevel From tbl_BinaryTree_Hierarchy H
Left Outer Join tbl_BinaryTree_Nodes B on H.Hiera_IN_FK_ChildCode = B.Nodes_IN_PK_Code
Where H.Hiera_IN_FK_ParentCode=1
and Hiera_IN_xx_NodeLevel<=5
change Hiera_IN_xx_NodeLevel<=5 , value 5 to what ever according to your need
AnupKumarYadav
Delhi,India
|
|
|
|

|
Hello Mr.Anup Kumar,your article helped me a lot to proceed in my project from the very first day.
I here by thank you for everything.
My client requirements are however with some differences so,i request you to help me out there so that i can conclude with my work efficiently.
|
|
|
|

|
Thanks for appreciation. you can get in touch with me on anupkumaryadav@yahoo.com
AnupKumarYadav
Delhi,India
|
|
|
|

|
If We can get data is 1-Dimensional array, then we can easily show it in Graph.
----------------------------------------------------------------------------------
| Root | AL | AR | ZL | ZR | BL | BR | ? | ? | ? | ? | CL | CR | DL | DR |.....
----------------------------------------------------------------------------------
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |......
----------------------------------------------------------------------------------
This can be acheived using DataTable or Recordset or Cursor, in this manner
Position | Node Data | other fields ...
1 | Root node |
2 | AL |
3 | AR |
4 | ZL |
5 | ZR |
6 | BL |
7 | BR |
8 | ? |
9 | ? |
10 | ? |
11 | ? |
12 | CL |
13 | CR |
14 | DL |
15 | DR |
------------------------------------- Changes Needed --------------------------------------------------
Add Column Hiera_IN_xx_TreePosition -- Node Location in 2-Dimensional Array w.r.t parent
In the Table tbl_BinaryTree_Hierarchy, secondary table to keep Node hierarchy
And Modify Trigger to update the newly column,
Create Trigger [dbo].[trg_BinaryTree_Nodes_ADD]
On [dbo].[tbl_BinaryTree_Nodes]
For Insert
AS
Begin
IF (@@RowCount=1)
Begin
Insert Into tbl_BinaryTree_Hierarchy
(Hiera_IN_FK_ParentCode, Hiera_IN_FK_ChildCode, Hiera_CH_xx_Position, Hiera_IN_xx_NodeLevel, Hiera_IN_xx_TreePosition)
Select (Case When Hiera_IN_FK_ParentCode Is Null
Then Hiera_IN_FK_ChildCode
Else Hiera_IN_FK_ParentCode End),
(Select Nodes_IN_PK_Code From Inserted),
(Case When Hiera_IN_FK_ParentCode Is Null
Then (Select Nodes_CH_xx_Position From Inserted)
Else Hiera_CH_xx_Position End) ,
(Hiera_IN_xx_NodeLevel + 1 ),
--New Change To incorporate Tree Placement w.r.t Parent
(Case When (Select Nodes_CH_xx_Position From Inserted)='L'
Then (Hiera_IN_xx_TreePosition*2)
Else ((Hiera_IN_xx_TreePosition*2)+1) End)
From tbl_BinaryTree_Hierarchy Where Hiera_IN_FK_ChildCode = (Select Nodes_IN_FK_ParentCode From Inserted)
Union ALL
Select NULL , Nodes_IN_PK_Code , Nodes_CH_xx_Position , 1, 1 From Inserted
End
Else
Begin
RaisError ('Multiple Insertion Not Handled in Trigger to update tbl_BinaryTree_Hierarchy',1,1)
-- Code Using Cursor for Multiple insert
--...
--...
--...
--
End
End
-- Now you can use simple query to get 1-dimensional representation to easily show in graphical format
----------------------------------------------------------------------------------
| Root | AL | AR | ZL | ZR | BL | BR | ? | ? | ? | ? | CL | CR | DL | DR |.....
----------------------------------------------------------------------------------
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |......
----------------------------------------------------------------------------------
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))
--------------
Data Table thus returned can be easily shown in Labels
Label1.Text = DataTable.Rows(0).item("FieldToDisplay") -- first node (root Node)
Label2.Text = DataTable.Rows(1).item("FieldToDisplay") -- 2nd node (AL)
:
:
LabelN.Text = DataTable.Rows(N-1).item("FieldToDisplay")
I regret because the codes are scattered every where in this article , I will update the entire article when I need enough time & I feel to do so.
I Hope This will help
AnupKumarYadav
Delhi,India
|
|
|
|

|
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
tbl_BinaryTree_Nodes ?
hope you can understand my requirement.
with regards
vikram
|
|
|
|

|
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.
AnupKumarYadav
Delhi,India
|
|
|
|

|
Thanks for reply.
My apologies if i unable to clear my requirement.
Well i read your following *Query which give me left count and righ count of the node and
message to PAY... i would like to know how can i update in my table tbl_BinaryTree_Nodes
column name Left_count and Right_count using below *Query and how i get to know that how
much i have to pay ? e.g. according to node no.3 he added on his first day 3 and 5 and he
got according to pairs 3 and (3*500)=1500, now on his 2nd day he joined 5 and 8 and it will
be 2 and (2*500)=100...
*farmula
(Prev_left,Prev_right) and (Newleft,NewRight)
find min. from(Prev_left,Prev_right) and from (Newleft,NewRight)
acc. to below values.(initial values(0,0))
1st day (0,0) (3,5) = (3-0) = 3
2nd day (3,5) (5,8) = (5-3) = 2
Date left_count right_Count
12/12/2010 3 5
13/12/2010 5 8
----------------------------------------------------------------------------------------------
*Query
With CTE as (
Select B.Nodes_IN_PK_Code as PK, B.Nodes_VC_xx_NodeData as NodeData,
Hiera_IN_xx_NodeLevel as NodeLevel, Hiera_CH_xx_Position as Position, '' as IsBalanced,
Power(2,Hiera_IN_xx_NodeLevel)-2 as TotalNodeForBalanceTree,
(Select Count(Hiera_IN_PK_Code) From tbl_BinaryTree_Hierarchy Where
Hiera_CH_xx_Position = 'L' and Hiera_IN_FK_ParentCode=B.Nodes_IN_PK_Code) as
TotalLeftChild,
(Select Count(Hiera_IN_PK_Code) From tbl_BinaryTree_Hierarchy Where
Hiera_CH_xx_Position = 'R' and Hiera_IN_FK_ParentCode=B.Nodes_IN_PK_Code) as
TotalRightChild
From tbl_BinaryTree_Hierarchy H
Left Outer Join tbl_BinaryTree_Nodes B on H.Hiera_IN_FK_ParentCode = B.Nodes_IN_PK_Code
Where H.Hiera_IN_FK_ParentCode is NOT NULL and H.Hiera_IN_FK_ChildCode = ?PKofNewnode?
-- (Primary key of Newly Added Node)
)
Select CTE.PK, CTE.NodeData, CTE.NodeLevel, CTE.Position,
Case When ((CTE.TotalNodeForBalanceTree / 2 = TotalLeftChild) and
(CTE.TotalNodeForBalanceTree / 2 = TotalRightChild)) then 'Yes' Else' No' End As
IsBalanced,
CTE.TotalNodeForBalanceTree , CTE.TotalLeftChild , CTE.TotalRightChild,
Case When Position ='L' then (Case When TotalLeftChild<=TotalRightChild Then 'PAY' Else
'Do Not Pay' End )
When Position ='R' then (Case When TotalLeftChild>=TotalRightChild Then 'PAY' Else
'Do Not Pay' End )
End As ShouldIPay
From CTE
----------------------------------------------------------------------------------------------
and about 1, 3, 7, 11 its a node type e.g if i joined 3 then i can get three node e.g
1(R) (L)2 (R))3, if i joined 7 then 1(R) (L)2 (R)3 (L)4(under 2) (R)5(under 3) and so on...
i have to calculate it binary calculation according to type
like if some joined 1 or 3 type then he will get the money per node is 500 and if 7 or 11 then 1000..hope you understand what i want to achieve and let me know ur suggestion too.
with regards
vik
|
|
|
|

|
The payment problem can be solved in many ways
Way 1 - by adding 4 column in tbl_BinaryTree_Nodes
Fields - PaidLeft , PaidRight, UnPaidLeft, UnpaidRight
Step A : Update Column UnPaidLeft & UnpaidRight -- Use trigger to increment the values when any new entry is made under this node upto level which is payable (if unlimited depth then always)
Step B : Now when you calculate payout then update column PaidLeft , PaidRight by decrementing Column UnPaidLeft & UnpaidRight by the numbers you are going to pay.
Way 2 - By adding Paid Column in tbl_BinaryTree_Hierarchy indicating that payout against this node has been paid or not.
Way 3 - Use data from the payout table to calculate & distribute payout.
In both way you need to add DateTime Column either in tbl_BinaryTree_Nodes or in tbl_BinaryTree_Hierarchy to query acc. to joining date.
AnupKumarYadav
Delhi,India
|
|
|
|

|
how can i update the left and right column of the table using below CTE Query ..?
------------------------------------------------------------------------------------------
With CTE as (
Select B.Nodes_IN_PK_Code as PK, B.Nodes_VC_xx_NodeData as NodeData, Hiera_IN_xx_NodeLevel as NodeLevel, Hiera_CH_xx_Position as Position, '' as IsBalanced,
Power(2,Hiera_IN_xx_NodeLevel)-2 as TotalNodeForBalanceTree,
(Select Count(Hiera_IN_PK_Code) From tbl_BinaryTree_Hierarchy Where Hiera_CH_xx_Position = 'L' and Hiera_IN_FK_ParentCode=B.Nodes_IN_PK_Code) as TotalLeftChild,
(Select Count(Hiera_IN_PK_Code) From tbl_BinaryTree_Hierarchy Where Hiera_CH_xx_Position = 'R' and Hiera_IN_FK_ParentCode=B.Nodes_IN_PK_Code) as TotalRightChild
From tbl_BinaryTree_Hierarchy H
Left Outer Join tbl_BinaryTree_Nodes B on H.Hiera_IN_FK_ParentCode = B.Nodes_IN_PK_Code
Where H.Hiera_IN_FK_ParentCode is NOT NULL and H.Hiera_IN_FK_ChildCode = ?PKofNewnode? -- (Primary key of Newly Added Node)
)
Select CTE.PK, CTE.NodeData, CTE.NodeLevel, CTE.Position,
Case When ((CTE.TotalNodeForBalanceTree / 2 = TotalLeftChild) and (CTE.TotalNodeForBalanceTree / 2 = TotalRightChild)) then 'Yes' Else' No' End As IsBalanced,
CTE.TotalNodeForBalanceTree , CTE.TotalLeftChild , CTE.TotalRightChild,
Case When Position ='L' then (Case When TotalLeftChild<=TotalRightChild Then 'PAY' Else 'Do Not Pay' End )
When Position ='R' then (Case When TotalLeftChild>=TotalRightChild Then 'PAY' Else 'Do Not Pay' End )
End As ShouldIPay
From CTE
|
|
|
|

|
You don't need this Query, Just Modify the Triggers "trg_BinaryTree_Nodes_ADD", so that It update all the parent node on entry of any node.
AnupKumarYadav
Delhi,India
|
|
|
|

|
You don't need this Query, Just Modify the Triggers "trg_BinaryTree_Nodes_ADD", so that It update all the parent node on entry of any child node.
AnupKumarYadav
Delhi,India
|
|
|
|

|
Could you please help on this question I would be highly thankful to you.
|
|
|
|

|
Please elaborate your question w.r.t technology & envorinment.
AnupKumarYadav
Delhi,India
|
|
|
|

|
Actually I am developing an MLM website in Asp.net(3.5) and C#. I have used your data structure for binary tree, I am able to show the hierarchy in Grid View using your querry but my client want that I should show it in Graphical repersentation form.
Could u please help me on this that how could I perform this, I am unable to do it on my own. Any help would highly appreciated.
regards
Abhishek
|
|
|
|

|
If You are using Asp.net then first decide the level of genealogy that you want to show, you can use Label/Link Label control to display names, Image to display image of member either depending on package , For this what you have to do is write a query to get the immediate child in left/right side & display it in label.
AnupKumarYadav
Delhi,India
|
|
|
|

|
Thanks for your reply I have adapted two methods to show the genealogy in one I have used built in tree control in asp.net and the second was the method you have already suggested.
Honestly speaking your data structure help me a lot. Keep writing such article and if I wud hav any issue again please reply on this forum.
Thanks once again and I must say I have searched all the big and small places on internet to find such data structure but only you have written. Great dude!!!
Regards
Abhishek
|
|
|
|

|
See, Displaying the tree need some SQL query & Some sort of Code in Webpage And its up to you how yo do it. For Your Reference you can check this image at http://www.anupkumaryadav.com/tree.gif[^] to get a flavor of the tree.
AnupKumarYadav
Delhi,India
|
|
|
|

|
Thanks for your quick reply. I would be higly thankful to you if you share code for the same to generate a tree as you have shared in link.
|
|
|
|

|
Well could you let me know how can i achieve to display genealogy ...?
any example or code...?
|
|
|
|

|
Representing Tree In Graphical format
Starting with Universal Rule "There are many ways to do a certain thing". Here is one way to represent Binary tree in graphical format.
With reference to binary tree representation above (Tree 2)
----------------------------------------------------------------------------------
| Root | AL | AR | ZL | ZR | BL | BR | ? | ? | ? | ? | CL | CR | DL | DR |.....
----------------------------------------------------------------------------------
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |......
----------------------------------------------------------------------------------
We Just need a query which can arrange the data in an array in this case DataTable with multiple row.
data can be retrived in 2 format,
1 - without row for missing node eg.
Position | Node Data | other fields ...
1 | Root node |
2 | AL |
3 | AR |
4 | ZL |
5 | ZR |
6 | BL |
7 | BR |
12 | CL |
13 | CR |
14 | DL |
15 | DR |
For the above rresult , we can use the code below
;With CTE ( Code, NodeData, Position, LeftRight) AS
(
Select Nodes_IN_PK_Code, Nodes_VC_xx_NodeData, 1 , Nodes_CH_xx_Position From tbl_BinaryTree_Nodes
Where Nodes_IN_PK_Code=1
Union ALL
(
Select Nodes_IN_PK_Code, Nodes_VC_xx_NodeData, (Position * 2) , Nodes_CH_xx_Position From tbl_BinaryTree_Nodes
join CTE on tbl_BinaryTree_Nodes.Nodes_IN_FK_ParentCode = CTE.Code And Nodes_CH_xx_Position ='L'
Union All
Select Nodes_IN_PK_Code, Nodes_VC_xx_NodeData, ((Position * 2) + 1 ) , Nodes_CH_xx_Position From tbl_BinaryTree_Nodes
join CTE on tbl_BinaryTree_Nodes.Nodes_IN_FK_ParentCode = CTE.Code And Nodes_CH_xx_Position ='R'
)
)
Select * From CTE Order by Position
2 - With rows for missing Nodes eg.
Position | Node Data | other fields ...
1 | Root node |
2 | AL |
3 | AR |
4 | ZL |
5 | ZR |
6 | BL |
7 | BR |
8 | ? |
9 | ? |
10 | ? |
11 | ? |
12 | CL |
13 | CR |
14 | DL |
15 | DR |
For the above result set we can create a table for example
Create Table tbl_GraphPosition_GrPos
(
GrPos_IN_PK_Code int Identity(1,1),
GrPos_IN_xx_Position Int
)
Insert Rows with values Natural Numbers from 1 to n , where n is the nth node you want to display in tree. For example to show tree like below we will insert 15 rows.
--'------------------------------------------
--' X
--' --------|-------
--' | |
--' X X
--' -----|----- -----|-----
--' | | | |
--' X X X X
--' ---|--- ---|--- ---|--- ---|---
--' | | | | | | | |
--' X X X X X X X X
--'------------------------------------------
Now the qwuery below will give use required DataTable
;With CTE ( Code, NodeData, Position, LeftRight) AS
(
Select Nodes_IN_PK_Code, Nodes_VC_xx_NodeData, 1 , Nodes_CH_xx_Position From tbl_BinaryTree_Nodes
Where Nodes_IN_PK_Code=1
Union ALL
(
Select Nodes_IN_PK_Code, Nodes_VC_xx_NodeData, (Position * 2) , Nodes_CH_xx_Position From tbl_BinaryTree_Nodes
join CTE on tbl_BinaryTree_Nodes.Nodes_IN_FK_ParentCode = CTE.Code And Nodes_CH_xx_Position ='L'
Union All
Select Nodes_IN_PK_Code, Nodes_VC_xx_NodeData, ((Position * 2) + 1 ) , Nodes_CH_xx_Position From tbl_BinaryTree_Nodes
join CTE on tbl_BinaryTree_Nodes.Nodes_IN_FK_ParentCode = CTE.Code And Nodes_CH_xx_Position ='R'
)
)
Select CTE.* From CTE
Right outer join tbl_GraphPosition_GrPos on CTE.Position = tbl_GraphPosition_GrPos.GrPos_IN_xx_Position
Order By tbl_GraphPosition_GrPos.GrPos_IN_xx_Position
Now we can easily display the data in labels ,
For example using in ASP.Net
Label1.Text = DataTable.Rows(0).item("FieldToDisplay")
Label2.Text = DataTable.Rows(1).item("FieldToDisplay")
:
:
LabelN.Text = DataTable.Rows(N-1).item("FieldToDisplay")
I Hope This will help
Alternatively , We can Add One More column in tbl_BinaryTree_Hierarchy , to keep Node placement in 1-Dimensional Array of the Newly Inserted nodde w.r.t Parent Node. Which can be updated using the same trigger, at the time of node insertion thus saving us from using Computed Table to calcualte location during runtime thus resulting in lighter overhead to Server in the Query.
AnupKumarYadav
Delhi,India
modified on Friday, December 24, 2010 10:59 AM
|
|
|
|

|
While I am running this query with Order by Clause Select * From CTE Order by Position
It shows error Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type int.
and when I run without Order By Clause It shows all the rows not limiting to 15 rows. Kindly resolve this issue I would be higly thankful to you.
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
|
Can be used in Multilevel Marketing with binary tree (can be modified accordingly to be used for n Tree)
| Type | Article |
| Licence | CPOL |
| First Posted | 3 Nov 2010 |
| Views | 37,349 |
| Downloads | 0 |
| Bookmarked | 18 times |
|
|