Make a generic TreeNode class with link weights in C#

[generic TreeNode class]

The post Handle generic TreeNode mouse events in C# explains how you can build and draw a tree where the nodes contain generic items. This example adds the ability to display link weights.

The generic TreeNode class now has a LinkWeight property that holds the weight (or cost) of the link from the node’s parent to the node. The AddChild method now includes a parameter that sets that cost when you add a node to the tree.

// Add a TreeNode to our Children list.
public void AddChild(TreeNode<T> child)
    AddChild(child, int.MinValue);
public void AddChild(TreeNode<T> child, int link_weight)
    child.LinkWeight = link_weight;

The program uses the following DrawLink method to draw a link from a node to its child.

// Draw a link with its weight.
private void DrawLink(Graphics gr,
    int link_weight, PointF point1, PointF point2)
    // Draw the link.
    gr.DrawLine(MyPen, point1, point2);

    // Display the link's weight.
    if (link_weight > int.MinValue)
        PointF middle = new PointF(
            (point1.X + point2.X) / 2f,
            (point1.Y + point2.Y) / 2f);
        SizeF size = gr.MeasureString(
            link_weight.ToString(), LinkWeightFont);
        RectangleF rect = new RectangleF(
            middle.X - size.Width / 2f,
            middle.Y - size.Height / 2f,
        gr.FillRectangle(WeightBgBrush, rect);

        using (StringFormat sf = new StringFormat())
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            LinkWeightFont, LinkWeightBrush, rect, sf);

This method draws a line representing the link. Then if the link’s weight is greater than int.MinValue (which is used to represent a link with no weight) the method draws the link’s weight.

To draw the weight, the code first finds the point in the middle of the link. It calls the Graphics object’s MeasureString method to see how big the link’s weight will be when drawn. It then creates a rectangle of that size centered on the link’s midpoint and fills the rectangle with the link background color (which was chosen to match the form’s background).

Next the method creates a StringFormat object to center text and draws the link’s weight inside the rectangle.

When you right-click on a node, a context menu lets you add or remove nodes. The Add Child dialog contains a new text box where you can enter the node’s link weight.

Download the example to see additional details.

Download Example   Follow me on Twitter   RSS feed   Donate

About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in algorithms, classes, generic, OOP and tagged , , , , , , , , , , , , , . Bookmark the permalink.

6 Responses to Make a generic TreeNode class with link weights in C#

  1. NAMANE SARA says:

    i thank you a lot for the example , i have another question the node can be colored ? for example leaves will have different color blue for example and other nodes red ? i can add that or it is not possible i have to add picture with color ??

  2. Namane sara says:

    Thank you i found how to color the nodes just another question can i add under each leaf a box where i put the shortest path from the root??

    • RodStephens says:

      You could do that but I don’t have any code that does that.

      You could make the arrange method calculate the depth. Or you could do it as you add each node.

      Then modify the code that draws a node to draw the new value.

Comments are closed.