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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:26:34+00:00 2026-05-31T00:26:34+00:00

I’m working on a problem for class I’m stuck on. It involves adding a

  • 0

I’m working on a problem for class I’m stuck on. It involves adding a methods to the Binary Search Tree found here: http://algs4.cs.princeton.edu/32bst/BST.java.html

I need to develop an iterative ceiling method that finds the ceiling for a given key. It cannot be recursive.

Here is my code so far. I understand the basics of the algorithm I am supposed to implement, but I’m finding actually doing so hard to wrap my head around.

Thanks in advance for any help you might be able to offer.

public Key ceiling_i(Key key)
{
    Node t = root;
    for(int i = 0; i < size(); i++){
        int cmp = key.compareTo(t.key);
        if(cmp == 0) return t.key;
        else if(cmp < 0) t = t.left;
        else if(cmp > 0) t = t.right;

    }
    return null;
}

Edit: The main problem I am having is how to deal with the iterations after the first one. According to my book, “If a given key is greater than the key at the root of a BST,
then the ceiling of key (the largest key in the BST greater
than or equal to key) must be in the right subtree. If key is
less than the key at the root, then the ceiling of key could
be in the left subtree; if not (or if key is equal to the key
at the root), then the key at the root is the ceiling of key.”
I am not sure how to deal with that in the loop.

  • 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-31T00:26:36+00:00Added an answer on May 31, 2026 at 12:26 am

    Your code is a good start. But your for loop does not make sense to me.

    public Key ceiling_i(Key key)
    {
        Node t = root;
        Node t largestVisited = null;
        while(t != null) {
            int cmp = key.compareTo(t.key);
            if(cmp == 0) return t.key;
            else if(cmp < 0) { largestVisited = Min(t, largestVisited); t = t.left; }
            else if(cmp > 0) { t = t.right; largestVisited = Min(t, largestVisited); }
    
        }
        return largestVisited;
    }
    
    Node Min(Node a, Node b) { return the node with the smaller key; }
    

    Tip: You could have derived this code by first writing the recursive solution and noticing that it is tail recursive. Tail recursive functions can trivially made non-recursive by just reusing the already existing local variables. No need to open another stack-frame if you won’t ever use the old one again.

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

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm parsing an XML file, the creators of it stuck in a bunch social
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.