forked from Priyadarshan2000/Hacktoberfest_2k22
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5517fe1
commit 4920c17
Showing
92 changed files
with
8,005 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/*0 - 1 Knapsack Problem | ||
You are given weights and values of N items, put these items in a knapsack | ||
of capacity W to get the maximum total value in the knapsack. | ||
Note that we have only one quantity of each item. | ||
In other words, given two integer arrays val[0..N-1] and wt[0..N-1] | ||
which represent values and weights associated with N items respectively. | ||
Also given an integer W which represents knapsack capacity, | ||
find out the maximum value subset of val[] such that | ||
sum of the weights of this subset is smaller than or equal to W. | ||
You cannot break an item, either pick the complete item or don’t pick it (0-1 property)*/ | ||
|
||
#include<bits/stdc++.h> | ||
using namespace std; | ||
class Solution | ||
{ | ||
public: | ||
int knapSack(int W, int wt[], int val[], int n) | ||
{ | ||
int t[n+1][W+1]; | ||
|
||
for(int i=0;i<n+1;i++){ | ||
for(int j=0;j<W+1;j++){ | ||
if(i==0 || j==0) | ||
t[i][j] = 0; | ||
|
||
else if(wt[i-1] <= j) | ||
t[i][j] = max(val[i-1] + t[i-1][j-wt[i-1]], t[i-1][j]); | ||
|
||
else if(wt[i-1] > j) | ||
t[i][j] = t[i-1][j]; | ||
} | ||
} | ||
return t[n][W]; | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
int t; | ||
cout<<"Enter the number of test cases : "; | ||
cin>>t; | ||
while(t--) | ||
{ | ||
int n, w; | ||
cout<<"Enter the arrays' size : "; | ||
cout<<"Enter the size of the knapsack : "; | ||
cin>>n>>w; | ||
|
||
int val[n]; | ||
int wt[n]; | ||
|
||
for(int i=0;i<n;i++) | ||
cin>>val[i]; | ||
|
||
for(int i=0;i<n;i++) | ||
cin>>wt[i]; | ||
|
||
Solution obj; | ||
cout<<obj.knapSack(w, wt, val, n)<<endl; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Adjascency List representation in C | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
struct node { | ||
int vertex; | ||
struct node* next; | ||
}; | ||
struct node* createNode(int); | ||
|
||
struct Graph { | ||
int numVertices; | ||
struct node** adjLists; | ||
}; | ||
|
||
// Create a node | ||
struct node* createNode(int v) { | ||
struct node* newNode = malloc(sizeof(struct node)); | ||
newNode->vertex = v; | ||
newNode->next = NULL; | ||
return newNode; | ||
} | ||
|
||
// Create a graph | ||
struct Graph* createAGraph(int vertices) { | ||
struct Graph* graph = malloc(sizeof(struct Graph)); | ||
graph->numVertices = vertices; | ||
|
||
graph->adjLists = malloc(vertices * sizeof(struct node*)); | ||
|
||
int i; | ||
for (i = 0; i < vertices; i++) | ||
graph->adjLists[i] = NULL; | ||
|
||
return graph; | ||
} | ||
|
||
// Add edge | ||
void addEdge(struct Graph* graph, int s, int d) { | ||
// Add edge from s to d | ||
struct node* newNode = createNode(d); | ||
newNode->next = graph->adjLists[s]; | ||
graph->adjLists[s] = newNode; | ||
|
||
// Add edge from d to s | ||
newNode = createNode(s); | ||
newNode->next = graph->adjLists[d]; | ||
graph->adjLists[d] = newNode; | ||
} | ||
|
||
// Print the graph | ||
void printGraph(struct Graph* graph) { | ||
int v; | ||
for (v = 0; v < graph->numVertices; v++) { | ||
struct node* temp = graph->adjLists[v]; | ||
printf("\n Vertex %d\n: ", v); | ||
while (temp) { | ||
printf("%d -> ", temp->vertex); | ||
temp = temp->next; | ||
} | ||
printf("\n"); | ||
} | ||
} | ||
|
||
int main() { | ||
struct Graph* graph = createAGraph(4); | ||
addEdge(graph, 0, 1); | ||
addEdge(graph, 0, 2); | ||
addEdge(graph, 0, 3); | ||
addEdge(graph, 1, 2); | ||
|
||
printGraph(graph); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
// AVL tree implementation in C | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
// Create Node | ||
struct Node { | ||
int key; | ||
struct Node *left; | ||
struct Node *right; | ||
int height; | ||
}; | ||
|
||
int max(int a, int b); | ||
|
||
// Calculate height | ||
int height(struct Node *N) { | ||
if (N == NULL) | ||
return 0; | ||
return N->height; | ||
} | ||
|
||
int max(int a, int b) { | ||
return (a > b) ? a : b; | ||
} | ||
|
||
// Create a node | ||
struct Node *newNode(int key) { | ||
struct Node *node = (struct Node *) | ||
malloc(sizeof(struct Node)); | ||
node->key = key; | ||
node->left = NULL; | ||
node->right = NULL; | ||
node->height = 1; | ||
return (node); | ||
} | ||
|
||
// Right rotate | ||
struct Node *rightRotate(struct Node *y) { | ||
struct Node *x = y->left; | ||
struct Node *T2 = x->right; | ||
|
||
x->right = y; | ||
y->left = T2; | ||
|
||
y->height = max(height(y->left), height(y->right)) + 1; | ||
x->height = max(height(x->left), height(x->right)) + 1; | ||
|
||
return x; | ||
} | ||
|
||
// Left rotate | ||
struct Node *leftRotate(struct Node *x) { | ||
struct Node *y = x->right; | ||
struct Node *T2 = y->left; | ||
|
||
y->left = x; | ||
x->right = T2; | ||
|
||
x->height = max(height(x->left), height(x->right)) + 1; | ||
y->height = max(height(y->left), height(y->right)) + 1; | ||
|
||
return y; | ||
} | ||
|
||
// Get the balance factor | ||
int getBalance(struct Node *N) { | ||
if (N == NULL) | ||
return 0; | ||
return height(N->left) - height(N->right); | ||
} | ||
|
||
// Insert node | ||
struct Node *insertNode(struct Node *node, int key) { | ||
// Find the correct position to insertNode the node and insertNode it | ||
if (node == NULL) | ||
return (newNode(key)); | ||
|
||
if (key < node->key) | ||
node->left = insertNode(node->left, key); | ||
else if (key > node->key) | ||
node->right = insertNode(node->right, key); | ||
else | ||
return node; | ||
|
||
// Update the balance factor of each node and | ||
// Balance the tree | ||
node->height = 1 + max(height(node->left), | ||
height(node->right)); | ||
|
||
int balance = getBalance(node); | ||
if (balance > 1 && key < node->left->key) | ||
return rightRotate(node); | ||
|
||
if (balance < -1 && key > node->right->key) | ||
return leftRotate(node); | ||
|
||
if (balance > 1 && key > node->left->key) { | ||
node->left = leftRotate(node->left); | ||
return rightRotate(node); | ||
} | ||
|
||
if (balance < -1 && key < node->right->key) { | ||
node->right = rightRotate(node->right); | ||
return leftRotate(node); | ||
} | ||
|
||
return node; | ||
} | ||
|
||
struct Node *minValueNode(struct Node *node) { | ||
struct Node *current = node; | ||
|
||
while (current->left != NULL) | ||
current = current->left; | ||
|
||
return current; | ||
} | ||
|
||
// Delete a nodes | ||
struct Node *deleteNode(struct Node *root, int key) { | ||
// Find the node and delete it | ||
if (root == NULL) | ||
return root; | ||
|
||
if (key < root->key) | ||
root->left = deleteNode(root->left, key); | ||
|
||
else if (key > root->key) | ||
root->right = deleteNode(root->right, key); | ||
|
||
else { | ||
if ((root->left == NULL) || (root->right == NULL)) { | ||
struct Node *temp = root->left ? root->left : root->right; | ||
|
||
if (temp == NULL) { | ||
temp = root; | ||
root = NULL; | ||
} else | ||
*root = *temp; | ||
free(temp); | ||
} else { | ||
struct Node *temp = minValueNode(root->right); | ||
|
||
root->key = temp->key; | ||
|
||
root->right = deleteNode(root->right, temp->key); | ||
} | ||
} | ||
|
||
if (root == NULL) | ||
return root; | ||
|
||
// Update the balance factor of each node and | ||
// balance the tree | ||
root->height = 1 + max(height(root->left), | ||
height(root->right)); | ||
|
||
int balance = getBalance(root); | ||
if (balance > 1 && getBalance(root->left) >= 0) | ||
return rightRotate(root); | ||
|
||
if (balance > 1 && getBalance(root->left) < 0) { | ||
root->left = leftRotate(root->left); | ||
return rightRotate(root); | ||
} | ||
|
||
if (balance < -1 && getBalance(root->right) <= 0) | ||
return leftRotate(root); | ||
|
||
if (balance < -1 && getBalance(root->right) > 0) { | ||
root->right = rightRotate(root->right); | ||
return leftRotate(root); | ||
} | ||
|
||
return root; | ||
} | ||
|
||
// Print the tree | ||
void printPreOrder(struct Node *root) { | ||
if (root != NULL) { | ||
printf("%d ", root->key); | ||
printPreOrder(root->left); | ||
printPreOrder(root->right); | ||
} | ||
} | ||
|
||
int main() { | ||
struct Node *root = NULL; | ||
|
||
root = insertNode(root, 2); | ||
root = insertNode(root, 1); | ||
root = insertNode(root, 7); | ||
root = insertNode(root, 4); | ||
root = insertNode(root, 5); | ||
root = insertNode(root, 3); | ||
root = insertNode(root, 8); | ||
|
||
printPreOrder(root); | ||
|
||
root = deleteNode(root, 3); | ||
|
||
printf("\nAfter deletion: "); | ||
printPreOrder(root); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.