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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:47:11+00:00 2026-05-27T06:47:11+00:00

I am having trouble figuring out what the code segment below is doing. It

  • 0

I am having trouble figuring out what the code segment below is doing. It is taken from the book Realistic Image Synthesis Using Photon Mapping by Henrik Wann Jensen. I think what it’s trying to do (or what I think it should be trying to do given its place in the code) is compute the median index in an array between some start and end index.

// inputs are an array, start and end indices that define a subset of the array
int median = 1;
while((4*median) <= (end-start+1))
  median += median;

if ((3*median) <= (end - start + 1)) {
   median += median;
   median += start - 1;
} else
  median = end - median + 1

For more context, the code comes from a section that builds a kd-tree data structure given a list of 3D points. At each recursive step of building the kd-tree the median point (with respect to some dimension) is selected to be the root of a new kd-tree.

I think this code is supposed to compute the median index between some start and end index but if I’m correct then I can’t figure out why this median index is computed in such a weird way.

Any help or insight would be appreciated, thanks!

Edit: Thanks to Vaughn Cato I now see that it is necessary to compute the median index in this way. Originally I was confused as to why you couldn’t just do (end – start)/2 + start. The goal of this code is to take a list of points and convert it into a full, balanced kd-tree that could be stored in a heap like data structure (the whole binary tree in an array). Computing the median index in the naive way won’t necessarily get you a tree that you can flatten into array.

Now I’m confused as to how somebody came up with that. Can anybody either explain or point me in the direction of a derrivation?

  • 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-27T06:47:12+00:00Added an answer on May 27, 2026 at 6:47 am

    The resulting median value is always a power of two away from the start or end. Assuming the median value is used to partition the tree, this would lead to branches that are powers of two in size, meaning each node would have either two or zero children. This could provide some efficiency in traversing the leaves of the tree.

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

Sidebar

Related Questions

I'm having trouble figuring out how to access a cookie from a compiled object.
I'm having trouble figuring out how to exclude /public/bin from this rewrite rule RewriteCond
I am having trouble figuring out the if statement code for changing the color
I am having trouble figuring out the reason why my .c code is having
I'm having trouble figuring out my last section of code for a Dynamic Coin
I'm just getting into using prototypal JavaScript and I'm having trouble figuring out how
I'm having trouble figuring out how could I use my imagebutton (see below) in
I'm a beginner with Haskell and am having trouble figuring out some code. What
I'm having trouble figuring out what my Flex Project is expecting from my PHP
I am using an ArrayList as my inventory. I am having trouble figuring out

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.