Skip to content
This repository was archived by the owner on Oct 29, 2020. It is now read-only.
Open
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
97 changes: 77 additions & 20 deletions CPP/BinarySearchTree.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,86 @@
#include <iostream>
using namespace std;

template <class T> struct Node
{
T value;
Node *left;
Node *right;
struct node {
int data;
node *left, *right;
};

Node(T value)
{
this->value = value;
}
node *newNode(int data){
node *n = new node;
n->data = data;
n->left = n->right = NULL;
return n;
}

Node (T value, Node <T> left, Node <T> right)
{
this->value = value;
this->left = left;
this->right = right;
}
void printInorder(node *root){
if(root == NULL)
return;
printInorder(root->left);
cout<<root->data<<" ";
printInorder(root->right);
}

template <class T> class BinarySearchTree{

void insert(node **root, int data){
if(*root == NULL)
*root = newNode(data);
else{
if((*root)->data > data){
insert(&(*root)->left, data);
}
else
insert(&(*root)->right, data);
}
}
node *deleteNode(node *root, int key){
if(root == NULL)
return NULL;
if(key < root->data){
root->left = deleteNode(root->left, key);
}
else if(key > root->data){
root->right = deleteNode(root->right, key);
}
else {
if(root->left == NULL){
node *res = root->right;
delete(root);
return res;
}
else if(root->right == NULL){
node *res = root->left;
delete(root);
return res;
}
else {
node *ptr = root->right;
while(ptr->left){
ptr = ptr->left;
}
root->data = ptr->data;
root->right = deleteNode(root->right, ptr->data);
}
}
return root;
}

int int main(int argc, char const *argv[]) {
/* code */
return 0;
int main(){
/* Let us create following BST
50
/ \
30 70
/ \ / \
20 40 60 80 */
struct node *root = NULL;
insert(&root, 50);
insert(&root, 30);
insert(&root, 20);
insert(&root, 40);
insert(&root, 70);
insert(&root, 60);
insert(&root, 80);

// print inoder traversal of the BST
printInorder(root);
getchar();
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Reference purposes .
16. Tries [cpp](https://github.com/saru95/DSA/blob/master/CPP/Tries.cpp) [python](https://github.com/saru95/DSA/blob/master/Python/tries.py)
17. Bellman-Ford Algorithm [cpp](https://github.com/saru95/DSA/blob/master/CPP/BellmanFord.cpp) [python](https://github.com/saru95/DSA/blob/master/Python/BellmanFord.py)
18. Rabin-Karp Algorithm [cpp](https://github.com/saru95/DSA/blob/master/CPP/RabinKarp.cpp) [python](https://github.com/saru95/DSA/blob/master/Python/RabinKarp.py)
19. Binary Search Tree [python](https://github.com/saru95/DSA/blob/master/Python/BinarySearchTree.py) [c](https://github.com/saru95/DSA/blob/master/C/BST.c)
19. Binary Search Tree [cpp](https://github.com/saru95/DSA/blob/master/CPP/BinarySearchTree.cpp) [python](https://github.com/saru95/DSA/blob/master/Python/BinarySearchTree.py) [c](https://github.com/saru95/DSA/blob/master/C/BST.c)
20. Sieve of Eratosthenes [cpp](https://github.com/saru95/DSA/blob/master/CPP/SEPrime.cpp) [python](https://github.com/saru95/DSA/blob/master/Python/SEPrime.py) [java](https://github.com/saru95/DSA/blob/master/Java/SieveOfEratosthenes.java) [kotlin](https://github.com/saru95/DSA/blob/master/Kotlin/SieveOfEratosthenes.kt)
21. Maximum Bipartite Matching [cpp](https://github.com/saru95/DSA/blob/master/CPP/MBM.cpp) [python](https://github.com/saru95/DSA/blob/master/Python/MBM.py)
22. Floyd-Warshall Algorithm [cpp](https://github.com/saru95/DSA/blob/master/CPP/FWA.cpp) [python](https://github.com/saru95/DSA/blob/master/Python/FloydWarshall.py) [java](https://github.com/saru95/DSA/blob/master/Java/floyd.java) [kotlin](https://github.com/saru95/DSA/blob/master/Kotlin/Floyd.kt)
Expand Down