|
|
Comments and Discussions
|
|
 |

|
Hi,
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.
Thanks a lot again.
|
|
|
|

|
Hi sir, I'm into developing my Multi-Level Marketing when someone says that my database structure is now properly analyze. Can you lend me your help on how to correct my database structure?
|
|
|
|

|
Hi sir, I'm into developing my Multi-Level Marketing when someone says that my database structure is now properly analyze. Can you lend me your help on how to correct my database structure?
|
|
|
|

|
Thanks every one for the appreciation. I am quite busy these days, however I'll respond to all of your queries very soon.
thanks all
AnupKumarYadav
Delhi,India
|
|
|
|

|
How can i use Trigger in Visual Studio...
i am a new for .net work please help
|
|
|
|

|
The article above is SQL implementation of the solution. No .Net involved in here.
thanks
AnupKumarYadav
Delhi,India
|
|
|
|

|
how to create binary tree in asp.net c#
please give me graphical representation
|
|
|
|

|
There are more than one way to graphically represent the tree.
Please wait a while. I'll upload the solution ASAP.
Thanks
AnupKumarYadav
Delhi,India
|
|
|
|

|
Please help to generate the weekly payout i had followed ur data structure which is given this article
Please help i will be highly appreciable,
thanks
|
|
|
|

|
dear sir plz help me to draw binary tree . asp.net and c#.from ur database what u gave already in article
|
|
|
|
|

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

|
It's great to read and now I am ready to implement this in MY MLM project but one more help needed how we can show this in a graphical representation. Thanks in Advance
|
|
|
|

|
I think this approach is needlessly complex and hard to use. Triggers and recursive CTEs are procedural code.
Look up a binary heap in an book on data structure. The idea is that if a parent node has an array position of (n), the the left and right children are at (2n) and (2n+1). I have a short discussion, with artwork at
http://www.simple-talk.com/sql/t-sql-programming/binary-trees-in-sql/
For fun, write code to balance a tree in this format.
--CELKO--
|
|
|
|

|
Your Idea is very basic that in binary Tree if parent node has an array position of (n), the the left and right children are at (2n) and (2n+1) - similarly if the tree is tertiary then same parent node at n will have children at position (3n-1) , (3n) , (3n+1) - on this very idea if a tree in suppose m-ary (that is having m child) then a parent at position (n) will have Left most node at (m*n)-(m-2) , and right most node is (m*n) + 1 and the other nodes are in between them.
Infact, there are multiple ways to tackle the same problem - (each solution has some benefit over the other) - I had just shown one of the way to deal with the problem.
Moreover This procedure is not too complex at most - there are 2 Table & 1 Trigger for all, All the basic Query is being done using Simple Select statement , The Query which has been written using CTE can also be written using Select Statement,
for example consider the query A .How To find the Balanced node due to new node entry (with reference to Tree -2 )
With CTE as (
Select B.Nodes_IN_PK_Code as PK, B.Nodes_VC_xx_NodeData as NodeData, Hiera_IN_xx_NodeLevel as NodeLevel,
'' 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 = 7 )
Select CTE.PK, CTE.NodeData, CTE.NodeLevel,
Case When ((CTE.TotalNodeForBalanceTree / 2 = TotalLeftChild) and (CTE.TotalNodeForBalanceTree / 2 = TotalRightChild)) then 'Yes' Else' No' End As IsBalanced,
CTE.TotalNodeForBalanceTree , CTE.TotalLeftChild , CTE.TotalRightChild
From CTE
The above code can also be written as
Select B.Nodes_IN_PK_Code as PK, B.Nodes_VC_xx_NodeData as NodeData, Hiera_IN_xx_NodeLevel as NodeLevel,
case when ((Power(2,Hiera_IN_xx_NodeLevel)-2/2) = (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))
and
((Power(2,Hiera_IN_xx_NodeLevel)-2/2) = (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))
then 'YES' Else 'NO' End as IsBalanced
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 = 7
So, There are multiple ways , Choose your way carefully.
AnupKumarYadav
Delhi,India
|
|
|
|

|
hello sir how can we display a tree view for each node or from particular node...??
with regards
vikram
|
|
|
|

