--'------------------------------------------ --' The Binary Tree Representation --'------------------------------------------ --' RootNode --' --------|------- --' | | --' AL AR --' -----|----- -----|----- --' | | | | --' ZL ZR BL BR --' ---|--- ---|--- --' | | | | --' CL CR DL *DR* --'------------------------------------------ --How To find the Balanced node due to new node entry -- Lets us assume that the newly added node is *DR* - (which makes AR, BR balanced) -- We can use simple SQL Query or Common Table Expressions (CTE) on SQL 2005 to get the data 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 -- (Primary key of Newly Added Node) ) 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 ------------------------------------------------------------------------------------------- -- Finding extreme left & right leaf of node 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. ;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; ------------------------------------------------------------------------------------------ --To get Node List which became balanced nodes due to new insertion 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 -- (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 ------------------------------------------------------------------------------------------------