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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:11:02+00:00 2026-06-15T15:11:02+00:00

I need to implement a red-black search tree in java. I thought about creating

  • 0

I need to implement a red-black search tree in java. I thought about creating two class: RBTree and RBNode, where RBNode is nested inside RBTree (this is the demand of the exercise).

RBNode should have the following fields:

key, value, color – no problem with that.

parent, leftChild, rightChild – which are also RBNode type. This I cannot seem to implement, because in order to create an instance of an RBNode, I need an instance of RBTree.

This is my code:

public class RBTree {


public RBNode createNode() {
    return this.new RBNode();
}



    class RBNode{

      private int key;
      private RBTree.RBNode rightChild = new RBTree.RBNode(); \//the problem is here

      public RBNode() {


        this.rightChild=null;
        this.key=-1;



      }  

}

    public static void main(String[] args) {

        RBTree t = new RBTree();
        RBNode rb = t.createNode();
        System.out.println(rb.key);
        System.out.println(rb.rightChild.key);
    }


}

I would appreciate any ideas how to solve this.

Thanks.

  • 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-15T15:11:03+00:00Added an answer on June 15, 2026 at 3:11 pm

    there is no point in making RBNode as a nested class.
    Make RBNode a separate class and store a reference to RBNode in RBTree. It should solve your problem.

    In case, you want to make a nested class, make it static class so that you dont have to instantiate the parent class to instantiate RBNode.

    new RBTree.RBNode();
    

    should work if change class declaration of RBNode to

    static class RBNode{}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to implement 2D collision detection by arc. I have player(black dot) and
I have a recursive insert method that is being implemented in a Red-Black Tree.
I have a keyword implemented with Java and if keyword fails i need to
There is a good function that I need, which is implemented in the Java
I need to implement portable code, but I do not know how to deal
I need an IntervalTree or RangeTree implementation in Java, and am having trouble finding
I need to implement a spatial data structure to store rectangles then be able
I have 2 instances x and y of a same class RBnode . Is
I need help in hovering two HTML tags at once. Please tell how it
I'm wondering about how you would implement an effect similar to this one implemented

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.