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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:38:43+00:00 2026-06-04T15:38:43+00:00

I cannot manage to get my head around redbacktrees, based on this implementation http://pastebin.com/Gg3YbPUg

  • 0

I cannot manage to get my head around redbacktrees, based on this implementation http://pastebin.com/Gg3YbPUg.

/**
 * Insert into the tree.
 * @param item the item to insert.
 * @throws DuplicateItemException if item is already present.
 */
public void insert( AnyType item )
{
    current = parent = grand = header;
    nullNode.element = item;

    while( compare( item, current ) != 0 )
    {
        great = grand; grand = parent; parent = current;
        current = compare( item, current ) < 0 ?
                     current.left : current.right;

            // Check if two red children; fix if so
        if( current.left.color == RED && current.right.color == RED )
             handleReorient( item );
    }

        // Insertion fails if already present
    if( current != nullNode )
        throw new DuplicateItemException( item.toString( ) );
    current = new RedBlackNode<AnyType>( item, nullNode, nullNode );

        // Attach to parent
    if( compare( item, parent ) < 0 )
        parent.left = current;
    else
        parent.right = current;
    handleReorient( item );
}

I’ve just made a simple tree using the numbers 1,14,15,2,11,7,5,8

// Test program
public static void main( String [ ] args )
{
    RedBlackTree<Integer> t = new RedBlackTree<Integer>( );

    t.insert(1);
    t.insert(14);
    t.insert(15);
    t.insert(2);
    t.insert(11);
    t.insert(7);
    t.insert(5);
    t.insert(8);

    t.printTree();
}

And based off the example from http://www.cs.auckland.ac.nz/software/AlgAnim/red_black.html, My Tree should look something like…

RBT

And my console output from the printTree function

/**
 * Print all items.
 */
public void printTree( )
{
    printTree( header.right );
}

/**
 * Internal method to print a subtree in sorted order.
 * @param t the node that roots the tree.
 */
private void printTree( RedBlackNode<AnyType> t )
{
    if( t != nullNode )
    {
        printTree( t.left );
        System.out.println("main element is <" + t.element + "("+t.color+")> left <" + t.left.element +"> right <" + t.right.element + ">" );
        printTree( t.right );
    }
}

Is…

main element is <1(COLOR:RED)> left <8> right <8>
main element is <2(COLOR:BLACK)> left <1> right <5>
main element is <5(COLOR:RED)> left <8> right <8>
main element is <7(COLOR:RED)> left <2> right <14>
main element is <8(COLOR:BLACK)> left <8> right <8>
main element is <11(COLOR:RED)> left <8> right <8>
main element is <14(COLOR:BLACK)> left <11> right <15>
main element is <15(COLOR:RED)> left <8> right <8>

(for reading purposes I have renamed 0 and 1 as COLOR RED AND BLACK)
From what I understand the “header” variable is the root of the tree.

If I was to do something like

 public void function(AnyType x)
 {
    RedBlackNode<AnyType> n = find(x);
    //what is the parent of n?
 }

/**
 * Find an item in the tree.
 * @param x the item to search for.
 * @return the matching item or null if not found.
 */
public RedBlackNode<AnyType> find( AnyType x )
{
    nullNode.element = x;
    current = header.right;

    for( ; ; )
    {
        if( x.compareTo( current.element ) < 0 )
            current = current.left;
        else if( x.compareTo( current.element ) > 0 ) 
            current = current.right;
        else if( current != nullNode )
            return current;
        else
            return null;
    }
}

From the above pastebin implementation of RedBlackTree how do I know what my current parent element is for n?

  • 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-04T15:38:44+00:00Added an answer on June 4, 2026 at 3:38 pm

    If it’s a proper red-black tree, abides by the rules, then the root has to be black, so 1 cannot be the root.

    I’ve build a red-black tree in Java which has a much better print-out if you are interested:

    └── (Black) 8
        ├── (Black) 2
        │   ├── (Red) 1
        │   │   ├── (Black) null
        │   │   └── (Black) null
        │   └── (Red) 5
        │       ├── (Black) null
        │       └── (Black) null
        └── (Red) 15
            ├── (Black) 11
            │   ├── (Black) null
            │   └── (Red) 14
            │       ├── (Black) null
            │       └── (Black) null
            └── (Black) 17
                ├── (Black) null
                └── (Black) null
    

    Here is the code to the red-black tree, check out the RedBlackTreePrinter class to see how I built the console output.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I cannot get word-wrap to work with this example... <html> <head></head> <body> <table style=table-layout:fixed;>
I occasionally get this error when saving to core data. I cannot manage to
I'm trying to wrap my head around NSNotification but can't seem to get it
I cannot seem to get this working. I need South to do migrations for
I want to perform a simple sql query but cannot manage to get it
I'm making an application running multiple webcams/cameras. I cannot manage to detect the number
Cannot dump Stack Overflow XML file into SQL Server 2008. Could you please explain
I cannot get the ReSharper Go to file member navigation to work on .less
I do not manage to get the latitude, longitude from the following json (hold
So I'm having two issues that I cannot seem to get unkinked. I run

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.