0

Probably a dumb question but, How can you tell if a "split" has occurred all the way up a btree? Would it be that the parent pointer would be NULL? Like I said probably a dumb question. I've got my BTreeNode class working (including the add) but the BTree class I have is not resetting the root

void BTree::add (int newKey)
{
	if (root == 0)
	{
		root = new BTreeNode (0, newKey, 0);
	}
	else // root != 0
	{
		BTreeNode* current = root;
		BTreeNode* newRoot = 0;
		while (current -> isNotLeaf())// current node is not a leaf
		{	
			current = current->findChild (newKey);
			newRoot = current->addKey (newKey);
		}
		if (current -> getChild (5) == 0)// split occurred at topmost level (getChild(5) is the parent pointer location
		{
			root = newRoot;
		}
	} // end else root != 0
}
4
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by pinsickle
4

I'd recommending changing the title of your post. This isn't really that easy of a question (at least to me, haha). You should call it "Detecting splits in a btree"

Dave

Votes + Comments
Agree. Not an easy question to me either.
1

I've noticed every time someone claims they have an easy or quick question, you can count on a minimum of a week or two of struggle to get the problem figured out.

If it was so easy, why do you have to ask? :icon_wink:

1

I've noticed every time someone claims they have an easy or quick question, you can count on a minimum of a week or two of struggle to get the problem figured out.

If it was so easy, why do you have to ask? :icon_wink:

Lol true, I actually it figured out last night (was trying to make it too idea complicated). Just finished debugging it and it seems to work.

1

You might wanna post your solution for others that stumbles upon this thread next week, maybe next month maybe in a few years! :)

At least i hate googling and then being cheered up by finding a post exactly regarding my problem, and the solution is: "I found the solution".

0

You might wanna post your solution for others that stumbles upon this thread next week, maybe next month maybe in a few years! :)

At least i hate googling and then being cheered up by finding a post exactly regarding my problem, and the solution is: "I found the solution".

BTreeNode* newRoot = 0;
		while (current -> isNotLeaf())// current node is not a leaf
		{	
			current = current->findChild (newKey);
		}
			newRoot = current->addKey (newKey);
		
		if (newRoot != 0)// split occurred at topmost level
		{
			root = newRoot;
		}
	} // end else root != 0

Like I said I was overcomplicating it, lol

0

Basicly a Binary Tree but on a file so to try and make of for the slowness of checking data off of a file you store nodes in blocks.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.