Tuesday, October 18, 2016

How to count Left and Right Member of MLM binary Tree in C#

How to count Left and Right Member of MLM binary Tree in C#.

 SQL table structure.

 -------------------------------------------------------
Id(int) | Parent_Id (int) | Placement (varchar(10))
-------------------------------------------------------
 1          0                        -
2          1                        Left
3          1                        Right
4          2                        Left
5          2                        Right

 ---------------------------------------------------

 private void pNodeR(string node, string place)
{
     string sql = "select * from biodata_master where parent_no='" + node + "'";

      DataTable dt = new DataTable();
      dt = obj.SelectData(sql);

      if (dt.Rows.Count == 1)
      {
          Cr = Cr + 1;
      }
      else if (dt.Rows.Count > 0)
      {
           Cr = Cr + 2;
           pNodeR(dt.Select("PLACEMENT='Left'")[0].ItemArray[0].ToString(), "LEFT");
           pNodeR(dt.Select("PLACEMENT='Right'")[0].ItemArray[0].ToString(), "RIGHT");
      }
}

 /// <summary>
 /// To Count left node
 /// </summary>
 /// <param name="node"></param>
 /// <param name="place"></param>
private void pNodeL(string node,string place)
{
      string sql = "select * from biodata_master where parent_no='"+node+"'";

       DataTable dt = new DataTable();
       dt = obj.SelectData(sql);

       if (dt.Rows.Count == 1)
       {
           Cl = Cl + 1;
       }
       else if (dt.Rows.Count > 1)
       {
            Cl = Cl + 2;
            pNodeL(dt.Select("PLACEMENT='Left'")[0].ItemArray[0].ToString(), "LEFT");
            pNodeL(dt.Select("PLACEMENT='Right'")[0].ItemArray[0].ToString(), "RIGHT");
       }
}


/* SelectData Method & Connection string */

SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ToString());

public System.Data.DataTable SelectData(string sql)
{
      try
      {
          SqlDataAdapter da = new SqlDataAdapter(sql, con);
          DataTable dt = new DataTable();
          da.Fill(dt);
          return dt;
      }
      catch
      {
          throw;
      }
}

 ---------------------------------------

No comments:

Post a Comment