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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:59:58+00:00 2026-06-17T23:59:58+00:00

I have a B-tree which already exists in the database. Now the following piece

  • 0

I have a B-tree which already exists in the database. Now the following piece of code which is written by some other programmer tries to traverse the B-Tree. However, I am unable to understand as to what is the use of the functions: readInner1(page,slot) and readInnerPage(page,slot). Can someone help me in understanding what the code does by using these 2 functions?

static inline unsigned readUint32Aligned(const unsigned char* data) { return toHost(*reinterpret_cast<const uint32_t*>(data)); }

/// The page remains accessible during the lifetime of the BufferReference object.
class BufferReference
{
   public:
   /// The size of a page
   static const unsigned pageSize = 16384;
   /// A page buffer
   struct PageBuffer { char data[pageSize]; };

   private:
   /// The buffer frame
   const BufferFrame* frame;

   /// No copying of references
   BufferReference(const BufferReference&);
   void operator=(const BufferReference&);

   public:
   /// Constructor
   BufferReference();
   /// Constructor from a request
   BufferReference(const BufferRequest& request);
   /// Destructor
   /// Access the page
   const void* getPage() const;
   /// Get the page number
   unsigned getPageNo() const;
};


Info1(unsigned root,unsigned value1)
{
   // Traverse the B-Tree
#define readInner1(page,slot) readUint32Aligned(page+24+8*(slot))
#define readInnerPage(page,slot) readUint32Aligned(page+24+8*(slot)+4)
   BufferReference ref;
   ref=readShared(root);
   while (true) {
      const unsigned char* page=static_cast<const unsigned char*>(ref.getPage());
      // Inner node?
      if (readUint32Aligned(page+8)==0xFFFFFFFF) {
         // Perform a binary search. The test is more complex as we only have the upper bound for ranges
         unsigned left=0,right=readUint32Aligned(page+16);
         cout<<"\n right="<<right<<"\n";
         while (left!=right) {
            unsigned middle=(left+right)/2;
            printf("\n MIDDLE=%d",middle);
            unsigned middle1=readInner1(page,middle);
            cout<<"\n middle1="<<middle1<<"\t value1="<<value1<<"\n";
            if (value1>middle1) {
               left=middle+1;
               cout<<"\n left="<<left<<"\n";
            } else if ((!middle)||(value1>readInner1(page,middle-1))) {
               ref=readShared(readInnerPage(page,middle));
               break;
            } else {
               right=middle;
               cout<<"\n right="<<right<<"\n";
            }
         }
         // Unsuccessful search?
         if (left==right) {
            ref.reset();
            return false;
         }
      } else {
         // A leaf node
         break;
      }
   }
#undef readInnerPage
#undef readInner1
}

Also it will be great if someone could explain the code?

  • 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-17T23:59:59+00:00Added an answer on June 17, 2026 at 11:59 pm

    The code in general is performing a binary search.

    Thus left and right are bounds to the search, and this halves every iteration until they hold the same value.

    The data seems to be arranged in pages of 16K bytes, and there is probably some header information within those pages, which are probably sorted and thus it is checking these values then readjusting the search.

    If you are allowed to modify the code may I suggest you replace the macros with inline functions.

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

Sidebar

Related Questions

In my source tree have Java code which is automatically generated from XSD files.
I have to build a tree using the following data which comes from a
I have a tree view which is populated already with files/folders from another procedure.
I have SWT tree which has child. Now I want to provide a right
JavaScript For example, I have the following JavaScript code (Dojo 1.6 is already loaded):
I have the following code to plot the minimum spanning tree of a graph
I have a piece of C++ code which reads out the text of a
I have a tree in which i want the onOpen and onClose to be
I have a simple tree which takes the shape below ROOT /\ A B
Say I have a binary tree which contains pointers at each node going to

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.