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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:08:33+00:00 2026-06-15T11:08:33+00:00

I am trying to implement an insert method for a BTree. My BTree class

  • 0

I am trying to implement an insert method for a BTree. My BTree class holds a pointer to a root BNode. The insert method has both an internal and external method. The internal one doesnt yet recurse, but I am having trouble having the root passed in hold its value for the second insertion. I think it might be a scoping issue, but I’m not sure exactly what it is. In main I tried to insert 2 values to test the method, but it seems that in the second insertion it still thinks that root == null and overwrites the first key value. Does anyone who why this might occur?

Here are my functions:

    template<typename T, int M>
    class BTree{
    private:


        returnStatus _insert( BNode<T, M>* r, const T& val, BNode<T, M>*& dptr, T& dkv );
        returnStatus shuffle_insert( BNode<T, M>* r, const T& val );
        returnStatus split_insert( BNode<T, M>* r, const T& val, BNode<T, M>*& dptr, T& dkv );
        bool isNodeLeaf( BNode<T, M>* node );
        int _getNodeIndex( BNode<T, M>* r, const T& val );

        returnStatus _find( BNode<T, M>* r, const T& val );
        returnStatus _remove( BNode<T, M>* r, const T& val, BNode<T, M>* & dptr, T& dkv );
        void _traverse( BNode<T, M>* r );

    public:
        BNode<T, M>* root;
        int size;

        BTree();
        void insert( const T& val );
        returnStatus find( const T& val );
        void remove( const T& val );
        void traverse();
        ~BTree();

    };
    template<typename T, int M>
    BTree<T, M>::BTree()
    {
    root = NULL;
    size = 0;
        }
        template<typename T, int M>
    returnStatus BTree<T, M>::_insert( BNode<T, M>* r, const T& val, BNode<T, M>*& dptr, T& dkv )
    {
    if( r == NULL ){
        dptr = NULL;
        dkv = val;
        r = new BNode<T, M>;
        r->keys[0] = val;
        size++; 
        r->keyCount++;

        return unsuccessful;
    }else{
        returnStatus contains = find(val);

        if( contains == duplicate ){
            return duplicate;
        }else{

            if( r->keyCount < M-1 ){
                returnStatus hold = shuffle_insert(r, val);
                return hold;
            }else{
                cout<<"need to split"<<endl;
                //split_insert
            }
        }
    }
}
template<typename T, int M>
returnStatus BTree<T, M>::shuffle_insert( BNode<T, M>* r, const T& val )
{
    if(r->keyCount == M-1 ) return unsuccessful;

    int index = _getNodeIndex(r, val);

    for(int i = r->keyCount; i>index; i--){
        r->keys[i+1] = r->keys[i];
    }
    r->keys[index] = val;
    r->keyCount++;
}
template<typename T, int M>
returnStatus BTree<T, M>::split_insert( BNode<T, M>* r, const T& val, BNode<T, M>*& dptr, T& dkv )
{



}
template<typename T, int M>
bool BTree<T, M>::isNodeLeaf( BNode<T, M>* node )
{
    for( int i = 0; i< M-1; i++ ){
        if( node->ptr[i] != NULL ){
            return false;
        }
    }
    return true;
}
template<typename T, int M>
returnStatus BTree<T, M>::_find( BNode<T, M>* r, const T& val )
{
    for( int i = 0; i<M; i++ ){
        if( r->ptr[i] != NULL ){
            _find( r->ptr[i], val);
        }
    }
    for( int i =0; i<r->keyCount; i++ ){
        if( r->keys[i] == val ) return duplicate;
    }
    return unsuccessful;
}
template<typename T, int M>
int BTree<T, M>::_getNodeIndex( BNode<T, M>* r, const T& val )
{
    for( int i = 0; i < r->keyCount; i++){
        if( val < r->keys[i] ){
            return i;
        }
    }
    return -1;
}

    template<typename T, int M>
void BTree<T, M>::insert( const T& val )
{
    int set = 0;
    BNode<T, M>* myNode = new BNode<T, M>();                        //May not be correct way to instantiate Reference to pointer
    returnStatus status = _insert( root, val, myNode, set);

    if( status == successful ){
        return;
    }else if( status == unsuccessful ){


    }else if( status == duplicate ){
        cout<<val<<" has already been inserted."<<endl;
    }

}
template<typename T, int M>
returnStatus BTree<T, M>::find( const T& val )
{
    returnStatus stat = _find(root, val);
    return stat;
}
template<typename T, int M>
void BTree<T, M>::remove( const T& val )
{

}
template<typename T, int M>
void BTree<T, M>::traverse()
{
    _traverse(root);
}
template<typename T, int M>
BTree<T, M>::~BTree()
{



}
  • 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-15T11:08:34+00:00Added an answer on June 15, 2026 at 11:08 am

    I think the following method to insert into a B-Tree will help you to clarify your doubts related to the insertion into the B-Tree.

    public void insert (Comparable object)
    {
    if(isEmpty ())
    {
        if(parent==NULL)
        {
            attachSubtree (0, new BTree(getM ()));
            key[1]=object;
            attachSubtree (1, new BTree(getM ()));
            count=1;
        }
        else
            parent.insertPair(object, new BTree (getM ()));
    }
    
    else
    {
        int index = findIndex (object);
        if(index !=0 && object.isEQ (key [index])) throw new IllegalArgumentException ("duplicate key");
            subtree [index].insert(object);
    }
    }
    

    This is extracted from the book
    “Data Structures and Algorithms with Object-Oriented Design Patterns in Java”.

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

Sidebar

Related Questions

I am trying to implement a if exists, update, otherwise, insert data access method
I'm trying to implement a binary insert method. Currently this method is very simple,
I'm new to ASP.NET, and I'm trying to implement a custom ObjectDataSource Insert method.
I'm trying to create a simple class THistory that has one procedure that takes
I am trying to learn LINQ to SQL. I have successfully implemented insert method
I'm trying to implement AVL. Here's my insert, balance_tree, check_bf (balance factor), and single
Trying to implement LoaderManager + CursorLoader. In onFinish method adapter should swap its cursor
I'm trying to implement swiftmailer into this mailing system. my client has about 300k
I am trying to implement a Linked List Sequence in Java however my method
While trying to learn c++, I tried to implement class representing very basic trie.

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.