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
}

Recommended Answers

All 7 Replies

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

commented: Agree. Not an easy question to me either. +28

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:

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.

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".

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

Hehe goodie :P
Lol and I'm actually unsure what a BTree is :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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.