Skip to content

Commit

Permalink
debuggered, Insert(), Split(), and GetNextChild() seem fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
dangerrangerous committed May 17, 2017
1 parent 8531fc3 commit 54fb104
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 44 deletions.
110 changes: 68 additions & 42 deletions 2-3-4_B_Tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ bool TwoThreeFourNode::b_IsLeaf()
return (childArray[0] == nullptr) ? true : false;
}

DataItem TwoThreeFourNode::GetItem(int index)
DataItem* TwoThreeFourNode::GetItem(int index)
{
return *dataItemArray[index];
return dataItemArray[index];
}

bool TwoThreeFourNode::b_IsFull()
Expand All @@ -189,7 +189,7 @@ bool TwoThreeFourNode::b_IsFull()

Tree234::Tree234(void)
{
root = new TwoThreeFourNode;
root = NULL;
}

Tree234::~Tree234(void)
Expand Down Expand Up @@ -225,42 +225,63 @@ int Tree234::Find(long key)

void Tree234::Insert(long dataValue)
{
TwoThreeFourNode* current = root;
DataItem* tempDataItemPtr = new DataItem;
tempDataItemPtr->data = dataValue;
// DataItem* tempDataItemPtr = &tempDataItem;
TwoThreeFourNode* current;
DataItem* newDataItem;
newDataItem = new DataItem;
newDataItem->data = dataValue;

// in case root is empty,
TwoThreeFourNode* newNode;
newNode = new TwoThreeFourNode;

while (true)
bool iterate = true;

if (IsEmpty())
{
// if node is full, split it
if (current->b_IsFull())
{
// split
Split(current);
// back up one level
current = current->GetParent();
// search
current = GetNextChild(current, dataValue);
}
// else if node is leaf, go insert
else if (current->b_IsLeaf())
{
break;
}
else
root = newNode;
root->InsertItem(newDataItem);
}
else
{
current = root;

while (iterate)
{
// else node is not full and not a leaf
// go to next level
current = GetNextChild(current, dataValue);
}
} // end while

current->InsertItem(tempDataItemPtr);

// if node is full, split it
if (current->b_IsFull())
{
// split
Split(current);
// back up one level
current = current->GetParent();
// search
current = GetNextChild(current, dataValue);
// NOTE: current gets nuked upon exiting this loop
}
// else if node is leaf, go insert
else if (current->b_IsLeaf())
{
iterate = false;
// break;
}
else
{
// else node is not full and not a leaf
// go to next level
current = GetNextChild(current, dataValue);
}
} // end while

current->InsertItem(newDataItem);
} // end else
// delete tempDataItemPtr;
} // end Insert()

bool Tree234::IsEmpty()
{
return (root == NULL);
} // end IsEmpty()

void Tree234::Split(TwoThreeFourNode* inNode)
{
// assume node is full
Expand All @@ -280,25 +301,26 @@ void Tree234::Split(TwoThreeFourNode* inNode)
child3 = inNode->DisconnectChild(3);

// make new node
TwoThreeFourNode newRight;
TwoThreeFourNode* newRightPtr = &newRight;
//TwoThreeFourNode newRight;
TwoThreeFourNode* newRightPtr = new TwoThreeFourNode;

if (inNode == root)
{
// create new node and have root point to it
root = new TwoThreeFourNode;
// inNode->parent = root
parent = root;
root->ConnectChild(0, inNode);
}
else
{
parent = inNode->GetParent();
}

// put itemB into parent of node being split
itemIndex = parent->InsertItem(itemB);
int numParentItems = parent->GetNumItems();

for (int i = numParentItems - 1; i > itemIndex; i++)
for (int i = numParentItems - 1; i > itemIndex; i--)
{
TwoThreeFourNode* temp = parent->DisconnectChild(i);
parent->ConnectChild(i + 1, temp);
Expand All @@ -308,24 +330,28 @@ void Tree234::Split(TwoThreeFourNode* inNode)
parent->ConnectChild(itemIndex + 1, newRightPtr);

// handle new right
newRight.InsertItem(itemC);
newRight.ConnectChild(0, child2);
newRight.ConnectChild(1, child3);
newRightPtr->InsertItem(itemC);
newRightPtr->ConnectChild(0, child2);
newRightPtr->ConnectChild(1, child3);
} // end Split()


TwoThreeFourNode* Tree234::GetNextChild(TwoThreeFourNode* inNode, long inValue)
{
// assumes node is not empty and not a leaf
int numItems = inNode->GetNumItems();
int j;

for (int j = 0; j < numItems; j++)
for (j = 0; j < numItems; j++)
{
if (inValue < inNode->GetItem(j).data)
if (inValue < inNode->GetItem(j)->data)
{
// less than so return left child
return inNode->GetChild(j);
}
}
} // end for
// greater so return right child
return inNode->GetChild(j);
} // end GetNextChild()


3 changes: 2 additions & 1 deletion 2-3-4_B_Tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class TwoThreeFourNode
bool b_IsLeaf();
bool b_IsFull();

DataItem GetItem(int index);
DataItem* GetItem(int index);


private:
Expand All @@ -71,6 +71,7 @@ class Tree234
void Insert(long dataValue);
void Split(TwoThreeFourNode* inNode);
TwoThreeFourNode* GetNextChild(TwoThreeFourNode* inNode, long inValue);
bool IsEmpty();
private:
// double check that new is handled
// I don't really like the syntax for root
Expand Down
Binary file modified Debug/2-3-4_B_Tree.obj
Binary file not shown.
1 change: 0 additions & 1 deletion Debug/2-3-4_B_Tree_Project.log
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
 2-3-4_B_Tree.cpp
c:\users\brian\onedrive\documents\visual studio 2015\projects\2-3-4_b_tree_project\2-3-4_b_tree_project\2-3-4_b_tree.cpp(126): warning C4715: 'TwoThreeFourNode::GetChild': not all control paths return a value
c:\users\brian\onedrive\documents\visual studio 2015\projects\2-3-4_b_tree_project\2-3-4_b_tree_project\2-3-4_b_tree.cpp(329): warning C4715: 'Tree234::GetNextChild': not all control paths return a value
2-3-4_B_Tree_Project.vcxproj -> C:\Users\Brian\onedrive\documents\visual studio 2015\Projects\2-3-4_B_Tree_Project\Debug\2-3-4_B_Tree_Project.exe
2-3-4_B_Tree_Project.vcxproj -> C:\Users\Brian\onedrive\documents\visual studio 2015\Projects\2-3-4_B_Tree_Project\Debug\2-3-4_B_Tree_Project.pdb (Full PDB)
Binary file modified Debug/2-3-4_B_Tree_Project.obj
Binary file not shown.
Binary file modified Debug/2-3-4_b_tree.obj.enc
Binary file not shown.
Binary file modified Debug/vc140.idb
Binary file not shown.
Binary file modified Debug/vc140.pdb
Binary file not shown.

0 comments on commit 54fb104

Please sign in to comment.