Skip to content

GH-250: Binary Search Tree in C++ #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions concepts/cpp/binary-search-tree/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# Binaray Search Tree

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.

## Binary Search Tree Implementation in C++

Binary Search TreeNode Struct:


```c++
#include <iostream>
#include <queue>

using namespace std;

struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;

TreeNode(): val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
}
```


```c++
TreeNode* root = new TreeNode(10);
```

**Print**: Print the tree in order


```c++
void print(TreeNode* root){
queue<TreeNode*> q;
q.push(root);
int count;
while(!q.empty()){
count = q.size();
while(count > 0){
TreeNode* node = q.front();
q.pop();
cout << node->val << " ";
if(node->left) q.push(node->left);
if(node->right) q.push(node->right);
count--;
}
cout << endl;
}
}
```

**Insertion**: Insert a new node into the tree


```c++
TreeNode* insertIntoBST(TreeNode* root, int val) {
if (root == nullptr) {
return new TreeNode(val);
}

if (val < root->val) {
root->left = insertIntoBST(root->left, val);
} else if (val > root->val){
root->right = insertIntoBST(root->right, val);
}

return root;
}
```


```c++
root = insertIntoBST(root, 2);
root = insertIntoBST(root, 1);
root = insertIntoBST(root, 5);
root = insertIntoBST(root, 15);
root = insertIntoBST(root, 11);
root = insertIntoBST(root, 20);
print(root)
```

10
2 15
1 5 11 20


**Deletion**: Delete a node from the tree


```c++
TreeNode* deleteNode(TreeNode* root, int key) {
if (root == nullptr) return root;
if (key < root->val)
root->left = deleteNode(root->left, key);
else if (key > root->val)
root->right = deleteNode(root->right, key);
else {
if (!root->left && !root->right) return nullptr;
if (!root->left)
return root->right;
else if (!root->right)
return root->left;
else {
TreeNode* temp = root->right;
while (temp->left) temp = temp->left;
temp->left = root->left;
return root->right;
}
}
return root;
}
```


```c++
root = deleteNode(root, 5);
print(root)
```

10
2 15
1 11 20



```c++
root = deleteNode(root, 10);
print(root)
```

15
11 20
2
1


## 🔗 Further Reading

* [Binary Search Tree](https://en.wikipedia.org/wiki/Binary_search_tree)
* [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
* ▶️ [Binary Search Tree Introduction](https://www.youtube.com/watch?v=JfSdGQdAzq8&pp=ygUgYmluYXJ5IHNlYXJjaCB0cmVlIHdpbGxpYW0gZmlzZXQ%3D&ab_channel=WilliamFiset), WilliamFiset
* ▶️ [Binary Search Tree Insertion](https://www.youtube.com/watch?v=LwpLXm3eb6A&ab_channel=WilliamFiset), WilliamFiset
* ▶️ [Data Structures: Binary Search Tree](https://www.youtube.com/watch?v=i_Q0v_Ct5lY&ab_channel=HackerRank), HackerRank
116 changes: 116 additions & 0 deletions concepts/cpp/binary-search-tree/binary_search_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/******************************************************************************

Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;
#include <iostream>
#include <queue>

using namespace std;

struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;

TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode* left, TreeNode* right)
: val(x), left(left), right(right) {}
};

TreeNode* insertIntoBST(TreeNode* root, int val) {
if (root == nullptr) {
return new TreeNode(val);
}

if (val < root->val) {
root->left = insertIntoBST(root->left, val);
} else if (val > root->val) {
root->right = insertIntoBST(root->right, val);
}

return root;
}

TreeNode* deleteNode(TreeNode* root, int key) {
if (root == nullptr) return root;
if (key < root->val)
root->left = deleteNode(root->left, key);
else if (key > root->val)
root->right = deleteNode(root->right, key);
else {
if (!root->left && !root->right) return nullptr;
if (!root->left)
return root->right;
else if (!root->right)
return root->left;
else {
TreeNode* temp = root->right;
while (temp->left) temp = temp->left;
temp->left = root->left;
return root->right;
}
}
return root;
}

void print(TreeNode* root) {
queue<TreeNode*> q;
q.push(root);
int count;
while (!q.empty()) {
count = q.size();
while (count > 0) {
TreeNode* node = q.front();
q.pop();
cout << node->val << " ";
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
count--;
}
cout << endl;
}
}

int main() {
TreeNode* root =
new TreeNode(10, new TreeNode(2, new TreeNode(1), new TreeNode(5)),
new TreeNode(15, new TreeNode(11), new TreeNode(20)));

cout << "Before delete\n";
print(root);

// cout << "\nAfter delete 5\n";
// root = deleteNode(root, 5);
// print(root);

// cout << "\nAfter delete 11\n";
// root = new TreeNode(10, new TreeNode(2, new TreeNode(1), new TreeNode(5)),
// new TreeNode(15, new TreeNode(11), new TreeNode(20)));
// root = deleteNode(root, 11);
// print(root);

// cout << "\nAfter delete 2\n";
// root = new TreeNode(10, new TreeNode(2, new TreeNode(1), new TreeNode(5)),
// new TreeNode(15, new TreeNode(11), new TreeNode(20)));
// root = deleteNode(root, 2);
// print(root);

cout << "\nAfter delete 10\n";
root = new TreeNode(10, new TreeNode(2, new TreeNode(1), new TreeNode(5)),
new TreeNode(15, new TreeNode(11), new TreeNode(20)));
root = deleteNode(root, 10);
print(root);

return 0;
}
Loading