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 195965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:43:37+00:00 2026-05-11T16:43:37+00:00

What algorithms can be used to draw a binary tree in the console? The

  • 0

What algorithms can be used to draw a binary tree in the console? The tree is implemented in C. For example, a BST with numbers: 2 3 4 5 8 would be shown in the console as:

alt text

  • 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-11T16:43:38+00:00Added an answer on May 11, 2026 at 4:43 pm

    Check out Printing Binary Trees in Ascii

    From @AnyOneElse Pastbin below:

    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !!!Code originally from /http://www.openasthra.com/c-tidbits/printing-binary-trees-in-ascii/
    !!! Just saved it, cause the website is down.
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    Printing Binary Trees in Ascii
    
    Here we are not going to discuss what binary trees are (please refer this, if you are looking for binary search trees), or their operations but printing them in ascii.
    
    The below routine prints tree in ascii for a given Tree representation which contains list of nodes, and node structure is this
    
        struct Tree 
        {
          Tree * left, * right;
          int element;
        };
    
    This pic illustrates what the below routine does on canvas..
    ascii tree
    
    Here is the printing routine..
    
        b5855d39a6b8a2735ddcaa04a404c125001 
    
    Auxiliary routines..
    
        //This function prints the given level of the given tree, assuming
        //that the node has the given x cordinate.
        void print_level(asciinode *node, int x, int level) 
        {
          int i, isleft;
          if (node == NULL) return;
          isleft = (node->parent_dir == -1);
          if (level == 0) 
          {
            for (i=0; i<(x-print_next-((node->lablen-isleft)/2)); i++) 
            {
              printf(" ");
            }
            print_next += i;
            printf("%s", node->label);
            print_next += node->lablen;
          } 
          else if (node->edge_length >= level) 
          {
            if (node->left != NULL) 
            {
              for (i=0; i<(x-print_next-(level)); i++) 
              {
                printf(" ");
              }
              print_next += i;
              printf("/");
              print_next++;
            }
            if (node->right != NULL) 
            {
              for (i=0; i<(x-print_next+(level)); i++) 
              {
                printf(" ");
              }
              print_next += i;
              printf("\\");
              print_next++;
            }
          } 
          else 
          {
            print_level(node->left, 
                        x-node->edge_length-1, 
                        level-node->edge_length-1);
            print_level(node->right, 
                        x+node->edge_length+1, 
                        level-node->edge_length-1);
          }
        }
    
    
        //This function fills in the edge_length and 
        //height fields of the specified tree
        void compute_edge_lengths(asciinode *node) 
        {
          int h, hmin, i, delta;
          if (node == NULL) return;
          compute_edge_lengths(node->left);
          compute_edge_lengths(node->right);
    
          /* first fill in the edge_length of node */
          if (node->right == NULL && node->left == NULL) 
          {
            node->edge_length = 0;
          } 
          else 
          {
            if (node->left != NULL) 
            {
              for (i=0; i<node->left->height && i < MAX_HEIGHT; i++) 
              {
                rprofile[i] = -INFINITY;
              }
              compute_rprofile(node->left, 0, 0);
              hmin = node->left->height;
            } 
            else 
            {
              hmin = 0;
            }
            if (node->right != NULL) 
            {
              for (i=0; i<node->right->height && i < MAX_HEIGHT; i++) 
              {
                lprofile[i] = INFINITY;
              }
              compute_lprofile(node->right, 0, 0);
              hmin = MIN(node->right->height, hmin);
            } 
            else 
            {
              hmin = 0;
            }
            delta = 4;
            for (i=0; i<hmin; i++) 
            {
              delta = MAX(delta, gap + 1 + rprofile[i] - lprofile[i]);
            }
    
            //If the node has two children of height 1, then we allow the
            //two leaves to be within 1, instead of 2 
            if (((node->left != NULL && node->left->height == 1) ||
                  (node->right != NULL && node->right->height == 1))&&delta>4) 
            {
              delta--;
            }
    
            node->edge_length = ((delta+1)/2) - 1;
          }
    
          //now fill in the height of node
          h = 1;
          if (node->left != NULL) 
          {
            h = MAX(node->left->height + node->edge_length + 1, h);
          }
          if (node->right != NULL) 
          {
            h = MAX(node->right->height + node->edge_length + 1, h);
          }
          node->height = h;
        }
    
        asciinode * build_ascii_tree_recursive(Tree * t) 
        {
          asciinode * node;
    
          if (t == NULL) return NULL;
    
          node = malloc(sizeof(asciinode));
          node->left = build_ascii_tree_recursive(t->left);
          node->right = build_ascii_tree_recursive(t->right);
    
          if (node->left != NULL) 
          {
            node->left->parent_dir = -1;
          }
    
          if (node->right != NULL) 
          {
            node->right->parent_dir = 1;
          }
    
          sprintf(node->label, "%d", t->element);
          node->lablen = strlen(node->label);
    
          return node;
        }
    
    
        //Copy the tree into the ascii node structre
        asciinode * build_ascii_tree(Tree * t) 
        {
          asciinode *node;
          if (t == NULL) return NULL;
          node = build_ascii_tree_recursive(t);
          node->parent_dir = 0;
          return node;
        }
    
        //Free all the nodes of the given tree
        void free_ascii_tree(asciinode *node) 
        {
          if (node == NULL) return;
          free_ascii_tree(node->left);
          free_ascii_tree(node->right);
          free(node);
        }
    
        //The following function fills in the lprofile array for the given tree.
        //It assumes that the center of the label of the root of this tree
        //is located at a position (x,y).  It assumes that the edge_length
        //fields have been computed for this tree.
        void compute_lprofile(asciinode *node, int x, int y) 
        {
          int i, isleft;
          if (node == NULL) return;
          isleft = (node->parent_dir == -1);
          lprofile[y] = MIN(lprofile[y], x-((node->lablen-isleft)/2));
          if (node->left != NULL) 
          {
            for (i=1; i <= node->edge_length && y+i < MAX_HEIGHT; i++) 
            {
              lprofile[y+i] = MIN(lprofile[y+i], x-i);
            }
          }
          compute_lprofile(node->left, x-node->edge_length-1, y+node->edge_length+1);
          compute_lprofile(node->right, x+node->edge_length+1, y+node->edge_length+1);
        }
    
        void compute_rprofile(asciinode *node, int x, int y) 
        {
          int i, notleft;
          if (node == NULL) return;
          notleft = (node->parent_dir != -1);
          rprofile[y] = MAX(rprofile[y], x+((node->lablen-notleft)/2));
          if (node->right != NULL) 
          {
            for (i=1; i <= node->edge_length && y+i < MAX_HEIGHT; i++) 
            {
              rprofile[y+i] = MAX(rprofile[y+i], x+i);
            }
          }
          compute_rprofile(node->left, x-node->edge_length-1, y+node->edge_length+1);
          compute_rprofile(node->right, x+node->edge_length+1, y+node->edge_length+1);
        }
    
    Here is the asciii tree structure…
    
        struct asciinode_struct
        {
          asciinode * left, * right;
    
          //length of the edge from this node to its children
          int edge_length; 
    
          int height;      
    
          int lablen;
    
          //-1=I am left, 0=I am root, 1=right   
          int parent_dir;   
    
          //max supported unit32 in dec, 10 digits max
          char label[11];  
        };
    

    output:

            2
           / \
          /   \
         /     \
        1       3
       / \     / \
      0   7   9   1
     /   / \     / \
    2   1   0   8   8
           /
          7
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.