Skip to content

Commit

Permalink
I added Binary Search Tree operation performed
Browse files Browse the repository at this point in the history
  • Loading branch information
Chiragjain7254 committed Dec 26, 2023
1 parent 676317a commit 367cce1
Showing 1 changed file with 119 additions and 0 deletions.
119 changes: 119 additions & 0 deletions MLSA Event-101/C++/theory/Binary_Search_Tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
1. Inserting a Node:

C++
struct Node {
int data;
Node* left;
Node* right;
};

Node* insert(Node* root, int data) {
if (root == nullptr) {
return new Node{data, nullptr, nullptr};
}

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

return root;
}
//------------------------------------------------------------------------------------------------------------
2. Searching for a Node:

C++
bool search(Node* root, int data) {
if (root == nullptr) {
return

false;
}

if (data == root->data) {
return

true;
}

if (data < root->data) {
return search(root->left, data);
} else {
return search(root->right, data);
}
}
//------------------------------------------------------------------------------------------------------------
3. Finding the Minimum Value:

C++
int findMin(Node* root) {
if (root == nullptr) {
throw std::runtime_error("Tree is empty");
}

while (root->left != nullptr) {
root = root->left;
}

return root->data;
}
//------------------------------------------------------------------------------------------------------------
4. Finding the Maximum Value:

C++
int findMax(Node* root) {
if (root == nullptr) {
throw std::runtime_error("Tree is empty");
}

while (root->right != nullptr) {
root = root->right;
}

return root->data;
}
//------------------------------------------------------------------------------------------------------------
5. In-Order Traversal (Left, Root, Right):

C++
void

inorderTraversal(Node* root)

{
if (root != nullptr) {
inorderTraversal(root->left);
std::cout << root->data << " ";
inorderTraversal(root->right);
}
}
6. Pre-Order Traversal (Root, Left, Right):

C++
void

preorderTraversal(Node* root)

{
if (root != nullptr) {
std::cout << root->data << " ";
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}
//------------------------------------------------------------------------------------------------------------
7. Post-Order Traversal (Left, Right, Root):

C++
void

postorderTraversal(Node* root)

{
if (root != nullptr) {
postorderTraversal(root->left);
postorderTraversal(root->right);
std::cout << root->data << " ";
}
}

0 comments on commit 367cce1

Please sign in to comment.