-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #426 from AishwaryJain07/master
I added Binary Search Tree operation performed
- Loading branch information
Showing
1 changed file
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 << " "; | ||
} | ||
} |
20b0b5f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
open-source-contribution-for-beginners – ./
open-source-contribution-for-beginners-git-master-cyberbuddy.vercel.app
open-source-contribution-for-beginners-cyberbuddy.vercel.app
open-source-contribution-for-beginners.vercel.app
opensource.cyberbuddy.live