Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7765155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:06:01+00:00 2026-06-01T15:06:01+00:00

I am trying to create a tree like node diagram, like the example image

  • 0

I am trying to create a tree like node diagram, like the example image here. I have the following code:

    private void DrawNode(Graphics g, Node<T> node, float xOffset, float yOffset)
    {
        if (node == null)
        {
            return;
        }

        Bitmap bmp = (from b in _nodeBitmaps where b.Node.Value.Equals(node.Value) select b.Bitmap).FirstOrDefault();

        if (bmp != null)
        {
            g.DrawImage(bmp, xOffset, yOffset);

            DrawNode(g, node.LeftNode, xOffset - 30 , yOffset + 20);
            DrawNode(g, node.RightNode, xOffset + 30, yOffset + 20);
        }
    }

My code is almost working. The problem I’m having is that some of the nodes are overlapping. In the picture above, nodes 25 and 66 are overlapping. The reason, I’m sure, is because its mathematically laying the left nodes and right nodes equal space, so the parent’s right node overlaps with the adjacent parent’s left node. How can I fix this problem?

UPDATE:

This is the code update I made after dtb’s suggestion:

            int nodeWidth = 0;
            int rightChildWidth = 0;

            if (node.IsLeafNode)
            {
                nodeWidth = bmp.Width + 50;
            }
            else
            {
                int leftChildWidth = 0;

                Bitmap bmpLeft = null;
                Bitmap bmpRight = null;

                if (node.LeftNode != null)
                {
                    bmpLeft =
                        (from b in _nodeBitmaps where b.Node.Value.Equals(node.LeftNode.Value) select b.Bitmap).
                            FirstOrDefault();
                    if (bmpLeft != null)
                        leftChildWidth = bmpLeft.Width;
                }
                if (node.RightNode != null)
                {
                    bmpRight =
                        (from b in _nodeBitmaps where b.Node.Value.Equals(node.RightNode.Value) select b.Bitmap).
                            FirstOrDefault();
                    if (bmpRight != null)
                        rightChildWidth = bmpRight.Width;
                }

                nodeWidth = leftChildWidth + 50 + rightChildWidth;
            }


            g.DrawImage(bmp, xOffset + (nodeWidth - bmp.Width) / 2, yOffset);

            if (node.LeftNode != null)
            {
                DrawNode(g, node.LeftNode, xOffset, yOffset + 20);
            }
            if (node.RightNode != null)
            {
                DrawNode(g, node.RightNode, xOffset + nodeWidth - rightChildWidth, yOffset + 20);
            }

Here is a screenshot from this code: Screen Shot

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:06:02+00:00Added an answer on June 1, 2026 at 3:06 pm

    Assign a width to each node:

    • the width of a leaf is the width of the image, w.
    • the width of a node is the width of its left child node + a constant d + the width of the right child node.

           Illustration

    void CalculateWidth(Node<T> node)
    {
        node.Width = 20;
        if (node.Left != null)
        {
            CalculateWidth(node.Left);
            node.Width += node.Left.Width;
        }
        if (node.Right != null)
        {
            CalculateWidth(node.Right);
            node.Width += node.Right.Width;
        }
        if (node.Width < bmp.Width)
        {
            node.Width = bmp.Width;
        }
    }
    

    Starting with the root node and x = 0, draw the image at the halve of the width, offset by x.
    Then calculate the x position for each child node and recurse:

    void DrawNode(Graphics g, Node<T> node, double x, double y)
    {
        g.DrawImage(x + (node.Width - bmp.Width) / 2, y, bmp);
    
        if (node.Left != null)
        {
            DrawNode(g, node.Left, x, y + 20);
        }
        if (node.Right != null)
        {
            DrawNode(g, node.Right, x + node.Width - node.Right.Width, y + 20);
        }
    }
    

    Usage:

    CalculateWidth(root);
    
    DrawNode(g, root, 0, 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create an expression tree that represents the following: myObject.childObjectCollection.Any(i =>
I'm trying to create an expression tree dynamicly. Let asume that I have two
I have a python script that is trying to create a directory tree dynamically
I'm trying to create a large XML tree in R. Here's a simplified version
A little advise please. I am trying to create a tree out of following
hello i am trying to create a tree like menu (kind of like the
I am trying to create a heterogeneous tree based on a sample provided here:
I'm trying to create routes which follow the structure of a tree navigation system,
I am trying to implement a B+ tree myself, but I want to create
I have a view that will look like this: I'm trying to figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.