diff --git a/data_structures/binary_search_tree.cpp b/data_structures/binary_search_tree.cpp index 86057c6c5..637a58778 100644 --- a/data_structures/binary_search_tree.cpp +++ b/data_structures/binary_search_tree.cpp @@ -6,7 +6,8 @@ * \warning This program is a poor implementation - C style - and does not * utilize any of the C++ STL features. */ -#include +#include // for NULL +#include // for operator<<, basic_ostream, char_traits, cout struct node { int val; @@ -145,28 +146,28 @@ int main() { std::cin >> ch; int x; switch (ch) { - case 1: - std::cout << "\nEnter the value to be Inserted : "; - std::cin >> x; - Insert(root, x); - break; - case 2: - std::cout << "\nEnter the value to be Deleted : "; - std::cin >> x; - Remove(root, root, x); - break; - case 3: - BFT(root); - break; - case 4: - Pre(root); - break; - case 5: - In(root); - break; - case 6: - Post(root); - break; + case 1: + std::cout << "\nEnter the value to be Inserted : "; + std::cin >> x; + Insert(root, x); + break; + case 2: + std::cout << "\nEnter the value to be Deleted : "; + std::cin >> x; + Remove(root, root, x); + break; + case 3: + BFT(root); + break; + case 4: + Pre(root); + break; + case 5: + In(root); + break; + case 6: + Post(root); + break; } } while (ch != 0); diff --git a/data_structures/binary_search_tree2.cpp b/data_structures/binary_search_tree2.cpp index 55ae3a438..b1fbac9a9 100644 --- a/data_structures/binary_search_tree2.cpp +++ b/data_structures/binary_search_tree2.cpp @@ -5,11 +5,13 @@ * @see binary_search_tree.cpp */ -#include -#include -#include -#include -#include +#include // for assert +#include // for function +#include // for basic_ostream, operator<<, cout, char_traits +#include // for operator==, unique_ptr, allocator +#include // for vector, operator== +#include // for size_t +#include // for move /** * @brief The Binary Search Tree class. diff --git a/data_structures/bloom_filter.cpp b/data_structures/bloom_filter.cpp index e2f7372e0..75a7d71a2 100644 --- a/data_structures/bloom_filter.cpp +++ b/data_structures/bloom_filter.cpp @@ -22,12 +22,13 @@ * @author [DanArmor](https://github.com/DanArmor) */ -#include /// for assert -#include /// for list of hash functions for bloom filter constructor -#include /// for initializer_list for bloom filter constructor -#include /// for testing on strings -#include /// for std::vector -#include /// for IO operations +#include // for assert +#include // for function +#include // for initializer_list +#include // for basic_string, string +#include // for vector +#include // for operator<<, basic_ostream, cout +#include // for size_t /** * @namespace data_structures diff --git a/data_structures/cll/cll.cpp b/data_structures/cll/cll.cpp index 42bc9067e..68f2e93d8 100644 --- a/data_structures/cll/cll.cpp +++ b/data_structures/cll/cll.cpp @@ -2,6 +2,10 @@ A simple class for Cicular Linear Linked List */ #include "cll.h" + +#include // for NULL +#include // for basic_ostream, char_traits, operator<<, cout + using namespace std; /* Constructor */ diff --git a/data_structures/cll/main_cll.cpp b/data_structures/cll/main_cll.cpp index 0b6bfd3ed..3f67b628b 100644 --- a/data_structures/cll/main_cll.cpp +++ b/data_structures/cll/main_cll.cpp @@ -1,4 +1,7 @@ -#include "cll.h" +#include // for basic_ostream, operator<<, char_traits, cout, endl + +#include "cll.h" // for cll + using namespace std; int main() { diff --git a/data_structures/doubly_linked_list.cpp b/data_structures/doubly_linked_list.cpp index 30cc257d8..ea09a1d7a 100644 --- a/data_structures/doubly_linked_list.cpp +++ b/data_structures/doubly_linked_list.cpp @@ -1,6 +1,5 @@ -#include -#include -#include +#include // for NULL +#include // for operator<<, basic_ostream, cout, char_traits struct node { int val; diff --git a/data_structures/dsu_path_compression.cpp b/data_structures/dsu_path_compression.cpp index 9dff7b40b..8bcaf7cbf 100644 --- a/data_structures/dsu_path_compression.cpp +++ b/data_structures/dsu_path_compression.cpp @@ -18,10 +18,12 @@ * @see dsu_union_rank.cpp */ -#include /// for assert -#include /// for integral typedefs -#include /// for IO operations -#include /// for std::vector +#include // for assert +#include // for uint64_t +#include // for char_traits, basic_ostream, operator<<, cout, endl +#include // for vector +#include // for max, min +#include // for swap using std::cout; using std::endl; diff --git a/data_structures/dsu_union_rank.cpp b/data_structures/dsu_union_rank.cpp index 4ca53bb76..3a491254a 100644 --- a/data_structures/dsu_union_rank.cpp +++ b/data_structures/dsu_union_rank.cpp @@ -19,10 +19,11 @@ * @see dsu_path_compression.cpp */ -#include /// for assert -#include /// for integral typedefs -#include /// for IO operations -#include /// for std::vector +#include // for assert +#include // for uint64_t +#include // for char_traits, basic_ostream, operator<<, cout, endl +#include // for vector +#include // for swap using std::cout; using std::endl; diff --git a/data_structures/linked_list.cpp b/data_structures/linked_list.cpp index 08bfe68b1..c10b76c98 100644 --- a/data_structures/linked_list.cpp +++ b/data_structures/linked_list.cpp @@ -14,9 +14,10 @@ * to point to the node that the current node is pointing to, and then returning * the current node to heap store. */ -#include -#include -#include +#include // for isdigit +#include // for operator<<, basic_ostream, cout, basic_istream, cin +#include // for shared_ptr, __shared_ptr_access, make_shared +#include // for char_traits, basic_string, operator>>, stoi, string /** * @namespace data_structures diff --git a/data_structures/list_array.cpp b/data_structures/list_array.cpp index 8995b6ce2..0660d4b08 100644 --- a/data_structures/list_array.cpp +++ b/data_structures/list_array.cpp @@ -14,10 +14,11 @@ * and print the list. */ -#include /// for std::array -#include /// for assert -#include /// for integral typedefs -#include /// for io operations +#include // for array +#include // for assert +#include // for uint64_t +#include // for operator<<, basic_ostream::operator<<, basic_ost... +#include // for swap /** * @namespace data_structures diff --git a/data_structures/morrisinorder.cpp b/data_structures/morrisinorder.cpp index f1f9e068c..ae70bc1e7 100644 --- a/data_structures/morrisinorder.cpp +++ b/data_structures/morrisinorder.cpp @@ -1,5 +1,6 @@ -#include -#include +#include // for NULL +#include // for char_traits, operator<<, basic_ostream, basic_os... +#include // for queue /************************** @author shrutisheoran diff --git a/data_structures/queue_using_array.cpp b/data_structures/queue_using_array.cpp index cbd84a623..5afd47011 100644 --- a/data_structures/queue_using_array.cpp +++ b/data_structures/queue_using_array.cpp @@ -17,9 +17,10 @@ * @author [Pooja](https://github.com/pooja-git11) * @author [Farbod Ahmadian](https://github.com/farbodahm) */ -#include /// for std::array -#include /// for integral typedefs -#include /// for io operations +#include // for exit +#include // for array +#include // for int16_t, int8_t, uint16_t +#include // for operator<<, basic_ostream, cout, char_traits constexpr uint16_t max_size{10}; ///< Maximum size of the queue diff --git a/data_structures/queue_using_linked_list.cpp b/data_structures/queue_using_linked_list.cpp index 7b44d240c..39eae2895 100644 --- a/data_structures/queue_using_linked_list.cpp +++ b/data_structures/queue_using_linked_list.cpp @@ -1,4 +1,6 @@ -#include +#include // for NULL +#include // for operator<<, basic_ostream, char_traits, cout + using namespace std; struct node { diff --git a/data_structures/queue_using_linkedlist.cpp b/data_structures/queue_using_linkedlist.cpp index f1bf18123..d487cf7d3 100644 --- a/data_structures/queue_using_linkedlist.cpp +++ b/data_structures/queue_using_linkedlist.cpp @@ -1,7 +1,8 @@ /* Write a program to implement Queue using linkedlist. */ -#include +#include // for NULL, exit +#include // for operator<<, basic_ostream, cout, char_traits struct linkedlist { int data; diff --git a/data_structures/queue_using_two_stacks.cpp b/data_structures/queue_using_two_stacks.cpp index a09644686..117f5b2c4 100644 --- a/data_structures/queue_using_two_stacks.cpp +++ b/data_structures/queue_using_two_stacks.cpp @@ -5,9 +5,10 @@ * Implementation of a Queue using two Stacks. */ -#include -#include -#include +#include // for exit +#include // for assert +#include // for operator<<, basic_ostream, char_traits, cout, cerr +#include // for stack namespace { /** diff --git a/data_structures/rb_tree.cpp b/data_structures/rb_tree.cpp index f2a51a30c..cda06d639 100644 --- a/data_structures/rb_tree.cpp +++ b/data_structures/rb_tree.cpp @@ -1,505 +1,431 @@ -#include +#include // for NULL +#include using namespace std; -struct node -{ - int key; - node *parent; - char color; - node *left; - node *right; +struct node { + int key; + node *parent; + char color; + node *left; + node *right; }; -class RBtree -{ - node *root; - node *q; -public: - RBtree() - { - q = NULL; - root = NULL; - } - void insert(); - void insertfix(node *); - void leftrotate(node *); - void rightrotate(node *); - void del(); - node* successor(node *); - void delfix(node *); - void disp(); - void display(node *); - void search(); +class RBtree { + node *root; + node *q; + + public: + RBtree() { + q = NULL; + root = NULL; + } + void insert(); + void insertfix(node *); + void leftrotate(node *); + void rightrotate(node *); + void del(); + node *successor(node *); + void delfix(node *); + void disp(); + void display(node *); + void search(); }; -void RBtree::insert() -{ - int z, i = 0; - cout << "\nEnter key of the node to be inserted: "; - cin >> z; - node *p, *q; - node *t = new node; - t->key = z; - t->left = NULL; - t->right = NULL; - t->color = 'r'; - p = root; - q = NULL; - if (root == NULL) - { - root = t; - t->parent = NULL; - } - else - { - while (p != NULL) - { - q = p; - if (p->key < t->key) - p = p->right; - else - p = p->left; - } - t->parent = q; - if (q->key < t->key) - q->right = t; - else - q->left = t; - } - insertfix(t); +void RBtree::insert() { + int z, i = 0; + cout << "\nEnter key of the node to be inserted: "; + cin >> z; + node *p, *q; + node *t = new node; + t->key = z; + t->left = NULL; + t->right = NULL; + t->color = 'r'; + p = root; + q = NULL; + if (root == NULL) { + root = t; + t->parent = NULL; + } else { + while (p != NULL) { + q = p; + if (p->key < t->key) + p = p->right; + else + p = p->left; + } + t->parent = q; + if (q->key < t->key) + q->right = t; + else + q->left = t; + } + insertfix(t); } -void RBtree::insertfix(node *t) -{ - node *u; - if (root == t) - { - t->color = 'b'; - return; - } - while (t->parent != NULL && t->parent->color == 'r') - { - node *g = t->parent->parent; - if (g->left == t->parent) - { - if (g->right != NULL) - { - u = g->right; - if (u->color == 'r') - { - t->parent->color = 'b'; - u->color = 'b'; - g->color = 'r'; - t = g; - } - } - else - { - if (t->parent->right == t) - { - t = t->parent; - leftrotate(t); - } - t->parent->color = 'b'; - g->color = 'r'; - rightrotate(g); - } - } - else - { - if (g->left != NULL) - { - u = g->left; - if (u->color == 'r') - { - t->parent->color = 'b'; - u->color = 'b'; - g->color = 'r'; - t = g; - } - } - else - { - if (t->parent->left == t) - { - t = t->parent; - rightrotate(t); - } - t->parent->color = 'b'; - g->color = 'r'; - leftrotate(g); - } - } - root->color = 'b'; - } +void RBtree::insertfix(node *t) { + node *u; + if (root == t) { + t->color = 'b'; + return; + } + while (t->parent != NULL && t->parent->color == 'r') { + node *g = t->parent->parent; + if (g->left == t->parent) { + if (g->right != NULL) { + u = g->right; + if (u->color == 'r') { + t->parent->color = 'b'; + u->color = 'b'; + g->color = 'r'; + t = g; + } + } else { + if (t->parent->right == t) { + t = t->parent; + leftrotate(t); + } + t->parent->color = 'b'; + g->color = 'r'; + rightrotate(g); + } + } else { + if (g->left != NULL) { + u = g->left; + if (u->color == 'r') { + t->parent->color = 'b'; + u->color = 'b'; + g->color = 'r'; + t = g; + } + } else { + if (t->parent->left == t) { + t = t->parent; + rightrotate(t); + } + t->parent->color = 'b'; + g->color = 'r'; + leftrotate(g); + } + } + root->color = 'b'; + } } -void RBtree::del() -{ - if (root == NULL) - { - cout << "\nEmpty Tree."; - return; - } - int x; - cout << "\nEnter the key of the node to be deleted: "; - cin >> x; - node *p; - p = root; - node *y = NULL; - node *q = NULL; - int found = 0; - while (p != NULL && found == 0) - { - if (p->key == x) - found = 1; - if (found == 0) - { - if (p->key < x) - p = p->right; - else - p = p->left; - } - } - if (found == 0) - { - cout << "\nElement Not Found."; - return; - } - else - { - cout << "\nDeleted Element: " << p->key; - cout << "\nColour: "; - if (p->color == 'b') - cout << "Black\n"; - else - cout << "Red\n"; +void RBtree::del() { + if (root == NULL) { + cout << "\nEmpty Tree."; + return; + } + int x; + cout << "\nEnter the key of the node to be deleted: "; + cin >> x; + node *p; + p = root; + node *y = NULL; + node *q = NULL; + int found = 0; + while (p != NULL && found == 0) { + if (p->key == x) + found = 1; + if (found == 0) { + if (p->key < x) + p = p->right; + else + p = p->left; + } + } + if (found == 0) { + cout << "\nElement Not Found."; + return; + } else { + cout << "\nDeleted Element: " << p->key; + cout << "\nColour: "; + if (p->color == 'b') + cout << "Black\n"; + else + cout << "Red\n"; - if (p->parent != NULL) - cout << "\nParent: " << p->parent->key; - else - cout << "\nThere is no parent of the node. "; - if (p->right != NULL) - cout << "\nRight Child: " << p->right->key; - else - cout << "\nThere is no right child of the node. "; - if (p->left != NULL) - cout << "\nLeft Child: " << p->left->key; - else - cout << "\nThere is no left child of the node. "; - cout << "\nNode Deleted."; - if (p->left == NULL || p->right == NULL) - y = p; - else - y = successor(p); - if (y->left != NULL) - q = y->left; - else - { - if (y->right != NULL) - q = y->right; - else - q = NULL; - } - if (q != NULL) - q->parent = y->parent; - if (y->parent == NULL) - root = q; - else - { - if (y == y->parent->left) - y->parent->left = q; - else - y->parent->right = q; - } - if (y != p) - { - p->color = y->color; - p->key = y->key; - } - if (y->color == 'b') - delfix(q); - } + if (p->parent != NULL) + cout << "\nParent: " << p->parent->key; + else + cout << "\nThere is no parent of the node. "; + if (p->right != NULL) + cout << "\nRight Child: " << p->right->key; + else + cout << "\nThere is no right child of the node. "; + if (p->left != NULL) + cout << "\nLeft Child: " << p->left->key; + else + cout << "\nThere is no left child of the node. "; + cout << "\nNode Deleted."; + if (p->left == NULL || p->right == NULL) + y = p; + else + y = successor(p); + if (y->left != NULL) + q = y->left; + else { + if (y->right != NULL) + q = y->right; + else + q = NULL; + } + if (q != NULL) + q->parent = y->parent; + if (y->parent == NULL) + root = q; + else { + if (y == y->parent->left) + y->parent->left = q; + else + y->parent->right = q; + } + if (y != p) { + p->color = y->color; + p->key = y->key; + } + if (y->color == 'b') + delfix(q); + } } -void RBtree::delfix(node *p) -{ - node *s; - while (p != root && p->color == 'b') - { - if (p->parent->left == p) - { - s = p->parent->right; - if (s->color == 'r') - { - s->color = 'b'; - p->parent->color = 'r'; - leftrotate(p->parent); - s = p->parent->right; - } - if (s->right->color == 'b'&&s->left->color == 'b') - { - s->color = 'r'; - p = p->parent; - } - else - { - if (s->right->color == 'b') - { - s->left->color = 'b'; - s->color = 'r'; - rightrotate(s); - s = p->parent->right; - } - s->color = p->parent->color; - p->parent->color = 'b'; - s->right->color = 'b'; - leftrotate(p->parent); - p = root; - } - } - else - { - s = p->parent->left; - if (s->color == 'r') - { - s->color = 'b'; - p->parent->color = 'r'; - rightrotate(p->parent); - s = p->parent->left; - } - if (s->left->color == 'b'&&s->right->color == 'b') - { - s->color = 'r'; - p = p->parent; - } - else - { - if (s->left->color == 'b') - { - s->right->color = 'b'; - s->color = 'r'; - leftrotate(s); - s = p->parent->left; - } - s->color = p->parent->color; - p->parent->color = 'b'; - s->left->color = 'b'; - rightrotate(p->parent); - p = root; - } - } - p->color = 'b'; - root->color = 'b'; - } +void RBtree::delfix(node *p) { + node *s; + while (p != root && p->color == 'b') { + if (p->parent->left == p) { + s = p->parent->right; + if (s->color == 'r') { + s->color = 'b'; + p->parent->color = 'r'; + leftrotate(p->parent); + s = p->parent->right; + } + if (s->right->color == 'b' && s->left->color == 'b') { + s->color = 'r'; + p = p->parent; + } else { + if (s->right->color == 'b') { + s->left->color = 'b'; + s->color = 'r'; + rightrotate(s); + s = p->parent->right; + } + s->color = p->parent->color; + p->parent->color = 'b'; + s->right->color = 'b'; + leftrotate(p->parent); + p = root; + } + } else { + s = p->parent->left; + if (s->color == 'r') { + s->color = 'b'; + p->parent->color = 'r'; + rightrotate(p->parent); + s = p->parent->left; + } + if (s->left->color == 'b' && s->right->color == 'b') { + s->color = 'r'; + p = p->parent; + } else { + if (s->left->color == 'b') { + s->right->color = 'b'; + s->color = 'r'; + leftrotate(s); + s = p->parent->left; + } + s->color = p->parent->color; + p->parent->color = 'b'; + s->left->color = 'b'; + rightrotate(p->parent); + p = root; + } + } + p->color = 'b'; + root->color = 'b'; + } } -void RBtree::leftrotate(node *p) -{ - if (p->right == NULL) - return; - else - { - node *y = p->right; - if (y->left != NULL) - { - p->right = y->left; - y->left->parent = p; - } - else - p->right = NULL; - if (p->parent != NULL) - y->parent = p->parent; - if (p->parent == NULL) - root = y; - else - { - if (p == p->parent->left) - p->parent->left = y; - else - p->parent->right = y; - } - y->left = p; - p->parent = y; - } +void RBtree::leftrotate(node *p) { + if (p->right == NULL) + return; + else { + node *y = p->right; + if (y->left != NULL) { + p->right = y->left; + y->left->parent = p; + } else + p->right = NULL; + if (p->parent != NULL) + y->parent = p->parent; + if (p->parent == NULL) + root = y; + else { + if (p == p->parent->left) + p->parent->left = y; + else + p->parent->right = y; + } + y->left = p; + p->parent = y; + } } -void RBtree::rightrotate(node *p) -{ - if (p->left == NULL) - return; - else - { - node *y = p->left; - if (y->right != NULL) - { - p->left = y->right; - y->right->parent = p; - } - else - p->left = NULL; - if (p->parent != NULL) - y->parent = p->parent; - if (p->parent == NULL) - root = y; - else - { - if (p == p->parent->left) - p->parent->left = y; - else - p->parent->right = y; - } - y->right = p; - p->parent = y; - } +void RBtree::rightrotate(node *p) { + if (p->left == NULL) + return; + else { + node *y = p->left; + if (y->right != NULL) { + p->left = y->right; + y->right->parent = p; + } else + p->left = NULL; + if (p->parent != NULL) + y->parent = p->parent; + if (p->parent == NULL) + root = y; + else { + if (p == p->parent->left) + p->parent->left = y; + else + p->parent->right = y; + } + y->right = p; + p->parent = y; + } } -node* RBtree::successor(node *p) -{ - node *y = NULL; - if (p->left != NULL) - { - y = p->left; - while (y->right != NULL) - y = y->right; - } - else - { - y = p->right; - while (y->left != NULL) - y = y->left; - } - return y; +node *RBtree::successor(node *p) { + node *y = NULL; + if (p->left != NULL) { + y = p->left; + while (y->right != NULL) y = y->right; + } else { + y = p->right; + while (y->left != NULL) y = y->left; + } + return y; } -void RBtree::disp() -{ - display(root); +void RBtree::disp() { display(root); } +void RBtree::display(node *p) { + if (root == NULL) { + cout << "\nEmpty Tree."; + return; + } + if (p != NULL) { + cout << "\n\t NODE: "; + cout << "\n Key: " << p->key; + cout << "\n Colour: "; + if (p->color == 'b') + cout << "Black"; + else + cout << "Red"; + if (p->parent != NULL) + cout << "\n Parent: " << p->parent->key; + else + cout << "\n There is no parent of the node. "; + if (p->right != NULL) + cout << "\n Right Child: " << p->right->key; + else + cout << "\n There is no right child of the node. "; + if (p->left != NULL) + cout << "\n Left Child: " << p->left->key; + else + cout << "\n There is no left child of the node. "; + cout << endl; + if (p->left) { + cout << "\n\nLeft:\n"; + display(p->left); + } + /*else + cout<<"\nNo Left Child.\n";*/ + if (p->right) { + cout << "\n\nRight:\n"; + display(p->right); + } + /*else + cout<<"\nNo Right Child.\n"*/ + } } -void RBtree::display(node *p) -{ - if (root == NULL) - { - cout << "\nEmpty Tree."; - return; - } - if (p != NULL) - { - cout << "\n\t NODE: "; - cout << "\n Key: " << p->key; - cout << "\n Colour: "; - if (p->color == 'b') - cout << "Black"; - else - cout << "Red"; - if (p->parent != NULL) - cout << "\n Parent: " << p->parent->key; - else - cout << "\n There is no parent of the node. "; - if (p->right != NULL) - cout << "\n Right Child: " << p->right->key; - else - cout << "\n There is no right child of the node. "; - if (p->left != NULL) - cout << "\n Left Child: " << p->left->key; - else - cout << "\n There is no left child of the node. "; - cout << endl; - if (p->left) - { - cout << "\n\nLeft:\n"; - display(p->left); - } - /*else - cout<<"\nNo Left Child.\n";*/ - if (p->right) - { - cout << "\n\nRight:\n"; - display(p->right); - } - /*else - cout<<"\nNo Right Child.\n"*/ - } +void RBtree::search() { + if (root == NULL) { + cout << "\nEmpty Tree\n"; + return; + } + int x; + cout << "\n Enter key of the node to be searched: "; + cin >> x; + node *p = root; + int found = 0; + while (p != NULL && found == 0) { + if (p->key == x) + found = 1; + if (found == 0) { + if (p->key < x) + p = p->right; + else + p = p->left; + } + } + if (found == 0) + cout << "\nElement Not Found."; + else { + cout << "\n\t FOUND NODE: "; + cout << "\n Key: " << p->key; + cout << "\n Colour: "; + if (p->color == 'b') + cout << "Black"; + else + cout << "Red"; + if (p->parent != NULL) + cout << "\n Parent: " << p->parent->key; + else + cout << "\n There is no parent of the node. "; + if (p->right != NULL) + cout << "\n Right Child: " << p->right->key; + else + cout << "\n There is no right child of the node. "; + if (p->left != NULL) + cout << "\n Left Child: " << p->left->key; + else + cout << "\n There is no left child of the node. "; + cout << endl; + } } -void RBtree::search() -{ - if (root == NULL) - { - cout << "\nEmpty Tree\n"; - return; - } - int x; - cout << "\n Enter key of the node to be searched: "; - cin >> x; - node *p = root; - int found = 0; - while (p != NULL && found == 0) - { - if (p->key == x) - found = 1; - if (found == 0) - { - if (p->key < x) - p = p->right; - else - p = p->left; - } - } - if (found == 0) - cout << "\nElement Not Found."; - else - { - cout << "\n\t FOUND NODE: "; - cout << "\n Key: " << p->key; - cout << "\n Colour: "; - if (p->color == 'b') - cout << "Black"; - else - cout << "Red"; - if (p->parent != NULL) - cout << "\n Parent: " << p->parent->key; - else - cout << "\n There is no parent of the node. "; - if (p->right != NULL) - cout << "\n Right Child: " << p->right->key; - else - cout << "\n There is no right child of the node. "; - if (p->left != NULL) - cout << "\n Left Child: " << p->left->key; - else - cout << "\n There is no left child of the node. "; - cout << endl; +int main() { + int ch, y = 0; + RBtree obj; + do { + cout << "\n\t RED BLACK TREE "; + cout << "\n 1. Insert in the tree "; + cout << "\n 2. Delete a node from the tree"; + cout << "\n 3. Search for an element in the tree"; + cout << "\n 4. Display the tree "; + cout << "\n 5. Exit "; + cout << "\nEnter Your Choice: "; + cin >> ch; + switch (ch) { + case 1: + obj.insert(); + cout << "\nNode Inserted.\n"; + break; + case 2: + obj.del(); + break; + case 3: + obj.search(); + break; + case 4: + obj.disp(); + break; + case 5: + y = 1; + break; + default: + cout << "\nEnter a Valid Choice."; + } + cout << endl; - } -} -int main() -{ - int ch, y = 0; - RBtree obj; - do - { - cout << "\n\t RED BLACK TREE "; - cout << "\n 1. Insert in the tree "; - cout << "\n 2. Delete a node from the tree"; - cout << "\n 3. Search for an element in the tree"; - cout << "\n 4. Display the tree "; - cout << "\n 5. Exit "; - cout << "\nEnter Your Choice: "; - cin >> ch; - switch (ch) - { - case 1: obj.insert(); - cout << "\nNode Inserted.\n"; - break; - case 2: obj.del(); - break; - case 3: obj.search(); - break; - case 4: obj.disp(); - break; - case 5: y = 1; - break; - default: cout << "\nEnter a Valid Choice."; - } - cout << endl; - - } while (y != 1); - return 1; + } while (y != 1); + return 1; } diff --git a/data_structures/reverse_a_linked_list.cpp b/data_structures/reverse_a_linked_list.cpp index d8315b4bb..d776c83bf 100644 --- a/data_structures/reverse_a_linked_list.cpp +++ b/data_structures/reverse_a_linked_list.cpp @@ -22,9 +22,12 @@ *scenes](https://drive.google.com/file/d/1pM5COF0wx-wermnNy_svtyZquaCUP2xS/view?usp=sharing) */ -#include /// for assert -#include /// for I/O operations -#include /// for managing dynamic storage +#include // for int32_t +#include // for exit +#include // for assert +#include // for char_traits, basic_ostream, operator<<, cerr, cout +#include // for bad_alloc +#include // for logic_error /** * @namespace data_structures diff --git a/data_structures/skip_list.cpp b/data_structures/skip_list.cpp index 9cf8c430f..bac77da2c 100644 --- a/data_structures/skip_list.cpp +++ b/data_structures/skip_list.cpp @@ -13,12 +13,12 @@ * @author [Krishna Vedala](https://github.com/kvedala) */ -#include -#include -#include -#include -#include -#include +#include // for array +#include // for time +#include // for basic_ostream, operator<<, char_traits, cout +#include // for shared_ptr, __shared_ptr_access, operator!=, ope... +#include // for vector +#include // for rand, srand, RAND_MAX /** \namespace data_structures * \brief Data-structure algorithms diff --git a/data_structures/sparse_table.cpp b/data_structures/sparse_table.cpp index 507dd3944..654c81d6e 100644 --- a/data_structures/sparse_table.cpp +++ b/data_structures/sparse_table.cpp @@ -22,10 +22,13 @@ * `gcd()`, `lcm()`, and `max()` by changing a few lines of code. */ -#include /// for std::array -#include /// for assert -#include /// for integral typedefs -#include /// for IO operations +#include // for copy +#include // for array +#include // for assert +#include // for size_t +#include // for int64_t, uint32_t, uint8_t +#include // for char_traits, basic_ostream, operator<<, cout, endl +#include // for begin, end /** * @namespace data_structures diff --git a/data_structures/stack_using_array.cpp b/data_structures/stack_using_array.cpp index 73feaf445..bda5ff0f2 100644 --- a/data_structures/stack_using_array.cpp +++ b/data_structures/stack_using_array.cpp @@ -1,7 +1,8 @@ -#include /// For std::assert -#include /// For std::cout -#include /// For std::unique_ptr -#include /// For std::out_of_range +#include // for assert +#include // for char_traits, basic_ostream, cout, operator<<, endl +#include // for unique_ptr +#include // for out_of_range +#include // for basic_string, operator==, string /** * @namespace diff --git a/data_structures/stack_using_queue.cpp b/data_structures/stack_using_queue.cpp index 7867d55ca..0bf69170d 100644 --- a/data_structures/stack_using_queue.cpp +++ b/data_structures/stack_using_queue.cpp @@ -8,10 +8,9 @@ * [StudyTonight](https://www.studytonight.com/data-structures/stack-using-queue) * @author [tushar2407](https://github.com/tushar2407) */ -#include /// for assert -#include /// for integral typedefs -#include /// for IO operations -#include /// for queue data structure +#include // for assert +#include // for int64_t, uint32_t +#include // for queue, swap /** * @namespace data_structures diff --git a/data_structures/test_queue.cpp b/data_structures/test_queue.cpp index d0fa3502e..3463f490b 100644 --- a/data_structures/test_queue.cpp +++ b/data_structures/test_queue.cpp @@ -1,7 +1,10 @@ -#include /// for assert -#include /// for std::cout +#include // for assert +#include // for operator<<, basic_ostream, cout +#include // for operator== +#include // for invalid_argument +#include // for vector, operator== -#include "./queue.hpp" +#include "./queue.hpp" // for queue template void testConstructedQueueIsEmpty() { diff --git a/data_structures/test_stack.cpp b/data_structures/test_stack.cpp index 81039e796..551261ac8 100644 --- a/data_structures/test_stack.cpp +++ b/data_structures/test_stack.cpp @@ -1,9 +1,10 @@ -#include /// for assert -#include /// for std::cout -#include /// std::invalid_argument -#include /// for std::vector +#include // for assert +#include // for operator<<, basic_ostream, cout +#include // for invalid_argument +#include // for vector, operator== +#include // for operator== -#include "./stack.hpp" +#include "./stack.hpp" // for stack template void testConstructedStackIsEmpty() { diff --git a/data_structures/test_stack_students.cpp b/data_structures/test_stack_students.cpp index dc3514bcb..2b2b275bc 100644 --- a/data_structures/test_stack_students.cpp +++ b/data_structures/test_stack_students.cpp @@ -8,14 +8,15 @@ * ./main student.txt ************************************************************ * */ -#include -#include -#include -#include -#include -#include +#include // for assert +#include // for NAN +#include // for basic_ostream, basic_ifstream, basic_istream +#include // for operator<<, setprecision, _Setprecision +#include // for cout +#include // for char_traits, operator>>, basic_string, operat... +#include // for operator== -#include "./stack.hpp" +#include "./stack.hpp" // for stack int main(int argc, char* argv[]) { double GPA = NAN; diff --git a/data_structures/treap.cpp b/data_structures/treap.cpp index 5be3b2416..8170fa6c1 100644 --- a/data_structures/treap.cpp +++ b/data_structures/treap.cpp @@ -17,10 +17,11 @@ * @author [Kairao ZHENG](https://github.com/fgmn) */ -#include /// For array -#include /// For assert -#include /// for integral typedefs -#include /// For IO operations +#include // for rand +#include // for array +#include // for assert +#include // for INT32_MAX +#include // for operator<<, basic_ostream, cout /** * @namespace diff --git a/data_structures/tree.cpp b/data_structures/tree.cpp index f46c31ff2..980709bab 100644 --- a/data_structures/tree.cpp +++ b/data_structures/tree.cpp @@ -1,5 +1,7 @@ -#include -#include +#include // for NULL +#include // for operator<<, basic_ostream, char_traits, cout +#include // for list + using namespace std; struct node { @@ -90,30 +92,30 @@ int main() { cout << "\nEnter Your Choice : "; cin >> ch; switch (ch) { - case 1: - int x; - char pos; - cout << "\nEnter the value to be Inserted : "; - cin >> x; - cout << "\nLeft or Right of Root : "; - cin >> pos; - if (pos == 'l') - CreateTree(root, root->left, x, pos); - else if (pos == 'r') - CreateTree(root, root->right, x, pos); - break; - case 2: - BFT(root); - break; - case 3: - Pre(root); - break; - case 4: - In(root); - break; - case 5: - Post(root); - break; + case 1: + int x; + char pos; + cout << "\nEnter the value to be Inserted : "; + cin >> x; + cout << "\nLeft or Right of Root : "; + cin >> pos; + if (pos == 'l') + CreateTree(root, root->left, x, pos); + else if (pos == 'r') + CreateTree(root, root->right, x, pos); + break; + case 2: + BFT(root); + break; + case 3: + Pre(root); + break; + case 4: + In(root); + break; + case 5: + Post(root); + break; } } while (ch != 0); } diff --git a/data_structures/tree_234.cpp b/data_structures/tree_234.cpp index 46de03bfa..e87b42191 100644 --- a/data_structures/tree_234.cpp +++ b/data_structures/tree_234.cpp @@ -13,13 +13,14 @@ Introduction](https://www.educative.io/page/5689413791121408/80001) * Other operations should be easy to be added. * @author [liuhuan](https://github.com/fedom) */ -#include /// for std::array -#include /// for assert -#include /// for std::ofstream -#include /// for std::cout -#include /// for std::unique_ptr -#include /// for std::queue -#include /// for std::to_string +#include // for int64_t, int8_t, int16_t +#include // for array +#include // for assert +#include // for basic_ostream, operator<<, basic_ofstream, ofstream +#include // for cout +#include // for allocator, unique_ptr +#include // for queue +#include // for char_traits, basic_string, operator+, stoi, to_s... /** * @namespace data_structures diff --git a/data_structures/trie_modern.cpp b/data_structures/trie_modern.cpp index 470af07a4..b0da638e8 100644 --- a/data_structures/trie_modern.cpp +++ b/data_structures/trie_modern.cpp @@ -4,9 +4,12 @@ * @author Anmol3299 * \brief A basic implementation of trie class to store only lower-case strings. */ -#include // for io operations -#include // for std::shared_ptr<> -#include // for std::string class +#include // for size_t +#include // for basic_ostream, operator<<, cout +#include // for shared_ptr, __shared_ptr_access, make_shared +#include // for runtime_error +#include // for basic_string, char_traits, string +#include // for move /** * A basic implementation of trie class to store only lower-case strings. diff --git a/data_structures/trie_tree.cpp b/data_structures/trie_tree.cpp index e966b2dfd..1c852f210 100644 --- a/data_structures/trie_tree.cpp +++ b/data_structures/trie_tree.cpp @@ -7,12 +7,13 @@ * @note the function ::data_structure::trie::deleteString might be erroneous * @see trie_modern.cpp */ -#include -#include -#include -#include -#include -#include +#include // for uint8_t +#include // for array +#include // for assert +#include // for operator<<, basic_ostream, cout, basic_ostream::... +#include // for shared_ptr, __shared_ptr_access +#include // for char_traits, basic_string, string +#include // for exit, EXIT_FAILURE /** \namespace data_structures * \brief Data-structure algorithms diff --git a/data_structures/trie_using_hashmap.cpp b/data_structures/trie_using_hashmap.cpp index 5fb9602a9..b043720fd 100644 --- a/data_structures/trie_using_hashmap.cpp +++ b/data_structures/trie_using_hashmap.cpp @@ -10,12 +10,14 @@ * in trie. * @see trie_modern.cpp for difference */ -#include /// for assert -#include /// for IO operations -#include /// for std::shared_ptr -#include /// for std::stack -#include /// for std::unordered_map -#include /// for std::vector +#include // for assert +#include // for basic_ostream, operator<<, char_traits, cout +#include // for shared_ptr, __shared_ptr_access, make_shared +#include // for stack +#include // for unordered_map, operator==, _Node_iterator_base +#include // for vector +#include // for basic_string, string, operator<< +#include // for pair /** * @namespace data_structures