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

  • Home
  • SEARCH
  • 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 6724797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:43:54+00:00 2026-05-26T09:43:54+00:00

So there is a great open source tutorial on creating TreeNode class . It

  • 0

So there is a great open source tutorial on creating TreeNode class. It is great. But I wonder how to change its Arrange function to make it draw indented tree?

Here’s the function:

// Arrange the node and its children in the allowed area.
// Set xmin to indicate the right edge of our subtree.
// Set ymin to indicate the bottom edge of our subtree.
public void Arrange(Graphics gr, ref float xmin, ref float ymin)
{
    // See how big this node is.
    SizeF my_size = Data.GetSize(gr, MyFont);

    // Recursively arrange our children,
    // allowing room for this node.
    float x = xmin;
    float biggest_ymin = ymin + my_size.Height;
    float subtree_ymin = ymin + my_size.Height + Voffset;
    foreach (TreeNode<T> child in Children)
    {
        // Arrange this child's subtree.
        float child_ymin = subtree_ymin;
        child.Arrange(gr, ref x, ref child_ymin);

        // See if this increases the biggest ymin value.
        if (biggest_ymin < child_ymin) biggest_ymin = child_ymin;

        // Allow room before the next sibling.
        x += Hoffset;
    }

    // Remove the spacing after the last child.
    if (Children.Count > 0) x -= Hoffset;

    // See if this node is wider than the subtree under it.
    float subtree_width = x - xmin;
    if (my_size.Width > subtree_width)
    {
        // Center the subtree under this node.
        // Make the children rearrange themselves
        // moved to center their subtrees.
        x = xmin + (my_size.Width - subtree_width) / 2;
        foreach (TreeNode<T> child in Children)
        {
            // Arrange this child's subtree.
            child.Arrange(gr, ref x, ref subtree_ymin);

            // Allow room before the next sibling.
            x += Hoffset;
        }

        // The subtree's width is this node's width.
        subtree_width = my_size.Width;
    }

    // Set this node's center position.
    Center = new PointF(
        xmin + subtree_width / 2,
        ymin + my_size.Height / 2);

    // Increase xmin to allow room for
    // the subtree before returning.
    xmin += subtree_width;

    // Set the return value for ymin.
    ymin = biggest_ymin;
}

How it looks now:

enter image description here

How indented tree looks like ( image based on DmitryG s grate answer):

enter image description here

So.. How do I make it draw the graph in the indented form?

  • 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-05-26T09:43:55+00:00Added an answer on May 26, 2026 at 9:43 am

    I have modified the TreeNode.Arrange() method for indented tree arrangement.

    Now it looks like this:

    public void Arrange(Graphics gr, ref float xmin, ref float ymin) {
        // See how big this node is.
        SizeF my_size = Data.GetSize(gr, MyFont);
        // Recursively arrange our children,
        // allowing room for this node.
        float y = ymin + my_size.Height;
        float biggest_xmin = xmin + my_size.Width;
        float subtree_xmin = xmin + my_size.Width + Hoffset;
        foreach(TreeNode<T> child in Children) {
            // Arrange this child's subtree.
            float child_xmin = subtree_xmin;
            child.Arrange(gr, ref child_xmin, ref y);
            // See if this increases the biggest ymin value.
            if(biggest_xmin < child_xmin) biggest_xmin = child_xmin;
            // Allow room before the next sibling.
            y += Voffset;
        }
        // Remove the spacing after the last child.
        if(Children.Count > 0) y -= Voffset;
        // See if this node is wider than the subtree under it.
        float subtree_height = y - ymin;
        if(my_size.Height > subtree_height) {
            y = ymin + (my_size.Height - subtree_height) / 2;
            foreach(TreeNode<T> child in Children) {
                // Arrange this child's subtree.
                child.Arrange(gr, ref subtree_xmin, ref y);
                y += Voffset;
            }
            subtree_height = my_size.Height;
        }
        // Set this node's center position.
        Center = new PointF(xmin + my_size.Width / 2, ymin + my_size.Height / 2);
        ymin += subtree_height;
        xmin = biggest_xmin;
    }
    

    Note, that the DrawSubtreLinks() method have also been modified:

    private void DrawSubtreeLinks(Graphics gr) {
        foreach(TreeNode<T> child in Children) {
            PointF p = new PointF(Center.X, child.Center.Y);
            gr.DrawLine(MyPen, Center, p);
            gr.DrawLine(MyPen, p, child.Center);
            child.DrawSubtreeLinks(gr);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i know nothing about medical records but im sure there's great opportunity in it
There exist any open source cms rtl ltr with a front end to work
DroidFu and GreenDroid are great open source projects which I would like to utilize
Are there any open-source examples of ASP.NET MVC applications that use the Entity Framework?
Are there solutions/tutorials/open source solutions to providing the functionality of having Content Editors edit
Are there any open source libraries for representing cooking units such as Teaspoon and
I always have found the open source space interesting but have never actually participated
Is there any open source (or example) code for Ruby on Rails which can
Is there an open source TCL script generator for NS2 ? There is NAM
I am using an FPS counter class from an open source project and I

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.