Skip to content

Commit

Permalink
rest
Browse files Browse the repository at this point in the history
  • Loading branch information
dangerrangerous committed Jun 1, 2017
1 parent a202805 commit 24dba80
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 51 deletions.
62 changes: 27 additions & 35 deletions 2-3-4_B_Tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
// Data item A remains where it is.
// The two rightmost children of the node being split are disconnected from it and connected to the new right-hand node.

// Implement a 2-3-4 B Tree class. Implement an Insert and Delete operators, as well as any others that are necessary.
// See the LaFore text. Also implement Inorder, Preorder, and Postorder tree traversal operators recursively.
// When the traversal operator visits a node, it should output all node key values.

#include "stdafx.h"
#include "2-3-4_B_Tree.h"
#include <iostream>
Expand Down Expand Up @@ -68,9 +64,9 @@ void DataItem::DisplayItem()
return;
}
}
// end DataItem -------------------/
// end DataItem =========================

// TwoThreeFourNode -------/
// TwoThreeFourNode =====================
TwoThreeFourNode::TwoThreeFourNode(void)
{
dataItemArray[ORDER - 1] = nullptr;
Expand Down Expand Up @@ -116,9 +112,6 @@ int TwoThreeFourNode::InsertItem(DataItem* inItem)
// all items have been shifted, insert inItem
dataItemArray[0] = inItem;

// delete the dataItem pointer
// delete inItem;

return 0;
} // end InsertItem()

Expand All @@ -136,7 +129,6 @@ DataItem* TwoThreeFourNode::RemoveItem()
// Connect child node to this node
void TwoThreeFourNode::ConnectChild(int childNum, TwoThreeFourNode* child)
{
// test this
childArray[childNum] = child;

if (child != nullptr)
Expand Down Expand Up @@ -167,7 +159,6 @@ TwoThreeFourNode* TwoThreeFourNode::GetChild(int childNum)

TwoThreeFourNode* TwoThreeFourNode::GetParent()
{
// parent isn't currently pointing to anything need to fix.
return parent;
} // end GetParent()

Expand All @@ -177,6 +168,7 @@ int TwoThreeFourNode::GetNumItems()
return numItems;
} // end GetNumItems()

// Utility function to be used by other functions.
int TwoThreeFourNode::FindItem(long key)
{
for (int i = 0; i <= ORDER - 1; i++)
Expand All @@ -194,7 +186,7 @@ int TwoThreeFourNode::FindItem(long key)
}

return -1;
}
} // end FindItem()

// Returns index of item being searched for
int TwoThreeFourNode::FindIndex(long key)
Expand All @@ -213,7 +205,7 @@ int TwoThreeFourNode::FindIndex(long key)
}

return -1;
}
} // end FindIndex()

void TwoThreeFourNode::DisplayNode()
{
Expand All @@ -226,7 +218,7 @@ void TwoThreeFourNode::DisplayNode()
}
}

// NOTE: this can potentially return true when a node isn't connected...
// NOTE: this can potentially return true when a node isn't connected.
bool TwoThreeFourNode::b_IsLeaf()
{
return (childArray[0] == nullptr) ? true : false;
Expand All @@ -241,6 +233,7 @@ bool TwoThreeFourNode::b_IsFull()
{
return (numItems == ORDER - 1) ? true : false;
}
// end TwoThreeFourNode ==========================

// ===============================================
// Tree234
Expand Down Expand Up @@ -364,7 +357,6 @@ void Tree234::Split(TwoThreeFourNode* inNode)
child3 = inNode->DisconnectChild(3);

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

if (inNode == root)
Expand Down Expand Up @@ -424,7 +416,7 @@ void Tree234::DisplayPreOrder()

RecursivePreOrderTraversal(root, 0, 0);
cout << endl;
}
} // end DisplayPreOrder()

void Tree234::DisplayInOrder()
{
Expand All @@ -433,7 +425,7 @@ void Tree234::DisplayInOrder()

RecursiveInOrderTraversal(root, 0, 0);
cout << endl;
}
} // end DisplayInOrder()

