diff --git a/CPP/BinarySearchTree.cpp b/CPP/BinarySearchTree.cpp index 973e2da..3e364e6 100644 --- a/CPP/BinarySearchTree.cpp +++ b/CPP/BinarySearchTree.cpp @@ -1,29 +1,86 @@ #include +using namespace std; -template 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 left, Node right) - { - this->value = value; - this->left = left; - this->right = right; - } +void printInorder(node *root){ + if(root == NULL) + return; + printInorder(root->left); + cout<data<<" "; + printInorder(root->right); } -template 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(); } diff --git a/README.md b/README.md index 15e8530..4bc3e93 100644 --- a/README.md +++ b/README.md @@ -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)