Skip to content

Commit

Permalink
adding utility functions for delete operation. Haven't tested anythin…
Browse files Browse the repository at this point in the history
…g yet woo
  • Loading branch information
dangerrangerous committed May 22, 2017
1 parent 04259f6 commit dbf6d87
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 11 deletions.
173 changes: 168 additions & 5 deletions 2-3-4_B_Tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,25 @@ int TwoThreeFourNode::FindItem(long key)
return -1;
}

// Returns index of item being searched for
int TwoThreeFourNode::FindIndex(long key)
{
for (int i = 0; i <= ORDER - 1; i++)
{
if (dataItemArray[i] == nullptr)
{
// if dataItemArray[0] is nullptr, we are at an empty leaf
break;
}
else if (dataItemArray[i]->data == key)
{
return i;
}
}

return -1;
}

void TwoThreeFourNode::DisplayNode()
{
for (int i = 0; i < numItems; i++)
Expand Down Expand Up @@ -191,6 +210,8 @@ Tree234::~Tree234(void)
// DestroyTree();
}

// Returns the value if found, otherwise returns -1
// Could cause problems if the key value actually is -1
int Tree234::Find(long key)
{
TwoThreeFourNode* current = root;
Expand Down Expand Up @@ -380,25 +401,167 @@ void Tree234::DeleteItem(long key)
Delete(root, key);
} // end DeleteItem()

/*
DataItem* Tree234::Delete(TwoThreeFourNode* rootPointer, long key)
{
TwoThreeFourNode* current = rootPointer;
bool keyIsInTree;
// Handle 3 cases.
// Case 1: Item is in leaf and there are at least 2 keys in the node
// Find the node and index of the key
// NOTE: Find() is returning child 1 when it should be returning child 2
// GetNextChild() may be returning the wrong child...
// check that key is in the tree
if (Find(key) != -1)
{
keyIsInTree = true;
}
else
{
keyIsInTree = false;
}
// test 3, should return 1, but since 7 returns 1, perhaps 3 will return 2
Find(key);
int index = current->FindIndex(key);
// Key is in this node
if (index < 3 && current->dataItemArray[index]->data == key)
{
}
// temp for test
return 0;
}
*/

void TwoThreeFourNode::Remove(long key)
{
int index = FindIndex(key);

// Key is in this node
if (index < ORDER - 1 && dataItemArray[index]->data == key)
{
if (b_IsLeaf)
{
RemoveFromLeaf(index);
}
else
{
RemoveFromNonLeaf(index);
}
}
// Key is not in this node
else
{
if (b_IsLeaf)
{
cout << "The Key " << key << " is not in the tree" << endl;
return;
}

// stuff goes here

}
}

// -
void TwoThreeFourNode::RemoveFromLeaf(int index)
{
// shift items after index backwards one to remove the item
for (int i = index + 1; i < ORDER - 1; i++)
{
dataItemArray[i - 1] = dataItemArray[i];
}

numItems--;

return;
}

void TwoThreeFourNode::RemoveFromNonLeaf(int index)
{

// cases:
// 2a if elements left child has at least two keys, replace the element with
// its predecessor.
// 2b if both children only have 1 key, merge the right into the left and
// delete key from the left.

// 3 If key is not in internal node, find the key. To ensure all of the nodes
// descended through have at least two keys, do one of the following before
// descending into a node. Eventually case 1 or 2 will be arrived at
// a) if the child node has only 1 key and has a sibling with at least 2 keys,
// move a key from the sibling into the parent.
/*
b) if both the child node and its immediate sibling have only 1 key each,
merge the child node with one of the siblings and move an element down
from the parent into the merged node. This key must be the middle key
in the node. Free the node whose keys were merged into another node
*/



}
// Merge()

// Rotate()
int TwoThreeFourNode::GetPredecessor(int index)
{
TwoThreeFourNode* current = childArray[index];

while (!current->b_IsLeaf())
{
current = current->childArray[current->numItems];

// return the last key of the leaf
return current->dataItemArray[numItems- 1]->data;
}
}

int TwoThreeFourNode::GetSuccessor(int index)
{
TwoThreeFourNode* current = childArray[index + 1];
while (!current->b_IsLeaf())
{
current = current->childArray[0];
}

// return first key of the leaf
return current->dataItemArray[0]->data;
}

// Borrows a key from left sibling and inserts it into childArray[index]
void TwoThreeFourNode::BorrowFromPrevious(int index)
{
TwoThreeFourNode* child = childArray[index];
TwoThreeFourNode* leftSibling = childArray[index - 1];

// Last key from leftSibling goes up to parent and key[index-1] from parent
// is inserted as the first key in child[index]. Sibling loses one key and
// child gains one key.

// Move all keys in childArray[index] one step ahead
for (int i = child->numItems- 1; i >= 0; i--)
{
child->dataItemArray[i + 1] = dataItemArray[i];
}

// Setting child's first key equal to dataItemArray[index-1] from the current node
child->dataItemArray[0] = dataItemArray[index - 1];

// Moving sibling's last child as childArray[index]'s first child
if (!b_IsLeaf())
{
child->childArray[0] = leftSibling->childArray[numItems];
}

// Move key from sibling to the parent
dataItemArray[index - 1] = leftSibling->dataItemArray[leftSibling->numItems- 1];

child->numItems += 1;
leftSibling->numItems -= 1;
}

void TwoThreeFourNode::BorrowFromNext(int index)
{

}
8 changes: 6 additions & 2 deletions 2-3-4_B_Tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,22 @@ class TwoThreeFourNode
DataItem* RemoveItem();
int GetNumItems();
int FindItem(long key);
int FindIndex(long key);
void DisplayNode();
bool b_IsLeaf();
bool b_IsFull();

void Remove(long key);
void RemoveFromLeaf(int index);
void RemoveFromNonLeaf(int index);

DataItem* GetItem(int index);


private:
static const int ORDER = 4;
int numItems;
TwoThreeFourNode* parent;

DataItem* dataItemArray[ORDER - 1];
TwoThreeFourNode* childArray[ORDER];

Expand All @@ -77,11 +81,11 @@ class Tree234
void DisplayTree();
void DeleteItem(long key);


private:

void RecursiveDisplayTree(TwoThreeFourNode* inNode, int level, int childNumber);
DataItem* Delete(TwoThreeFourNode* rootPointer, long key);


TwoThreeFourNode* root;
};
4 changes: 2 additions & 2 deletions 2-3-4_B_Tree_Project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ int main()

tree234.Insert(4);
tree234.Insert(5);
tree234.Insert(7);
tree234.Insert(42);

tree234.Insert(6);

tree234.DisplayTree();

tree234.DeleteItem(7);
tree234.DeleteItem(42);

return 0;
}
Expand Down
Binary file modified Debug/2-3-4_B_Tree.obj
Binary file not shown.
2 changes: 0 additions & 2 deletions Debug/2-3-4_B_Tree_Project.log
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
 2-3-4_B_Tree_Project.cpp
2-3-4_B_Tree.cpp
Generating Code...
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/vc140.idb
Binary file not shown.
Binary file modified Debug/vc140.pdb
Binary file not shown.

0 comments on commit dbf6d87

Please sign in to comment.