void Tree234::DisplayPostOrder()
{
Expand All @@ -442,9 +434,10 @@ void Tree234::DisplayPostOrder()

RecursivePostOrderTraversal(root, 0, 0);
cout << endl;
}
} // end DisplayPostOrder()

// Recursive PreOrder Traversal
// Prints Level number and child numbers.
void Tree234::RecursivePreOrderTraversal(TwoThreeFourNode* inNode, int level, int childNumber)
{
cout << "Level: " << level << " " << "Child: " << childNumber << " " << endl;
Expand All @@ -457,7 +450,6 @@ void Tree234::RecursivePreOrderTraversal(TwoThreeFourNode* inNode, int level, in
{
// if these pointers need to be deleted just let me know.
TwoThreeFourNode* nextNode = inNode->GetChild(i);

if (nextNode != nullptr)
{
RecursivePreOrderTraversal(nextNode, level + 1, i);
Expand All @@ -469,6 +461,8 @@ void Tree234::RecursivePreOrderTraversal(TwoThreeFourNode* inNode, int level, in
}
} // end RecursivePreOrderTraversal()

// Prints all values of the nodes as they are visited via inorder traversal
// Ignores level number and child number.
void Tree234::RecursiveInOrderTraversal(TwoThreeFourNode* inNode, int level, int childNumber)
{

Expand All @@ -485,6 +479,8 @@ void Tree234::RecursiveInOrderTraversal(TwoThreeFourNode* inNode, int level, int

} // end RecursiveInOrderTraversal()

// Prints all values of the nodes as they are visited via postorder traversal.
// Ignores level number and child number.
void Tree234::RecursivePostOrderTraversal(TwoThreeFourNode* inNode, int level, int childNumber)
{
if (inNode == nullptr)
Expand All @@ -508,7 +504,7 @@ int TwoThreeFourNode::FindKey(int key)
index++;
}
return index;
}
} // end FindKey()

void TwoThreeFourNode::Remove(long key)
{
Expand Down Expand Up @@ -559,9 +555,8 @@ void TwoThreeFourNode::Remove(long key)

return;
}
}
} // end Remove()

// -
void TwoThreeFourNode::RemoveFromLeaf(int index)
{
delete(dataItemArray[index]);
Expand All @@ -575,11 +570,10 @@ void TwoThreeFourNode::RemoveFromLeaf(int index)
numItems--;

return;
}
} // end RemoveFromLeaf()

void TwoThreeFourNode::RemoveFromNonLeaf(int index)
{

// cases:
// 2a if elements left child has at least two keys, replace the element with
// its predecessor.
Expand Down Expand Up @@ -629,7 +623,7 @@ void TwoThreeFourNode::RemoveFromNonLeaf(int index)
}

return;
}
} // end RemoveFromNonLeaf()

int TwoThreeFourNode::GetPredecessor(int index)
{
Expand All @@ -642,7 +636,7 @@ int TwoThreeFourNode::GetPredecessor(int index)

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

int TwoThreeFourNode::GetSuccessor(int index)
{
Expand All @@ -654,7 +648,7 @@ int TwoThreeFourNode::GetSuccessor(int index)

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

// Borrows a key from left sibling and inserts it into childArray[index]
void TwoThreeFourNode::BorrowFromPrevious(int index)
Expand Down Expand Up @@ -686,7 +680,7 @@ void TwoThreeFourNode::BorrowFromPrevious(int index)

child->numItems += 1;
leftSibling->numItems -= 1;
}
} // end BorrowFromPrevious()

void TwoThreeFourNode::BorrowFromNext(int index)
{
Expand Down Expand Up @@ -714,7 +708,6 @@ void TwoThreeFourNode::BorrowFromNext(int index)
// remove the last key in the array so there are no duplicates
rightSibling->RemoveItem();


// Move the child pointers one step behind
if (!rightSibling->b_IsLeaf())
{
Expand All @@ -728,7 +721,7 @@ void TwoThreeFourNode::BorrowFromNext(int index)
child->numItems++;

return;
}
} // end BorrowFromNext()

