Skip to content

Commit 20b0b5f

Browse files
Merge pull request #426 from AishwaryJain07/master
I added Binary Search Tree operation performed
2 parents 6048a98 + 367cce1 commit 20b0b5f

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
1. Inserting a Node:
2+
3+
C++
4+
struct Node {
5+
int data;
6+
Node* left;
7+
Node* right;
8+
};
9+
10+
Node* insert(Node* root, int data) {
11+
if (root == nullptr) {
12+
return new Node{data, nullptr, nullptr};
13+
}
14+
15+
if (data < root->data) {
16+
root->left = insert(root->left, data);
17+
} else {
18+
root->right = insert(root->right, data);
19+
}
20+
21+
return root;
22+
}
23+
//------------------------------------------------------------------------------------------------------------
24+
2. Searching for a Node:
25+
26+
C++
27+
bool search(Node* root, int data) {
28+
if (root == nullptr) {
29+
return
30+
31+
false;
32+
}
33+
34+
if (data == root->data) {
35+
return
36+
37+
true;
38+
}
39+
40+
if (data < root->data) {
41+
return search(root->left, data);
42+
} else {
43+
return search(root->right, data);
44+
}
45+
}
46+
//------------------------------------------------------------------------------------------------------------
47+
3. Finding the Minimum Value:
48+
49+
C++
50+
int findMin(Node* root) {
51+
if (root == nullptr) {
52+
throw std::runtime_error("Tree is empty");
53+
}
54+
55+
while (root->left != nullptr) {
56+
root = root->left;
57+
}
58+
59+
return root->data;
60+
}
61+
//------------------------------------------------------------------------------------------------------------
62+
4. Finding the Maximum Value:
63+
64+
C++
65+
int findMax(Node* root) {
66+
if (root == nullptr) {
67+
throw std::runtime_error("Tree is empty");
68+
}
69+
70+
while (root->right != nullptr) {
71+
root = root->right;
72+
}
73+
74+
return root->data;
75+
}
76+
//------------------------------------------------------------------------------------------------------------
77+
5. In-Order Traversal (Left, Root, Right):
78+
79+
C++
80+
void
81+
82+
inorderTraversal(Node* root)
83+
84+
{
85+
if (root != nullptr) {
86+
inorderTraversal(root->left);
87+
std::cout << root->data << " ";
88+
inorderTraversal(root->right);
89+
}
90+
}
91+
6. Pre-Order Traversal (Root, Left, Right):
92+
93+
C++
94+
void
95+
96+
preorderTraversal(Node* root)
97+
98+
{
99+
if (root != nullptr) {
100+
std::cout << root->data << " ";
101+
preorderTraversal(root->left);
102+
preorderTraversal(root->right);
103+
}
104+
}
105+
//------------------------------------------------------------------------------------------------------------
106+
7. Post-Order Traversal (Left, Right, Root):
107+
108+
C++
109+
void
110+
111+
postorderTraversal(Node* root)
112+
113+
{
114+
if (root != nullptr) {
115+
postorderTraversal(root->left);
116+
postorderTraversal(root->right);
117+
std::cout << root->data << " ";
118+
}
119+
}

0 commit comments

Comments
 (0)