Skip to content

Commit

Permalink
added recursive DisplayTree(), prep for different traversals
Browse files Browse the repository at this point in the history
  • Loading branch information
dangerrangerous committed May 18, 2017
1 parent 54fb104 commit 5da6967
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 16 deletions.
45 changes: 32 additions & 13 deletions 2-3-4_B_Tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ DataItem* TwoThreeFourNode::RemoveItem()
numItems--;

return temp;
}
} // end RemoveItem()

// Connect child node to this node
void TwoThreeFourNode::ConnectChild(int childNum, TwoThreeFourNode* child)
Expand All @@ -101,16 +101,13 @@ void TwoThreeFourNode::ConnectChild(int childNum, TwoThreeFourNode* child)
}
} // end ConnectChild()

// smelly syntax.
// NOTE: This clears the child array of the node but the disconnected node still
// contains a pointer to parent... which we may want to remove.
TwoThreeFourNode* TwoThreeFourNode::DisconnectChild(int childNum)
{
TwoThreeFourNode* tempNode = childArray[childNum];
childArray[childNum] = nullptr;

return tempNode;
}
} // end DisconnectChild()

TwoThreeFourNode* TwoThreeFourNode::GetChild(int childNum)
{
Expand All @@ -120,25 +117,21 @@ TwoThreeFourNode* TwoThreeFourNode::GetChild(int childNum)
}
else
{
cout << "child is null" << endl;
// return something
return nullptr;
}
}
} // end GetChild()

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


int TwoThreeFourNode::GetNumItems()
{
// testing
cout << "GetNumItems() returned " << numItems << endl;

return numItems;
}
} // end GetNumItems()

int TwoThreeFourNode::FindItem(long key)
{
Expand Down Expand Up @@ -354,4 +347,30 @@ TwoThreeFourNode* Tree234::GetNextChild(TwoThreeFourNode* inNode, long inValue)
return inNode->GetChild(j);
} // end GetNextChild()

void Tree234::DisplayTree()
{
RecursiveDisplayTree(root, 0, 0);
}

void Tree234::RecursiveDisplayTree(TwoThreeFourNode* inNode, int level, int childNumber)
{
cout << "Level: " << level << " " << "Child: " << childNumber << " " << endl;
inNode->DisplayNode();
cout << endl;

// recursive call for each child of this node
int numItems = inNode->GetNumItems();
for (int i = 0; i < numItems + 1; i++)
{
TwoThreeFourNode* nextNode = inNode->GetChild(i);

if (nextNode != nullptr)
{
RecursiveDisplayTree(nextNode, level + 1, i);
}
else
{
return;
}
}
} // end RecursiveDisplayTree()
10 changes: 8 additions & 2 deletions 2-3-4_B_Tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class TwoThreeFourNode
class Tree234
{
public:
// TODO: implement Delete(). Inorder, PreOrder, and
// PostOrder traversals recursively.
// Default constructor
Tree234(void);
// Default destructor
Expand All @@ -72,8 +74,12 @@ class Tree234
void Split(TwoThreeFourNode* inNode);
TwoThreeFourNode* GetNextChild(TwoThreeFourNode* inNode, long inValue);
bool IsEmpty();
void DisplayTree();
void RecursiveDisplayTree(TwoThreeFourNode* inNode, int level, int childNumber);



private:
// double check that new is handled
// I don't really like the syntax for root

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

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

tree234.Insert(6);

tree234.DisplayTree();

return 0;
}
Expand Down
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_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,4 +1,3 @@
 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
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/2-3-4_b_tree_project.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 5da6967

Please sign in to comment.