Skip to content

Commit

Permalink
Merge pull request #345 from keithf40/Open-source-Contribution
Browse files Browse the repository at this point in the history
Added Binary Tree to C++
  • Loading branch information
kelvins committed Jul 2, 2024
2 parents c67a20b + 990e409 commit 227ae66
Showing 1 changed file with 126 additions and 0 deletions.
126 changes: 126 additions & 0 deletions src/cpp/BinaryTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#include <iostream>

using namespace std;

// Create a class for the BinaryTree
class BinaryTree
{
// Create a struct for the TreeNode
struct TreeNode
{
// Variables for the TreeNode
int data;
TreeNode* left;
TreeNode* right;

// Constructor for the TreeNode
TreeNode(int value) : data(value), left(nullptr), right(nullptr) {}
};

// Private Variables and Functions
private:
TreeNode* root;

//Insert Function
TreeNode* insert(TreeNode* root, int value)
{
if (root == nullptr)
return new TreeNode(value);

if (value < root->data)
root->left = insert(root->left, value);
else
root->right = insert(root->right, value);

return root;
}

// Print Inorder Function
void printInorder(TreeNode* head)
{
if (head != nullptr)
{
printInorder(head->left);
cout << head->data << " ";
printInorder(head->right);
}
}

// Print Preorder Function
void printPreorder(TreeNode* head)
{
if (head != nullptr)
{
cout << head->data << " ";
printPreorder(head->left);
printPreorder(head->right);
}
}

// Print Postorder Function
void printPostorder(TreeNode* head)
{
if (head != nullptr)
{
printPostorder(head->left);
printPostorder(head->right);
cout << head->data << " ";
}
}

// Public Functions
public:
// Constructor
BinaryTree() : root(nullptr) {}

// Insert Function
void insert(int value)
{
root = insert(root, value);
}

// Print Inorder Function
void printInorder()
{
printInorder(root);
cout << endl;
}

// Print Preorder Function
void printPreorder()
{
printPreorder(root);
cout << endl;
}

// Print Postorder Function
void printPostorder()
{
printPostorder(root);
cout << endl;
}
};

int main()
{
// Create tree
BinaryTree binaryTree;

binaryTree.insert(10);
binaryTree.insert(6);
binaryTree.insert(15);
binaryTree.insert(3);
binaryTree.insert(8);
binaryTree.insert(20);

cout << "InOrder: ";
binaryTree.printInorder();

cout << "PreOrder: ";
binaryTree.printPreorder();

cout << "PostOrder: ";
binaryTree.printPostorder();

return 0;
}

0 comments on commit 227ae66

Please sign in to comment.