Click here to Skip to main content
15,904,935 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing a MLM(Multi Level Marketing) web application through ASP.NET 4.0 with C# and this is my first project as I am a fresher. In this project, I have a need to create a binary tree. When a new customer joins the organization then the sponsor add him/her to either in his left or to his right. The tree structure must be like this :-

[^]

a
/\
b c
/\/\
defg


The tree is a n-level tree where as soon as a new customer joins, his associated ID
must appears through new node in the tree. In this tree, I also have to count all the left child and all the right child of the parent node.

I have stuck here. How can I code this ? I have googled a lot but haven't found a satisfactory solution yet. Is there any built in tool in .NET? Please provide me the step by step solution. Your help is truly appreciated.

Thanks and Regards
Posted
Updated 29-Oct-15 0:50am
v5
Comments
Nathan Minier 29-Oct-15 8:10am    
Is this basically what you're looking for?

public class Marketer
{
public Marketer Parent { get; protected set; }
public List<marketer> Children { get; protected set; }

...
public void AddSibling(Marketer sibling)
{
this.Parent.Children.Add(sibling);
}
}
Member 12097141 29-Oct-15 11:23am    
In my project, I have the option to add a child either to my left or to my right and also have to count all my children in my left sub tree and all my children in right sub tree. Your code just adds a child to parent not to left or right of the parent. If you have any other solution, then please share it with me. By the way, thanks for the help.
Nathan Minier 29-Oct-15 11:48am    
That's true, I was mostly wondering if a recursive model is what you're looking for. A simple fix is to use the same concept and use fixed properties instead of an array. Since Each node has up to 3 possible connections (if I understand you correctly), then you just set them up as properties:

public class Marketer
{
public Marketer Parent { get; set; }
public Marketer LeftChild { get; set;}
public Marketer RightChild { get; set; }
}

Now that's not a terribly robust method, since it does limit you to a pre-established n direct children.

A better method would be to establish what defines a left vs a right child, and make it testable by comparison. That way if the model is ever expanded you have a way to expand your tree, for instance: left, center, right. That would use the array method I noted earlier with the addition of a weight (or in this case, relative position) method.
Member 11999085 17-Jul-18 7:08am    
Provide me code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900