• 0

[Help in C#] Intregate Doubly Link list with the Tree-Node


Question

x29qw6.jpg

Well, here is want i want to do

DT is the starting point which is connected to point "1".

So load on "DT" is equal to load on point "1".

But point "1" is connected to 3SP and point "2".

So load on point "1" = load on 3 SP + load on point"2"

and so on.... till I reach the tree end ie point "6" which is having only one SP

Thus final load on

DT = load on "1" = 3 SP + load on "2" = 3 SP + (load on "3") = 3 SP + ((2 SP + load on "4")).... = 3 SP + .......... + load on "6" = 3 SP + .............. + SP on 6

By this i can able to get the total load on DT as well as individual loads on each points.

I not so sure how do i proceed with it........... but may be doubly link list of my interest (but i m not able to use it)

Any help would be deeply appreciated

Attach is my project

DynamicTree.zip

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

How are you equating load to values?

You'll need a recursive function to calculate loads starting from any node:

public int CalculateLoad(TreeNode node)
{
	if (node == null) return 0;

	int load = 0;
	foreach (TreeNode child in node.Nodes)
	{
		load += 1;
		load += CalculateLoad(child);
	}
	return load;
}

Starting at what ever node you need. During for foreach block, you can change the weighting of the different types of nodes etc.

Hope that helps. :)

Link to comment
Share on other sites

  • 0
How are you equating load to values?

You'll need a recursive function to calculate loads starting from any node:

public int CalculateLoad(TreeNode node)
{
	if (node == null) return 0;

	int load = 0;
	foreach (TreeNode child in node.Nodes)
	{
		load += 1;
		load += CalculateLoad(child);
	}
	return load;
}

Starting at what ever node you need. During for foreach block, you can change the weighting of the different types of nodes etc.

Hope that helps. :)

The loads are stored in [Load] columns. A little more description would be great help :)

u can c the attached project for more detail

Link to comment
Share on other sites

  • 0

Well, I see, you are storing the values in the datatable, but what you could do is store the load values in the treeview nodes themselves:

private void initTreeView(TreeNode N)
{
	// ...
	foreach (DataRow r3 in temp.Rows)
	{
		TreeNode tn = new TreeNode();
		tn.Text = r3["Info"].ToString();

		// Added:
		tn.Tag = r3["Load"].ToString();

		initTreeView(tn);
		N.Nodes.Add(tn);
	}
}

And then use that to caculate the loads:

public int CalculateLoad(TreeNode node)
{
	if (node == null) return 0;

	int load = 0;
	foreach (TreeNode child in node.Nodes)
	{
		if (child.Tag != null)
		  load += int.Parse(child.Tag.ToString());

		load += CalculateLoad(child);
	}
	return load;
}

Link to comment
Share on other sites

  • 0
Well, I see, you are storing the values in the datatable, but what you could do is store the load values in the treeview nodes themselves:

private void initTreeView(TreeNode N)
{
	// ...
	foreach (DataRow r3 in temp.Rows)
	{
		TreeNode tn = new TreeNode();
		tn.Text = r3["Info"].ToString();

		// Added:
		tn.Tag = r3["Load"].ToString();

		initTreeView(tn);
		N.Nodes.Add(tn);
	}
}

And then use that to caculate the loads:

public int CalculateLoad(TreeNode node)
{
	if (node == null) return 0;

	int load = 0;
	foreach (TreeNode child in node.Nodes)
	{
		if (child.Tag != null)
		  load += int.Parse(child.Tag.ToString());

		load += CalculateLoad(child);
	}
	return load;
}

Thank you so much, well I am able to calculate the total load on DT ie at the Tree Top

But what if i want to calculate total load on each node ie 1,2,3,4,5,6

Link to comment
Share on other sites

  • 0

Thats ok, the CalculateLoad method accepts a TreeNode is its parameter. So instead of passing in the root node, pass in a node from further in the tree.

Link to comment
Share on other sites

  • 0
Thats ok, the CalculateLoad method accepts a TreeNode is its parameter. So instead of passing in the root node, pass in a node from further in the tree.

A little more explanation would be kind of u?I have just started programming :(

Link to comment
Share on other sites

  • 0

Well, are you currently doing something like this to calculate the load of the entire tree?:

int load = CalculateLoad(treeView1.Nodes[0]);

Instead of passing in that first node of the treeView1 control, pass on of its child nodes, e.g.:

int load = CalculateLoad(treeView1.Nodes[0].Nodes[1]);

Any bettter?

Link to comment
Share on other sites

  • 0

Srry......... for a very late reply

Solved the problem by adding following function

foreach (DataRow r3 in temp.Rows)
			{
				TreeNode tn = new TreeNode();
				tn.Text = r3["ID"].ToString();
				tn.Tag = r3["Load"].ToString();
				initTreeView(tn);				
				[b]load =load + CalculateLoad(tn);[/b]//added line to calculate the load at each node and downstream
				N.Nodes.Add(tn);
				if (tn.Text == "SP")
				{
				}
				else
				MessageBox.Show("Load at Node: " + tn.Text.ToString() +  "=" + load.ToString());					   
			}


public int CalculateLoad(TreeNode node)
		{
			int load = 0;
			if (node == null) return 0; 

			foreach (TreeNode child in node.Nodes)
			{
				if (child.Tag != null)
					load += int.Parse(child.Tag.ToString());

				load += CalculateLoad(child);
			}			
			return load;
		}

Please do find the attached copy of the sample code

Thank you so much for your help

tree.zip

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.