diff --git a/BinaryTree.cpp b/BinaryTree.cpp new file mode 100644 index 0000000..823ea2c --- /dev/null +++ b/BinaryTree.cpp @@ -0,0 +1,77 @@ +// Program to print binary tree in 2D +#include +#define COUNT 10 + +// A binary tree node +struct Node +{ + int data; + Node* left, *right; +}; + +// Helper function to allocates a new node +Node* newNode(int data) +{ + Node* node = new Node; + node->data = data; + node->left = node->right = NULL; + return node; +} + +// Function to print binary tree in 2D +// It does reverse inorder traversal +void print2DUtil(Node *root, int space) +{ + // Base case + if (root == NULL) + return; + + // Increase distance between levels + space += COUNT; + + // Process right child first + print2DUtil(root->right, space); + + // Print current node after space + // count + printf("\n"); + for (int i = COUNT; i < space; i++) + printf(" "); + printf("%d\n", root->data); + + // Process left child + print2DUtil(root->left, space); +} + +// Wrapper over print2DUtil() +void print2D(Node *root) +{ + // Pass initial space count as 0 + print2DUtil(root, 0); +} + +// Driver program to test above functions +int main() +{ + Node *root = newNode(1); + root->left = newNode(2); + root->right = newNode(3); + + root->left->left = newNode(4); + root->left->right = newNode(5); + root->right->left = newNode(6); + root->right->right = newNode(7); + + root->left->left->left = newNode(8); + root->left->left->right = newNode(9); + root->left->right->left = newNode(10); + root->left->right->right = newNode(11); + root->right->left->left = newNode(12); + root->right->left->right = newNode(13); + root->right->right->left = newNode(14); + root->right->right->right = newNode(15); + + print2D(root); + + return 0; +} diff --git a/LinkedListLoop.cpp b/LinkedListLoop.cpp new file mode 100644 index 0000000..bc77532 --- /dev/null +++ b/LinkedListLoop.cpp @@ -0,0 +1,71 @@ +// C++ program to detect loop in a linked list +#include +using namespace std; + +/* Link list node */ +struct Node +{ + int data; + struct Node* next; +}; + +void push(struct Node** head_ref, int new_data) +{ + /* allocate node */ + struct Node* new_node = new Node; + + /* put in the data */ + new_node->data = new_data; + + /* link the old list off the new node */ + new_node->next = (*head_ref); + + /* move the head to point to the new node */ + (*head_ref) = new_node; +} + +// Returns true if there is a loop in linked list +// else returns false. +bool detectLoop(struct Node *h) +{ + unordered_set s; + while (h != NULL) + { + // If this node is already present + // in hashmap it means there is a cycle + // (Because you we encountering the + // node for the second time). + if (s.find(h) != s.end()) + return true; + + // If we are seeing the node for + // the first time, insert it in hash + s.insert(h); + + h = h->next; + } + + return false; +} + +/* Drier program to test above function*/ +int main() +{ + /* Start with the empty list */ + struct Node* head = NULL; + + push(&head, 20); + push(&head, 4); + push(&head, 15); + push(&head, 10); + + /* Create a loop for testing */ + head->next->next->next->next = head; + + if (detectLoop(head)) + cout << "Loop found"; + else + cout << "No Loop"; + + return 0; +}