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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:43:35+00:00 2026-05-28T07:43:35+00:00

I need help in understanding how to code a pretty printer for any given

  • 0

I need help in understanding how to code a pretty printer for any given binary tree.

I know the first step is to do preorder to get all of the nodes.

I know that during the preorder traversal, that’s where all of the prettyness will be implemented.

Just not sure how to get started. I am given a preorder traversal function that works but i’m not sure where to start modifying it or if I should just make my own function.

No code, just asking for ideas on how others would go about it.

And maybe later code if I get desperate 😛

To be specific, It should look like this:

example

  • 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-28T07:43:36+00:00Added an answer on May 28, 2026 at 7:43 am

    If you know the depth of the tree, i.e. the number of levels you can calculate the maximum number of nodes in the last level (which is n2 for n=number of levels – 1, 1 element if you have the root only). If you further know the width of the elements (e.g. if you have at most 2-digit numbers each element would have width 2) you can calculate the width of the last level:
    ((number of elements - 1) * (element width + spacing) + element width).

    Actually the above paragraph doesn’t matter. All you need is the depth of the tree, i.e. the maximum level that is to be displayed. However, if the tree is sparse, i.e. not all elements of the last level and maybe above are present, you’ll need to get the position of the node you’re rendering in order to adapt the indent/spacing for that case accordingly.

    In your pre-order iteration you can then calculate the indentation and spacing between the elements at each level. The formula for the indent would be: 2(max level – level) – 1 and for the spacing: 2(max level – level + 1) – 1 (level is 1-based)

    Example:

           1
       2       3
     4   5   6   7
    8 9 A B C D E F
    

    In that tree, the number of levels is 4. The spacing at the last level is 1 whereas the indent is 0. You’ll get the following values:

    • level 1:
      • indent = 7: (2(4-1) – 1 = 23 – 1 = 8 – 1 = 7)
      • first level, so spacing doesn’t matter
    • level 2:
      • indent = 3: (2(4-2) – 1 = 22 – 1 = 4 – 1 = 3)
      • spacing = 7 (2(4-1) – 1 = 23 – 1 = 8 – 1 = 7)
    • level 3:
      • indent = 1: (2(4-3) – 1 = 21 – 1 = 2 – 1 = 1)
      • spacing = 3 (2(4-2) – 1 = 22 – 1 = 4 – 1 = 3)
    • level 4:
      • indent = 0: (2(4-4) – 1 = 20 – 1 = 1 – 1 = 0)
      • spacing = 1: (2(4-3) – 1 = 21 – 1 = 2 – 1 = 1)

    Note that at the last level you’ll always have spacing 1 * element width. Thus for a maximum element width of 3 (e.g. 3-digit numbers) you’d have a spacing of 3 at the last level, in order to get some pretty alignment of the upper levels.

    Edit: For your pretty print, you’d just have to calculate the indent as width element * level where level would be zero-based. Then if the node is not a leaf, draw it with a opening paranthesis in front and a closing paranthesis after drawing the children, if it is a leaf just draw it and if the leaf is missing, draw double paranthis.

    Thus you’d get something like this:

    public void printSubtree( int indent, node ) {
      for( int i = 0; i < indent; ++i) {
        System.out.print(" ");
      }
    
      if( inner node) {
        System.out.println("(" + value);     
        printSubtree(indent + elem width, left child); //this is a recursive call, alternatively use the indent formula above if you don't use recursion
        printSubtree(indent + elem width, right child);
    
        //we have a new line so print the indent again
        for( int i = 0; i < indent; ++i) {
          System.out.print(" ");
        }
        System.out.println(")"); 
      } else if( not empty) {
        System.out.println(value);
      } else { //empty/non existing node
        System.out.println("()");
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help understanding this bit of code pre { white-space: pre; white-space:
I need some help understanding what's happening here. This code is from a models/log.py
I'm brand new to python, and need some help understanding this code fragment: for
Iam a perl newbie and need help in understanding the below piece of code.
I need help in understanding the code snipped below...allocate is a function that would
I need help understanding how to implement this part of PHP code in Ruby.
I need help in understanding the following C++ code (in a .h file): bool
I need help understanding some C++ operator overload statements. The class is declared like
I'm a SQL noob, and I need a little bit of help understanding the
I need help on this following aspx code aspx Code: <asp:Label ID =lblName runat

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.