From 1eea787f5ed9cfd92fcc42a78a1239079f016981 Mon Sep 17 00:00:00 2001 From: Vinaya S Rao <41911422+vinayassrao@users.noreply.github.com> Date: Thu, 26 Aug 2021 19:29:30 +0530 Subject: [PATCH] chore(CPlusPlus): add algorithms to linked lists and tree (#425) --- .../Linked-Lists/Cycle-Detection.cpp | 69 ++++++++++ .../Linked-Lists/Find-Merge-Point.cpp | 125 ++++++++++++++++++ algorithms/CPlusPlus/README.md | 4 + algorithms/CPlusPlus/Trees/Height-Of-Tree.cpp | 58 ++++++++ .../CPlusPlus/Trees/Top-View-Of-A-Tree.cpp | 93 +++++++++++++ 5 files changed, 349 insertions(+) create mode 100644 algorithms/CPlusPlus/Linked-Lists/Cycle-Detection.cpp create mode 100644 algorithms/CPlusPlus/Linked-Lists/Find-Merge-Point.cpp create mode 100644 algorithms/CPlusPlus/Trees/Height-Of-Tree.cpp create mode 100644 algorithms/CPlusPlus/Trees/Top-View-Of-A-Tree.cpp diff --git a/algorithms/CPlusPlus/Linked-Lists/Cycle-Detection.cpp b/algorithms/CPlusPlus/Linked-Lists/Cycle-Detection.cpp new file mode 100644 index 00000000..6035e437 --- /dev/null +++ b/algorithms/CPlusPlus/Linked-Lists/Cycle-Detection.cpp @@ -0,0 +1,69 @@ +#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; +} + + +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; +} + + +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; +} + diff --git a/algorithms/CPlusPlus/Linked-Lists/Find-Merge-Point.cpp b/algorithms/CPlusPlus/Linked-Lists/Find-Merge-Point.cpp new file mode 100644 index 00000000..9598b10a --- /dev/null +++ b/algorithms/CPlusPlus/Linked-Lists/Find-Merge-Point.cpp @@ -0,0 +1,125 @@ +#include +using namespace std; + +//Defining a Class for creating nodes. +class SinglyLinkedListNode +{ + public: + int data; + SinglyLinkedListNode* next; +}; + + +//Function to find the merge point of two linked list + + +int findMergeNode(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) +{ + int k,n=0,m=0; + SinglyLinkedListNode* temp=head1,*temp1=head1,*temp2=head2; + + for(int i=0;temp!=NULL;i++)//finding the length of first linked list + { + n++; + temp=temp->next; + } + + temp=head2; + for(int i=0;temp!=NULL;i++)//finding the length of second linked list + { + m++; + temp=temp->next; + } + + k=abs(n-m); + + if(n==m) + { + for(int i=0;idata; + temp1=temp1->next; + temp2=temp2->next; + } + return 0; + } + else if(n>m) + { + for(int i=0;inext; + //temp2=temp2->next; + } + for(int i=0;temp1!=NULL && temp2!=NULL;i++) + { + if(temp1==temp2) + return temp1->data; + temp1=temp1->next; + temp2=temp2->next; + } + return 0; + } + else + { + for(int i=0;inext; + //temp2=temp2->next; + } + for(int i=0;temp1!=NULL && temp2!=NULL;i++) + { + if(temp1==temp2) + return temp1->data; + temp1=temp1->next; + temp2=temp2->next; + } + return 0; + } +} + + + + + +int main() +{ + /* + Create two linked lists + + 1st 3->6->9->15->30 + 2nd 10->15->30 + + 15 is the intersection point + */ + + SinglyLinkedListNode* newNode; + + // Addition of new nodes + SinglyLinkedListNode* head1 = new SinglyLinkedListNode(); + head1->data = 10; + + SinglyLinkedListNode* head2 = new SinglyLinkedListNode(); + head2->data = 3; + + newNode = new SinglyLinkedListNode(); + newNode->data = 6; + head2->next = newNode; + + newNode = new SinglyLinkedListNode(); + newNode->data = 9; + head2->next->next = newNode; + + newNode = new SinglyLinkedListNode(); + newNode->data = 15; + head1->next = newNode; + head2->next->next->next = newNode; + + newNode = new SinglyLinkedListNode(); + newNode->data = 30; + head1->next->next = newNode; + + head1->next->next->next = NULL; + + cout << "The node of intersection is : " << findMergeNode(head1, head2); +} \ No newline at end of file diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 4787c9c6..f28fe6a7 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -42,6 +42,8 @@ 6. [Reversing a linked lists](Linked-Lists/reverse.cpp) 7. [Merging two sorted linked lists](Linked-Lists/merge.cpp) 8. [Reorder List](Linked-Lists/Reorder-List.cpp) +9. [Detecting cycle in a singly linked list](Linked-Lists/Cycle-Detection.cpp) +10. [Find Merge Point of two singly linked list](Linked-Lists/Find-Merge-Point.cpp) ## Searching @@ -98,6 +100,8 @@ 7. [In order Predecessor and Successor](Trees/in-order-predecessor-and-successor.cpp) 8. [Avl Tree](Trees/avl.cpp) 9. [Min Heap](Trees/min-heap.cpp) +10. [Finding the height of a given tree](Trees/Height-Of-Tree.cpp) +11. [Finding the elements of a tree visible from top view](Trees/Top-View-Of-A-Tree.cpp) # Maths diff --git a/algorithms/CPlusPlus/Trees/Height-Of-Tree.cpp b/algorithms/CPlusPlus/Trees/Height-Of-Tree.cpp new file mode 100644 index 00000000..d8896507 --- /dev/null +++ b/algorithms/CPlusPlus/Trees/Height-Of-Tree.cpp @@ -0,0 +1,58 @@ +#include +using namespace std; + + +// Basic Structure of a Tree +struct Node { + int data; + struct Node *left; + struct Node *right; + + Node(int val) { + data = val; + left = NULL; + right = NULL; + } +}; + + +//Function to find the height of the tree +int height(struct Node* root) +{ + if (root == NULL) + return -1; + else + { + /* compute the depth of each subtree */ + int lDepth = height(root->left); + int rDepth = height(root->right); + + /* use the larger one */ + return max(lDepth, rDepth) + 1; + } + +} + + +int main() +{ + Node* root = new Node(1); + root->left = new Node(2); + root->right = new Node(3); + root->left->left = new Node(4); + root->left->right = new Node(5); + root->right->right = new Node(7); + + /* + 1----height=2 + / \ + 2 3----height=1 + / \ \ + 4 5 7----height=0 + Expected-> height=2 + + */ + cout << "Height of the tree is : " << height(root) << endl;; + + return 0; +} \ No newline at end of file diff --git a/algorithms/CPlusPlus/Trees/Top-View-Of-A-Tree.cpp b/algorithms/CPlusPlus/Trees/Top-View-Of-A-Tree.cpp new file mode 100644 index 00000000..9d8cef38 --- /dev/null +++ b/algorithms/CPlusPlus/Trees/Top-View-Of-A-Tree.cpp @@ -0,0 +1,93 @@ +#include +#include +using namespace std; + + +// Basic Structure of a Tree +struct Node +{ + int data; + struct Node *left; + struct Node *right; + + Node(int val) { + data = val; + left = NULL; + right = NULL; + } +}; + + + + + + + +//Function which prints out the top view of the tree + +void topView(struct Node * root) +{ + if (root == NULL) + return; + + queue>q; + map mp; + int d; + q.push({root, 0}); + Node *temp = NULL; + + while (!q.empty()) + { + temp = q.front().first; + d = q.front().second; + + + if (mp.find(d) == mp.end()) + { + //cout << temp->data <<" "; + mp[d] = temp->data; + } + + if (temp->left) + { + q.push({temp->left, d - 1}); //every node left to its predecessor is tagged a number less than one + + } + if (temp->right) + { + q.push({temp->right, d + 1}); //every node right to its predecessor is tagged a number greater than one + + } + q.pop(); + } + for (auto i = mp.begin(); i != mp.end(); i++) + cout << i->second << " "; + + + +} + + +int main() +{ + Node* root = new Node(1); + root->left = new Node(2); + root->right = new Node(3); + root->left->left = new Node(4); + root->left->right = new Node(5); + root->right->right = new Node(7); + + /* + 1 + / \ + 2 3 + / \ \ + 4 5 7 + Expected-> 4 2 1 3 7 + + */ + cout << "The top view of the tree is : " << endl; + topView(root); + + return 0; +} \ No newline at end of file