|
Fig.1
--'------------------------------------------
--' The Binary Tree Representation
--'------------------------------------------
--' RootNode
--' --------|-------
--' | |
--'------------------------------------------
from the above diagram how can we insert in the Left side of the Root Node and same as for the Right side??
Fig.2
--'------------------------------------------
--' The Binary Tree Representation
--'------------------------------------------
--' RootNode
--' --------|-------
--' | |
--' AL1
--' | |
--' AL2 AR2
--' ---|---
--' | |
--' AL3 AR3
--' | |
--' (?)
--'------------------------------------------
According to Fig.2 how can insert into right side ?
how can we insert into left most(AL3(L) from the root at left side...?
hope u can understand what i need..?
*
Actually i would like to know how can add the node at the left end most and at the right end most?
if there is no node in the right side from the Root Node then how can we detect that there is no node in the right side from the ROOT NODE ? if there is node then how to detect the right end most and add the node after it.?
modified on Thursday, December 9, 2010 6:36 AM
|
|
|
|

|
--'------------------------------------------
--' The Binary Tree Representation
--'------------------------------------------
--' RootNode
--' --------|-------
--' | |
--' AL AR
--' -----|----- -----|-----
--' | | | |
--' ZL ZR BL BR
--' ---|--- ---|---
--' | | | |
--' CL CR DL *DR*
--'------------------------------------------
To add new node below any node to either its extreme right or extreme left you just need to know the last node (Leaf) in the corresponding side. This you can get using the simple SQL Query.
For Eg. In the Tree above extreme right of root is *DR* , and extreme left is ZL.
similarly extreme right of Node AR is *DR* and extreme Left is CL.
-- This SQL will return extreme Left Or Right Node of any Parent node
;with CTE (ExtremeNode, Position, NodeLevel )As
(
Select Hiera_IN_FK_ChildCode, Hiera_CH_xx_Position, 1 from tbl_BinaryTree_Hierarchy
where Hiera_IN_xx_NodeLevel=2 and Hiera_IN_FK_ParentCode = ?ParentNodePK? and Hiera_CH_xx_Position = ?LegSide?
UNION ALL
Select Hiera_IN_FK_ChildCode, Hiera_CH_xx_Position , CTE.NodeLevel + 1 from tbl_BinaryTree_Hierarchy
INNER JOIN CTE ON CTE.ExtremeNode = Hiera_IN_FK_ParentCode and CTE.Position = Hiera_CH_xx_Position
where Hiera_IN_xx_NodeLevel = 2
)
Select Top 1 * From CTE order By NodeLevel Desc;
-- Do vote & bookmark if it was helpful to you, thanks
AnupKumarYadav
Delhi,India
|
|
|
|

|
Thanks sir. Its solve my problem regarding extreme Left Or Right Node.
Really u r genius...
with regards
vikram
|
|
|
|

|
--'------------------------------------------
--' The Binary Tree Representation
--'------------------------------------------
--' RootNode
--' --------|-------
--' | |
--' AL AR
--' -----|----- -----|-----
--' | | | |
--' ZL ZR BL BR
--' ---|--- ---|---
--' | | | |
--' CL CR DL *DR*
--'------------------------------------------
Hi, with reference to your query & Tree above, what I conclude is that when *DR* is added then BR & AR gets paid - but how much is not clear - According to general MLM trend I can figure out that BR will get benefit of 1 Pair, and AR will get benefit of 1 pair.
Let us assume that you want to give benefit of 1 unit pair to BR and also to AR the benefit of 1 unit pair ( as you might had been already given the benefit of 1 unit pair when DL was added.)
So you need to find out the list of parent Nodes w.r.t the newly added node who will get benefit.
Let us assume the Primary Key of the newly added node (*DR*) is 123456.
Now to get the list of nodes who needs to be paid is as follows..
The SQL Query below will give you the list of all the parents with last column ShouldIPay (Pay or DoNotPay) indicating which parent node needs to be paid.
(for the sake of keeping the sql similar to the previous one I had just added the column "Hiera_CH_xx_Position" in the CTE & added a calculated column in the result set "ShouldIPay")
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 = 123456 )
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
Hope this solves your problem.
*Happy Coding*
AnupKumarYadav
Delhi,India
modified on Thursday, December 9, 2010 10:26 PM
|
|
|
|

|
Its really Nice and Great, i am very thankful.
|
|
|
|

|
This article helps me alot..
thanks AnupKumarYadav.
with regards
vikram
|
|
|
|

|
Thanks, Vikram - Please do vote,bookmark any article you liked in appreciation. This raises the joy of sharing.
AnupKumarYadav
Delhi,India
|
|
|
|

|
Its a nice article to share with the people like us, thank you.
|
|
|
|

|
my pleasure, Thanks
AnupKumarYadav
Delhi,India
|
|
|
|

|
Nice article. Thanks for sharing
|
|
|
|

|
Thanks Jai, I am writing an article for Data Structure to create dynamically configurable Invoicing/Billing Structure . I'll inform you when I publish It.
AnupKumarYadav
Delhi,India
|
|
|
|
 |
|
|
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 | 38,296 |
| Downloads | 0 |
| Bookmarked | 18 times |
|
|