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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:02:03+00:00 2026-05-26T13:02:03+00:00

I’m implementing a red/black tree in Java and to verify if my tree is

  • 0

I’m implementing a red/black tree in Java and to verify if my tree is correct and to make debugging easier i simply copy/pasted a method that prints out the tree to standard output.

For an input sequence of: 29, 42, 23, 47, 11, 4
the method would print out:

enter image description here

With a little imagination this is in fact a red/black tree, just not with edges between the nodes.
42 is the black root with a right black child 47 and a left red child 23 (red nodes are surrounded by < and >), etc.

This is just fine for smaller trees but becomes a little complicated for larger trees.
Right now the root is to the left and the tree expands to the right.
I was wondering if there are any readily available methods that print out such a tree by printing the root first, and expanding the tree downwards?

Like so:
enter image description here

Or if there is not such a method readily available, how could i change the current method so it prints like the second image?

This is the current method:

private static void printHelper(Node n, int indent) {

    if (n.getRight() != null) {
        printHelper(n.getRight(), indent + INDENT_STEP);
    }

    for (int i = 0; i < indent; i++) {
        System.out.print(" ");
    }

    if (n.getColor() == BLACK) {
        System.out.println(n.getValue());
    } else {
        System.out.println("<" + n.getValue() + ">");
    }

    if (n.getLeft() != null) {
        printHelper(n.getLeft(), indent + INDENT_STEP);
    }
}

And is being called with the root of the tree as node and 0 as indent (and INDENT_STEP is 4).

EDIT: Now that i think of it this is not a specific problem to red/black trees. I’m thus removing the red/black from the title and i replace it with binary tree.

  • 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-26T13:02:03+00:00Added an answer on May 26, 2026 at 1:02 pm

    Dammit, I’m so close to getting this to work as expected! Does anyone know why this still falls short of the required result?

    package tree;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class Tree {
    
        private final static int BLACK = 1;
        private final static int RED = 2;
        private Tree left = null;
        private Tree right = null;
        private int color = BLACK;
        private String value = "";
    
        Tree(final Tree left, final Tree right, final int color, final String value) {
            this.left = left;
            this.right = right;
            this.color = color;
            this.value = value;
        }
    
        Tree getLeft() {
            return left;
        }
    
        Tree getRight() {
            return right;
        }
    
        int getColor() {
            return color;
        }
    
        String getValue() {
            return value;
        }
    
        public static void main(String[] args) {
    
            Tree leaf1 = new Tree(null, null, RED, "20");
            Tree leaf2 = new Tree(null, null, BLACK, "30");
            Tree leaf3 = new Tree(null, null, RED, "2");
            Tree leaf4 = new Tree(null, null, RED, "100");
            Tree leaf5 = new Tree(null, null, BLACK, "5");
    
            Tree middle1 = new Tree(leaf1, leaf2, RED, "40");
            Tree middle2 = new Tree(middle1, leaf3, BLACK, "200");
            Tree middle3 = new Tree(leaf4, leaf5, RED, "3");
    
            Tree root = new Tree(middle2, middle3, RED, "50");
    
            printTree(root);
    
        }
    
        static void printTree(final Tree t) {
    
            final Map<Tree, Integer> widths = new HashMap<Tree, Integer>();
            final Map<Tree, Integer> offsets = new HashMap<Tree, Integer>();
    
            setWidths(widths, t);
    
            setOffsets(offsets, widths, t, widths.get(t)/2);
    
            final List<Tree> root = new ArrayList<Tree>();
            root.add(t);
            printTree(offsets, root);
    
        }
    
        static int setWidths(final Map<Tree, Integer> widths, final Tree t) {
    
            if(widths.containsKey(t))
                return widths.get(t);
    
            int width = (t.getColor() == BLACK) ? t.getValue().length()
                        : t.getValue().length() + 2;
    
            final Tree left = t.getLeft();
            final Tree right = t.getRight();
    
            if(left != null)
                width += setWidths(widths, left);
            if(right != null)
                width += setWidths(widths, right);
    
            widths.put(t, width);
    
            return width;
    
        }
    
        static void setOffsets(final Map<Tree, Integer> offsets, final Map<Tree, Integer> widths,
                final Tree t, final int offset) {
    
            offsets.put(t, offset);
    
            System.out.println("Parent offset for node " + t.getValue() + ", offset " + offset);
    
            final Tree left = t.getLeft();
            final Tree right = t.getRight();
    
            if(left != null)
                setOffsets(offsets, widths, left, offset - widths.get(left)/2);
            if(right != null)
                setOffsets(offsets, widths, right, offset + widths.get(right)/2);
    
        }
    
        static void printTree(final Map<Tree, Integer> offsets, final List<Tree> trees) {
    
            if(trees.isEmpty())
                return;
    
            final List<Tree> children = new ArrayList<Tree>();
            int linePos = 0;
    
            for(int i = 0; i < trees.size(); ++i) {
    
                final Tree t = trees.get(i);
    
                int offset = offsets.get(t);
                final char[] lead = new char[Math.max(offset - linePos, 0)];
                Arrays.fill(lead, ' ');
    
                System.out.print(new String(lead));
                linePos += Math.max(offset, 0);
    
                if(t.getColor() == RED) {
                    System.out.print(t.getValue());
                    linePos += t.getValue().length();
                } else {
                    System.out.print("<" + t.getValue() + ">");
                    linePos += t.getValue().length() + 2;
                }
    
                if(t.getLeft() != null)
                    children.add(t.getLeft());
                if(t.getRight() != null)
                    children.add(t.getRight());
    
            }
    
            System.out.println("");
    
            printTree(offsets, children);
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't

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.