// Merge childArray[index] with childArray[index+1], then free
// childArray[index+1] after merging
Expand All @@ -745,6 +738,7 @@ void TwoThreeFourNode::Merge(int index)
// position 0. Could be unnecessary but it's safer, still need to test more cases.
// Since n will not be larger than 3 this nested for loop is acceptable. However, if
// this were a B tree like the one CouchDB uses, n could upwards of 1 Million.
// Room for optimization.
for (int i = 0; i < ORDER - 1; i++)
{
if (rightSibling->dataItemArray[i] != nullptr)
Expand Down Expand Up @@ -782,16 +776,14 @@ void TwoThreeFourNode::Merge(int index)
childArray[i] = nullptr;
}



child->numItems += rightSibling->numItems+1;
numItems--;

rightSibling = nullptr;
delete(rightSibling);

return;
}
} // end Merge()

// Fills child with keys by borrowing from previous or next
void TwoThreeFourNode::Fill(int index)
Expand Down Expand Up @@ -819,7 +811,7 @@ void TwoThreeFourNode::Fill(int index)
}

return;
}
} // end Fill()

void Tree234::RemoveFromTree(long key)
{
Expand Down Expand Up @@ -851,4 +843,4 @@ void Tree234::RemoveFromTree(long key)
}

return;
}
} // end RemoveFromTree()
8 changes: 0 additions & 8 deletions 2-3-4_B_Tree_Project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ int main()
menu.ProcessCommand(tree234);
} while (menu.Continue());

// DisplayTree() is a PreOrder traversal
/*
tree234.DisplayPreOrder();
tree234.DisplayInOrder();
tree234.DisplayPostOrder();
*/


return 0;
}

Binary file modified Debug/2-3-4_B_.E48060A2.tlog/CL.write.1.tlog
Binary file not shown.
Binary file modified Debug/2-3-4_B_.E48060A2.tlog/link.read.1.tlog
Binary file not shown.
Binary file modified Debug/2-3-4_B_Tree.obj
Binary file not shown.
2 changes: 1 addition & 1 deletion Debug/2-3-4_B_Tree_Project.log
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppBuild.targets(392,5): warning MSB8028: The intermediate directory (Debug\) contains files shared from another project (2-3-4_B_Tree_Project.vcxproj). This can lead to incorrect clean and rebuild behavior.
Menu.cpp
2-3-4_B_Tree.cpp
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/Menu.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.
19 changes: 12 additions & 7 deletions Menu.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Menu.cpp
// Brian Keppinger

#include "stdafx.h"
#include "Menu.h"
#include "2-3-4_B_Tree.h"
Expand Down Expand Up @@ -70,11 +73,13 @@ void Menu::QueryUser()
break;
case 7: userMenuSelection = Postorder;
break;
/*
TODO:
case 8: userMenuSelection = Height;
break;
case 9: userMenuSelection = NodeCount;
break;

*/
default: userMenuSelection = Quit;
}
cout << endl;
Expand Down Expand Up @@ -131,12 +136,12 @@ void Menu::ProcessCommand(Tree234& twoThreeFourTree)
break;
/*
TODO:
case Height:
cout << "Assessing the height of the tree:" << endl;
cout << "The tree is " << binaryTree.TreeHeight() << " levels tall." << endl;
cout << endl;
break;
case Height:
cout << "Assessing the height of the tree:" << endl;
cout << "The tree is " << binaryTree.TreeHeight() << " levels tall." << endl;
cout << endl;
break;
case Nodecount:
cout << "There are " << twoThreeFourTree.NodeCount() << " nodes in the tree." << endl;
break;
Expand Down
3 changes: 3 additions & 0 deletions Menu.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Menu.h
// Brian Keppinger

#pragma once

#include "2-3-4_B_Tree.h"
Expand Down

0 comments on commit 24dba80

Please sign in to comment.