diff --git a/data_structures/avltree.cpp b/data_structures/avltree.cpp index f38133c05..836f0f667 100644 --- a/data_structures/avltree.cpp +++ b/data_structures/avltree.cpp @@ -1,8 +1,15 @@ +/** + * \file + * \brief A simple tree implementation using nodes + * + * \todo update code to use C++ STL library features and OO structure + * \warning This program is a poor implementation and does not utilize any of + * the C++ STL features. + */ +#include #include #include -using namespace std; - typedef struct node { int data; int height; @@ -10,10 +17,7 @@ typedef struct node { struct node *right; } node; -int max(int a, int b) { return a > b ? a : b; } - -// Returns a new Node - +/** Create and return a new Node */ node *createNode(int data) { node *nn = new node(); nn->data = data; @@ -23,20 +27,17 @@ node *createNode(int data) { return nn; } -// Returns height of tree - +/** Returns height of tree */ int height(node *root) { if (root == NULL) return 0; - return 1 + max(height(root->left), height(root->right)); + return 1 + std::max(height(root->left), height(root->right)); } -// Returns difference between height of left and right subtree - +/** Returns difference between height of left and right subtree */ int getBalance(node *root) { return height(root->left) - height(root->right); } -// Returns Node after Right Rotation - +/** Returns Node after Right Rotation */ node *rightRotate(node *root) { node *t = root->left; node *u = t->right; @@ -45,8 +46,7 @@ node *rightRotate(node *root) { return t; } -// Returns Node after Left Rotation - +/** Returns Node after Left Rotation */ node *leftRotate(node *root) { node *t = root->right; node *u = t->left; @@ -55,16 +55,14 @@ node *leftRotate(node *root) { return t; } -// Returns node with minimum value in the tree - +/** Returns node with minimum value in the tree */ node *minValue(node *root) { if (root->left == NULL) return root; return minValue(root->left); } -// Balanced Insertion - +/** Balanced Insertion */ node *insert(node *root, int item) { node *nn = createNode(item); if (root == NULL) @@ -86,8 +84,7 @@ node *insert(node *root, int item) { return root; } -// Balanced Deletion - +/** Balanced Deletion */ node *deleteNode(node *root, int key) { if (root == NULL) return root; @@ -118,14 +115,13 @@ node *deleteNode(node *root, int key) { return root; } -// LevelOrder (Breadth First Search) - +/** LevelOrder (Breadth First Search) */ void levelOrder(node *root) { - queue q; + std::queue q; q.push(root); while (!q.empty()) { root = q.front(); - cout << root->data << " "; + std::cout << root->data << " "; q.pop(); if (root->left) q.push(root->left); @@ -134,18 +130,19 @@ void levelOrder(node *root) { } } +/** Main function */ int main() { // Testing AVL Tree node *root = NULL; int i; for (i = 1; i <= 7; i++) root = insert(root, i); - cout << "LevelOrder: "; + std::cout << "LevelOrder: "; levelOrder(root); root = deleteNode(root, 1); // Deleting key with value 1 - cout << "\nLevelOrder: "; + std::cout << "\nLevelOrder: "; levelOrder(root); root = deleteNode(root, 4); // Deletin key with value 4 - cout << "\nLevelOrder: "; + std::cout << "\nLevelOrder: "; levelOrder(root); return 0; } diff --git a/data_structures/binary_search_tree.cpp b/data_structures/binary_search_tree.cpp index c3a74f385..ebcf2754e 100644 --- a/data_structures/binary_search_tree.cpp +++ b/data_structures/binary_search_tree.cpp @@ -1,5 +1,12 @@ +/** + * \file + * \brief A simple tree implementation using structured nodes + * + * \todo update code to use C++ STL library features and OO structure + * \warning This program is a poor implementation - C style - and does not + * utilize any of the C++ STL features. + */ #include -using namespace std; struct node { int val; @@ -84,7 +91,7 @@ void Remove(node *p, node *n, int x) { void BFT(node *n) { if (n != NULL) { - cout << n->val << " "; + std::cout << n->val << " "; enqueue(n->left); enqueue(n->right); BFT(dequeue()); @@ -93,7 +100,7 @@ void BFT(node *n) { void Pre(node *n) { if (n != NULL) { - cout << n->val << " "; + std::cout << n->val << " "; Pre(n->left); Pre(n->right); } @@ -102,7 +109,7 @@ void Pre(node *n) { void In(node *n) { if (n != NULL) { In(n->left); - cout << n->val << " "; + std::cout << n->val << " "; In(n->right); } } @@ -111,7 +118,7 @@ void Post(node *n) { if (n != NULL) { Post(n->left); Post(n->right); - cout << n->val << " "; + std::cout << n->val << " "; } } @@ -121,45 +128,47 @@ int main() { int value; int ch; node *root = new node; - cout << "\nEnter the value of root node :"; - cin >> value; + std::cout << "\nEnter the value of root node :"; + std::cin >> value; root->val = value; root->left = NULL; root->right = NULL; do { - cout << "\n1. Insert"; - cout << "\n2. Delete"; - cout << "\n3. Breadth First"; - cout << "\n4. Preorder Depth First"; - cout << "\n5. Inorder Depth First"; - cout << "\n6. Postorder Depth First"; + std::cout << "\n1. Insert" + << "\n2. Delete" + << "\n3. Breadth First" + << "\n4. Preorder Depth First" + << "\n5. Inorder Depth First" + << "\n6. Postorder Depth First"; - cout << "\nEnter Your Choice : "; - cin >> ch; + std::cout << "\nEnter Your Choice : "; + std::cin >> ch; int x; switch (ch) { - case 1: - cout << "\nEnter the value to be Inserted : "; - cin >> x; - Insert(root, x); - break; - case 2: - cout << "\nEnter the value to be Deleted : "; - 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); + + return 0; } diff --git a/data_structures/binaryheap.cpp b/data_structures/binaryheap.cpp index a31d87674..72e56e91a 100644 --- a/data_structures/binaryheap.cpp +++ b/data_structures/binaryheap.cpp @@ -1,48 +1,52 @@ -// A C++ program to demonstrate common Binary Heap Operations +/** + * \file + * \brief A C++ program to demonstrate common Binary Heap Operations + */ #include #include -using namespace std; +#include -// Prototype of a utility function to swap two integers -void swap(int *x, int *y); - -// A class for Min Heap +/** A class for Min Heap */ class MinHeap { - int *harr; // pointer to array of elements in heap - int capacity; // maximum possible size of min heap - int heap_size; // Current number of elements in min heap + int *harr; ///< pointer to array of elements in heap + int capacity; ///< maximum possible size of min heap + int heap_size; ///< Current number of elements in min heap + public: - // Constructor + /** Constructor + * \param[in] capacity initial heap capacity + */ MinHeap(int capacity); - // to heapify a subtree with the root at given index + /** to heapify a subtree with the root at given index + */ void MinHeapify(int); int parent(int i) { return (i - 1) / 2; } - // to get index of left child of node at index i + /** to get index of left child of node at index i */ int left(int i) { return (2 * i + 1); } - // to get index of right child of node at index i + /** to get index of right child of node at index i */ int right(int i) { return (2 * i + 2); } - // to extract the root which is the minimum element + /** to extract the root which is the minimum element */ int extractMin(); - // Decreases key value of key at index i to new_val + /** Decreases key value of key at index i to new_val */ void decreaseKey(int i, int new_val); - // Returns the minimum key (key at root) from min heap + /** Returns the minimum key (key at root) from min heap */ int getMin() { return harr[0]; } - // Deletes a key stored at index i + /** Deletes a key stored at index i */ void deleteKey(int i); - // Inserts a new key 'k' + /** Inserts a new key 'k' */ void insertKey(int k); }; -// Constructor: Builds a heap from a given array a[] of given size +/** Constructor: Builds a heap from a given array a[] of given size */ MinHeap::MinHeap(int cap) { heap_size = 0; capacity = cap; @@ -52,7 +56,7 @@ MinHeap::MinHeap(int cap) { // Inserts a new key 'k' void MinHeap::insertKey(int k) { if (heap_size == capacity) { - cout << "\nOverflow: Could not insertKey\n"; + std::cout << "\nOverflow: Could not insertKey\n"; return; } @@ -63,17 +67,18 @@ void MinHeap::insertKey(int k) { // Fix the min heap property if it is violated while (i != 0 && harr[parent(i)] > harr[i]) { - swap(&harr[i], &harr[parent(i)]); + std::swap(harr[i], harr[parent(i)]); i = parent(i); } } -// Decreases value of key at index 'i' to new_val. It is assumed that -// new_val is smaller than harr[i]. +/** Decreases value of key at index 'i' to new_val. It is assumed that new_val + * is smaller than harr[i]. + */ void MinHeap::decreaseKey(int i, int new_val) { harr[i] = new_val; while (i != 0 && harr[parent(i)] > harr[i]) { - swap(&harr[i], &harr[parent(i)]); + std::swap(harr[i], harr[parent(i)]); i = parent(i); } } @@ -96,15 +101,17 @@ int MinHeap::extractMin() { return root; } -// This function deletes key at index i. It first reduced value to minus -// infinite, then calls extractMin() +/** This function deletes key at index i. It first reduced value to minus + * infinite, then calls extractMin() + */ void MinHeap::deleteKey(int i) { decreaseKey(i, INT_MIN); extractMin(); } -// A recursive method to heapify a subtree with the root at given index -// This method assumes that the subtrees are already heapified +/** A recursive method to heapify a subtree with the root at given index + * This method assumes that the subtrees are already heapified + */ void MinHeap::MinHeapify(int i) { int l = left(i); int r = right(i); @@ -114,18 +121,11 @@ void MinHeap::MinHeapify(int i) { if (r < heap_size && harr[r] < harr[smallest]) smallest = r; if (smallest != i) { - swap(&harr[i], &harr[smallest]); + std::swap(harr[i], harr[smallest]); MinHeapify(smallest); } } -// A utility function to swap two elements -void swap(int *x, int *y) { - int temp = *x; - *x = *y; - *y = temp; -} - // Driver program to test above functions int main() { MinHeap h(11); @@ -136,9 +136,9 @@ int main() { h.insertKey(5); h.insertKey(4); h.insertKey(45); - cout << h.extractMin() << " "; - cout << h.getMin() << " "; + std::cout << h.extractMin() << " "; + std::cout << h.getMin() << " "; h.decreaseKey(2, 1); - cout << h.getMin(); + std::cout << h.getMin(); return 0; } diff --git a/data_structures/circular_queue_using_linked_list.cpp b/data_structures/circular_queue_using_linked_list.cpp index b6a471044..5d6484ce3 100644 --- a/data_structures/circular_queue_using_linked_list.cpp +++ b/data_structures/circular_queue_using_linked_list.cpp @@ -1,5 +1,4 @@ #include -using namespace std; struct node { int data; @@ -48,11 +47,10 @@ class Queue { node *ptr; ptr = front; do { - cout << ptr->data << " "; + std::cout << ptr->data << " "; ptr = ptr->next; } while (ptr != rear->next); - cout << front->data; - cout << endl; + std::cout << front->data << std::endl; } }; int main(void) { diff --git a/data_structures/linkedlist_implentation_usingarray.cpp b/data_structures/linkedlist_implentation_usingarray.cpp index a078bef16..837d9f65b 100644 --- a/data_structures/linkedlist_implentation_usingarray.cpp +++ b/data_structures/linkedlist_implentation_usingarray.cpp @@ -1,17 +1,23 @@ -/* The difference between the pointer implementation of linked list and array - implementation of linked list: - 1. The NULL is represented by -1; - 2. Limited size. (in the following case it is 100 nodes at max). But we can - reuse the nodes that are to be deleted by again linking it bacj to the list. -*/ +/** + * \file + * \brief Linked list implementation using Arrays + * + * The difference between the pointer implementation of linked list and array + * implementation of linked list: + * 1. The NULL is represented by -1; + * 2. Limited size. (in the following case it is 100 nodes at max). But we can + * reuse the nodes that are to be deleted by again linking it bacj to the list. + */ #include -using namespace std; + struct Node { int data; int next; }; -Node AvailArray[100]; // array that will act as nodes of a linked list. + +Node AvailArray[100]; ///< array that will act as nodes of a linked list. + int head = -1; int avail = 0; void initialise_list() { @@ -21,26 +27,27 @@ void initialise_list() { AvailArray[99].next = -1; // indicating the end of the linked list. } -int getnode() // This will return the index of the first free node present in - // the avail list -{ +/** This will return the index of the first free node present in the avail list + */ +int getnode() { int NodeIndexToBeReturned = avail; avail = AvailArray[avail].next; return NodeIndexToBeReturned; } -void freeNode( - int nodeToBeDeleted) // This function when called will delete the node with - // the index presented as an argument, and will put - // back that node into the array. -{ +/** This function when called will delete the node with + * the index presented as an argument, and will put + * back that node into the array. + */ +void freeNode(int nodeToBeDeleted) { AvailArray[nodeToBeDeleted].next = avail; avail = nodeToBeDeleted; } -void insertAtTheBeginning(int data) // The function will insert the given data - // into the front of the linked list. -{ +/** The function will insert the given data + * into the front of the linked list. + */ +void insertAtTheBeginning(int data) { int newNode = getnode(); AvailArray[newNode].data = data; AvailArray[newNode].next = head; @@ -62,43 +69,46 @@ void insertAtTheEnd(int data) { void display() { int temp = head; while (temp != -1) { - cout << AvailArray[temp].data << "->"; + std::cout << AvailArray[temp].data << "->"; temp = AvailArray[temp].next; } - cout << "-1" << endl; - ; + std::cout << "-1" << std::endl; } +/** Main function */ int main() { initialise_list(); int x, y, z; for (;;) { - cout << "1. Insert At The Beginning" << endl; - cout << "2. Insert At The End" << endl; - cout << "3. Display" << endl; - cout << "4.Exit" << endl; - cout << "Enter Your choice" << endl; - cin >> z; + std::cout << "1. Insert At The Beginning" << std::endl; + std::cout << "2. Insert At The End" << std::endl; + std::cout << "3. Display" << std::endl; + std::cout << "4.Exit" << std::endl; + std::cout << "Enter Your choice" << std::endl; + std::cin >> z; switch (z) { - case 1: - cout << "Enter the number you want to enter" << endl; - cin >> x; - insertAtTheBeginning(x); - break; - case 2: - cout << "Enter the number you want to enter" << endl; - cin >> y; - insertAtTheEnd(y); - break; - case 3: - cout << "The linked list contains the following element in order" - << endl; - display(); - break; - case 4: - exit(0); - default: - cout << "The entered choice is not correct" << endl; + case 1: + std::cout << "Enter the number you want to enter" << std::endl; + std::cin >> x; + insertAtTheBeginning(x); + break; + case 2: + std::cout << "Enter the number you want to enter" << std::endl; + std::cin >> y; + insertAtTheEnd(y); + break; + case 3: + std::cout + << "The linked list contains the following element in order" + << std::endl; + display(); + break; + case 4: + return 0; + default: + std::cout << "The entered choice is not correct" << std::endl; } } + + return 0; } diff --git a/data_structures/trie_modern.cpp b/data_structures/trie_modern.cpp index 218c90b68..c2eba30e0 100644 --- a/data_structures/trie_modern.cpp +++ b/data_structures/trie_modern.cpp @@ -1,8 +1,8 @@ /** - * Copyright 2020 @author Anmol3299 * @file * - * A basic implementation of trie class to store only lower-case strings. + * Copyright 2020 @author Anmol3299 + * \brief A basic implementation of trie class to store only lower-case strings. */ #include // for io operations #include // for std::shared_ptr<> @@ -35,7 +35,8 @@ class Trie { * Function to check if a node has some children which can form words. * @param node whose character array of pointers need to be checked for * children. - * @return if a child is found, it returns @ true, else it returns @ false. + * @return `true` if a child is found + * @return `false` if a child is not found */ inline static bool hasChildren(std::shared_ptr node) { for (size_t i = 0; i < ALPHABETS; i++) { @@ -98,7 +99,7 @@ class Trie { } public: - // constructor to initialise the root of the trie. + /// constructor to initialise the root of the trie. Trie() : m_root(std::make_shared()) {} /**