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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:45:50+00:00 2026-05-17T00:45:50+00:00

If I am given a certain set of numbers (which I store in a

  • 0

If I am given a certain set of numbers (which I store in a balanced binary search tree for easiness), then I want to answer a query that requires me to inform what is the ith smallest number between [A,B], what would be a fast algorithm to perform that task?

Technically I could traverse the tree from the root searching for A (or a number immediately greater than that if A not present), than backtrack searching for B (or a number smaller than B), and while doing that I could keep a counter for i, to determine when I would be at the ith number. But that does not seem optimal to me.

Can I perform this operation on O(log n), height of the tree that I’m using to store the universal set of numbers?

Thank you

  • 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-17T00:45:51+00:00Added an answer on May 17, 2026 at 12:45 am

    You certainly can do that, if you can keep some information in nodes. To make traversal O(logn), we need for each node to know, how many nodes its right subtree has. (You can maintain this information and still have adding to/removing from tree in O(log(n)) time)

    def search(currentNode, k)
        if k == 0 then
            return currentNode
        else if currentNode.rightBranchSize >= k then
            // we remove 1 from k because currentNode isn't considered anymore
            return search(currentNode.right, k - 1)
        else
            // we decrease k because currentNode and its whole right subtree aren't considered anymore
            return search(currentNode.parent, k - currentNode.rightBranchSize - 1)
        end
    end
    

    The code should be quite self-explanatory.
    We start from currentNode being the first node with number >= A and look for k-th element (k is 0-based).

    1. As schnaader in his comment, it would be easier to just traverse tree if k is
      small.
    2. Most wide-spread libraries (like STL) won’t allow you traverse tree in such way (they don’t provide any kind of Node struct with pointers to left and right subtrees). So, although algorithm is simple, implementing it might be difficult.
    3. It takes only little modification to consider B from your question.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a certain date, I want to set the value of a cell with
Given a certain set of parameters is it possible to stop a set of
I have a certain set of assertions that I give to z3 which are
I'm trying to generate valid trees given certain graph edges (which can be considered
I'm having trouble interpreting a certain question about inserting elements to a binary search
I'm trying to set up a parser which, given a value, can assign it
Possible Duplicate: Creating multiple numbers with certain number of bits set I'm attempting to
Given an app (your app) and a certain action it has, is it possible
Given a table where the first column is seconds past a certain reference point
given to certain circumstances, I'm forced to keep page settings (Javascript-values) in the session

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.