المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : List tree help!



C# Programming
10-11-2009, 10:01 AM
I have started to make a basic generic list tree how ever when it comes to adding a parent that has more than one node I have realised that i need to some how dynamicly add loops to i can display the details from "n" number of nodes attached, of course this is a silly idea so i need some help on finding a more feasible solution, can any body help me?

This is my source :

class Program
{
static void Main(string[] args)
{
CsTree tree = new CsTree();
tree.AddNode(new CsNode(new CsBox(1,2,4,8)));


tree.NodeList[0].AddChild(new CsBox(100,200,400,800));
tree.NodeList[0].AddChild(new CsBox(2, 3, 4, 5));
tree.AddNode(new CsNode(new CsBox(1, 1, 1, 1)));
tree.NodeList[0].NodeMembersList[0].AddChild(new CsBox(2, 2, 2, 2));



tree.DisplayAllNodeData();

//Want this to be added for the CsTree>DisplayAllNodeData() but how can i dertermine how many loops?
tree.NodeList[0].NodeMembersList[0].NodeMembersList[0].NodeData.DisplayDetails();
Console.ReadKey();

}
}

class CsNode
{
public T NodeData;
public Dictionary NodeMembersList = new Dictionary();
public int NodeMemberCount = 0;

public CsNode(T nodeData)
{
NodeData = nodeData;

}
public void AddChild(T childData)
{
NodeMembersList.Add(NodeMemberCount, new CsNode(childData));
NodeMemberCount++;
}

}

class CsTree
{
public Dictionary NodeList = new Dictionary();
public int TotalNodes = 0;


public void AddNode(CsNode node)
{
NodeList.Add(TotalNodes, node);
TotalNodes++;
}

//Only two loops so far as i realised i dont know how to dynamicly decide how many loops are needed
public void DisplayAllNodeData()
{
for (int i = 0; i < TotalNodes;i++ )
{

NodeList[i].NodeData.DisplayDetails();
for (int k = 0; k < NodeList[i].NodeMemberCount; k++)
{
NodeList[i].NodeMembersList[k].NodeData.DisplayDetails();
}
}
}

}
class CsBox
{
public int X;
public int Y;
public int Width;
public int Height;

public CsBox(int x,int y,int width,int height)
{
X = x;
Y = y;
Width = width;
Height = height;
}

public void DisplayDetails()
{
Console.WriteLine("X {0} Y {1} Width {2} Height {3}",X,Y,Width,Height);
}
}
So far i have managed to get it to display the details of The Base node, and its child nodes attached, i want it so i can display an unknown number of nodes attached to the child nodes if any, ie Base- Child - node - node ect instead of just Base - Child
http://www.barakasoft.com/script/Forums/Images/smiley_doh.gif