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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:52:18+00:00 2026-06-08T01:52:18+00:00

So I was implementing my own binary search tree and noticed that an ugly

  • 0

So I was implementing my own binary search tree and noticed that an ugly if statement appears all too often the way I’m doing it (which possibly isn’t the best way, but that’s not what we’re discussing), based on whether a child of a node is the left or right child, e.g:

if (leftChild)
    parent.setLeft(child.getRight());
else
    parent.setRight(child.getRight());

Then I thought of this:

parent.setChild(childIndex, child.getRight());

Where childIndex is a byte that was determined earlier where leftChild would have been determined.

As you can see this much more concise, but to have it this way I would either have to have an if statement in the setChild method or represent the children as an array of length 2. If we pretend here that this BST requires maximized performance/space efficiency, what kind of trade-off would it be to switch the storage of child node references to a 2-element array, rather than a pair of variables (or even just hide the if statement inside the setChild method).

I know in a real-world situation this might not matter that much, but I’m still interested in which would be the best approach.

  • 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-08T01:52:20+00:00Added an answer on June 8, 2026 at 1:52 am

    As I understand it, you’re asking, which of the following two is more efficient:

    if (condition)
        x = value
    else
        y = value
    

    and

    xyArray[index] = value
    

    First of all any answer is compiler and/or JVM implementation dependent since the JLS doesn’t mention any execution times for any types of statements.

    That being said, I would suspect that the if-statement would be slightly faster since the JVM doesn’t have to compute any array offset and check array bounds.

    In any case, this smells like premature optimization to me. Choose approach based on which one is easiest to read and debug.

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

Sidebar

Related Questions

Is there a native way (preferably without implementing your own method) to check that
I've been implementing my own version of a red-black tree, mostly basing my algorithms
I'm implementing my own double-buffering for a BufferedImage in a JPanel so that I
Where can I get info about implementing my own methods that have the ellipsis
We have an application that is essentially implementing its own messaging queue. When a
I'm implementing an AVL binary tree data structure in C# .NET 2.0 (possibly moving
While implementing my own IFilter I found that most likely some consumers will require
I'm enamoured with the idea of implementing my own single-entry-point gateway that does two
I'm implementing my own named pipe Client/Server class, but I'm getting too much troubles
I'm thinking of designing & implementing my own SQL-equivalent (DDL+DML) that is... a pure

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.