Skip to content

Commit 90a54cb

Browse files
authored
GH-250: Binary Search Tree in C++ (#261)
1 parent fb550bb commit 90a54cb

File tree

5 files changed

+548
-2
lines changed

5 files changed

+548
-2
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# Binaray Search Tree
2+
3+
A `binary search tree (BST)` is a type of binary tree data structure in which each node has at most two child nodes, usually referred to as left and right children. In a binary search tree, the value of each node is greater than or equal to the values of all nodes in its left subtree and less than or equal to the values of all nodes in its right subtree.
4+
5+
## Binary Search Tree Implementation in C++
6+
7+
Binary Search TreeNode Struct:
8+
9+
10+
```c++
11+
#include <iostream>
12+
#include <queue>
13+
14+
using namespace std;
15+
16+
struct TreeNode {
17+
int val;
18+
TreeNode* left;
19+
TreeNode* right;
20+
21+
TreeNode(): val(0), left(nullptr), right(nullptr) {}
22+
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
23+
TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
24+
}
25+
```
26+
27+
28+
```c++
29+
TreeNode* root = new TreeNode(10);
30+
```
31+
32+
**Print**: Print the tree in order
33+
34+
35+
```c++
36+
void print(TreeNode* root){
37+
queue<TreeNode*> q;
38+
q.push(root);
39+
int count;
40+
while(!q.empty()){
41+
count = q.size();
42+
while(count > 0){
43+
TreeNode* node = q.front();
44+
q.pop();
45+
cout << node->val << " ";
46+
if(node->left) q.push(node->left);
47+
if(node->right) q.push(node->right);
48+
count--;
49+
}
50+
cout << endl;
51+
}
52+
}
53+
```
54+
55+
**Insertion**: Insert a new node into the tree
56+
57+
58+
```c++
59+
TreeNode* insertIntoBST(TreeNode* root, int val) {
60+
if (root == nullptr) {
61+
return new TreeNode(val);
62+
}
63+
64+
if (val < root->val) {
65+
root->left = insertIntoBST(root->left, val);
66+
} else if (val > root->val){
67+
root->right = insertIntoBST(root->right, val);
68+
}
69+
70+
return root;
71+
}
72+
```
73+
74+
75+
```c++
76+
root = insertIntoBST(root, 2);
77+
root = insertIntoBST(root, 1);
78+
root = insertIntoBST(root, 5);
79+
root = insertIntoBST(root, 15);
80+
root = insertIntoBST(root, 11);
81+
root = insertIntoBST(root, 20);
82+
print(root)
83+
```
84+
85+
10
86+
2 15
87+
1 5 11 20
88+
89+
90+
**Deletion**: Delete a node from the tree
91+
92+
93+
```c++
94+
TreeNode* deleteNode(TreeNode* root, int key) {
95+
if (root == nullptr) return root;
96+
if (key < root->val)
97+
root->left = deleteNode(root->left, key);
98+
else if (key > root->val)
99+
root->right = deleteNode(root->right, key);
100+
else {
101+
if (!root->left && !root->right) return nullptr;
102+
if (!root->left)
103+
return root->right;
104+
else if (!root->right)
105+
return root->left;
106+
else {
107+
TreeNode* temp = root->right;
108+
while (temp->left) temp = temp->left;
109+
temp->left = root->left;
110+
return root->right;
111+
}
112+
}
113+
return root;
114+
}
115+
```
116+
117+
118+
```c++
119+
root = deleteNode(root, 5);
120+
print(root)
121+
```
122+
123+
10
124+
2 15
125+
1 11 20
126+
127+
128+
129+
```c++
130+
root = deleteNode(root, 10);
131+
print(root)
132+
```
133+
134+
15
135+
11 20
136+
2
137+
1
138+
139+
140+
## 🔗 Further Reading
141+
142+
* [Binary Search Tree](https://en.wikipedia.org/wiki/Binary_search_tree)
143+
* [An Introduction To Binary Search And Red-black Trees](https://www.topcoder.com/thrive/articles/An%20Introduction%20to%20Binary%20Search%20and%20Red-Black%20Trees), TopCoder
144+
* ▶️ [Binary Search Tree Introduction](https://www.youtube.com/watch?v=JfSdGQdAzq8&pp=ygUgYmluYXJ5IHNlYXJjaCB0cmVlIHdpbGxpYW0gZmlzZXQ%3D&ab_channel=WilliamFiset), WilliamFiset
145+
* ▶️ [Binary Search Tree Insertion](https://www.youtube.com/watch?v=LwpLXm3eb6A&ab_channel=WilliamFiset), WilliamFiset
146+
* ▶️ [Data Structures: Binary Search Tree](https://www.youtube.com/watch?v=i_Q0v_Ct5lY&ab_channel=HackerRank), HackerRank
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/******************************************************************************
2+
3+
Online C++ Compiler.
4+
Code, Compile, Run and Debug C++ program online.
5+
Write your code in this editor and press "Run" button to compile and execute it.
6+
7+
*******************************************************************************/
8+
9+
#include <algorithm>
10+
#include <iostream>
11+
#include <unordered_map>
12+
#include <vector>
13+
14+
using namespace std;
15+
#include <iostream>
16+
#include <queue>
17+
18+
using namespace std;
19+
20+
struct TreeNode {
21+
int val;
22+
TreeNode* left;
23+
TreeNode* right;
24+
25+
TreeNode() : val(0), left(nullptr), right(nullptr) {}
26+
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
27+
TreeNode(int x, TreeNode* left, TreeNode* right)
28+
: val(x), left(left), right(right) {}
29+
};
30+
31+
TreeNode* insertIntoBST(TreeNode* root, int val) {
32+
if (root == nullptr) {
33+
return new TreeNode(val);
34+
}
35+
36+
if (val < root->val) {
37+
root->left = insertIntoBST(root->left, val);
38+
} else if (val > root->val) {
39+
root->right = insertIntoBST(root->right, val);
40+
}
41+
42+
return root;
43+
}
44+
45+
TreeNode* deleteNode(TreeNode* root, int key) {
46+
if (root == nullptr) return root;
47+
if (key < root->val)
48+
root->left = deleteNode(root->left, key);
49+
else if (key > root->val)
50+
root->right = deleteNode(root->right, key);
51+
else {
52+
if (!root->left && !root->right) return nullptr;
53+
if (!root->left)
54+
return root->right;
55+
else if (!root->right)
56+
return root->left;
57+
else {
58+
TreeNode* temp = root->right;
59+
while (temp->left) temp = temp->left;
60+
temp->left = root->left;
61+
return root->right;
62+
}
63+
}
64+
return root;
65+
}
66+
67+
void print(TreeNode* root) {
68+
queue<TreeNode*> q;
69+
q.push(root);
70+
int count;
71+
while (!q.empty()) {
72+
count = q.size();
73+
while (count > 0) {
74+
TreeNode* node = q.front();
75+
q.pop();
76+
cout << node->val << " ";
77+
if (node->left) q.push(node->left);
78+
if (node->right) q.push(node->right);
79+
count--;
80+
}
81+
cout << endl;
82+
}
83+
}
84+
85+
int main() {
86+
TreeNode* root =
87+
new TreeNode(10, new TreeNode(2, new TreeNode(1), new TreeNode(5)),
88+
new TreeNode(15, new TreeNode(11), new TreeNode(20)));
89+
90+
cout << "Before delete\n";
91+
print(root);
92+
93+
// cout << "\nAfter delete 5\n";
94+
// root = deleteNode(root, 5);
95+
// print(root);
96+
97+
// cout << "\nAfter delete 11\n";
98+
// root = new TreeNode(10, new TreeNode(2, new TreeNode(1), new TreeNode(5)),
99+
// new TreeNode(15, new TreeNode(11), new TreeNode(20)));
100+
// root = deleteNode(root, 11);
101+
// print(root);
102+
103+
// cout << "\nAfter delete 2\n";
104+
// root = new TreeNode(10, new TreeNode(2, new TreeNode(1), new TreeNode(5)),
105+
// new TreeNode(15, new TreeNode(11), new TreeNode(20)));
106+
// root = deleteNode(root, 2);
107+
// print(root);
108+
109+
cout << "\nAfter delete 10\n";
110+
root = new TreeNode(10, new TreeNode(2, new TreeNode(1), new TreeNode(5)),
111+
new TreeNode(15, new TreeNode(11), new TreeNode(20)));
112+
root = deleteNode(root, 10);
113+
print(root);
114+
115+
return 0;
116+
}

0 commit comments

Comments
 (0)