chore(CPlusPlus): add algorithms to linked lists and tree (#425)

pull/432/head
Vinaya S Rao 2021-08-26 19:29:30 +05:30 committed by GitHub
parent 94c823596a
commit 1eea787f5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 349 additions and 0 deletions

View File

@ -0,0 +1,69 @@
#include <bits/stdc++.h>
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<Node*> 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;
}

View File

@ -0,0 +1,125 @@
#include <bits/stdc++.h>
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;i<n;i++)
{
if(temp1==temp2)
return temp1->data;
temp1=temp1->next;
temp2=temp2->next;
}
return 0;
}
else if(n>m)
{
for(int i=0;i<k;i++)
{
temp1=temp1->next;
//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;i<k;i++)
{
temp2=temp2->next;
//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);
}

View File

@ -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

View File

@ -0,0 +1,58 @@
#include<iostream>
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;
}

View File

@ -0,0 +1,93 @@
#include<bits/stdc++.h>
#include<iostream>
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<pair<struct Node*, int>>q;
map<int, int> 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;
}