Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Longest Common Subsequence #115

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions avl-tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@

// C++ program to insert a node in AVL tree
#include <bits/stdc++.h>
using namespace std;

class Node {
public:
int key;
Node *left;
Node *right;
int height;
};

int max(int a, int b);

int height(Node *N) {
if(N == NULL)
return 0;
return N->height;
}

int max(int a, int b) {
return ((a>b)? a : b);

}

Node *newNode(int key) {
Node* node = new Node();
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1; //new node is initially added at leaf

return (node);
}

Node *rightRotate(Node *y) {
Node *x = y->left;
Node *T2 = x->right;

//Perform rotation
x->right = y;
y->left = T2;

//Update heights
y->height = max(height(y->left), height(y->right)) +1;
x->height = max(height(x->left), height(x->right)) +1;

return x;
}

Node *leftRotate(Node *x) {
Node *y = x->right;
Node *T2 = y->left;

//Perform rotation
y->left = x;
x->right = T2;

//Update heights
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;

return y;
}

int getBalance(Node *N) {
if(N == NULL)
return 0;
return height(N->left) - height(N->right);
}

// Recursive function to insert a key in the subtree rooted with node and returns the new root of the subtree.
Node* insert(Node* node, int key) {
// 1. Perform the BST insertion
if(node == NULL)
return (newNode(key));

if(key < node->key)
node->left = insert(node->left, key);
else if(key > node->key)
node->right = insert(node->right, key);
else //Equal keys are not allowed in BST
return node;

//2. Update height of this ancestor node
node->height = 1+ max(height(node->left), height(node->right));

//3. Get the balance factor of this ancestor node to check whether this node became unbalanced
int balance = getBalance(node);

//If this node becomes unbalanced, then there are 4 cases

//Left Left Case
if (balance > 1 && key < node->left->key)
return rightRotate(node);

//Right Right Case
if(balance < -1 && key > node->right->key)
return leftRotate(node);

//Left Right Case
if(balance > 1 && key > node->right->key) {
node->left = leftRotate(node->left);
return rightRotate(node);
}

//Right Left Case
if (balance < -1 && key < node->right->key){
node->right = rightRotate(node->right);
return leftRotate(node);
}

return node;
}

void preOrder(Node *root) {
if(root != NULL) {
cout << root->key <<" ";
preOrder(root->left);
preOrder(root->right);
}
}

int main(void) {
Node *root = NULL;

root = insert(root, 10);
root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 40);
root = insert(root, 50);
root = insert(root, 25);

/* The constructed AVL Tree would be
30
/ \
20 40
/ \ \
10 25 50
*/
cout << "Preorder traversal of the "
"constructed AVL tree is \n";
preOrder(root);

return 0;
}
123 changes: 123 additions & 0 deletions inorder_traversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@


// RECURSION METHOD
// #include <iostream>
// using namespace std;

// struct Node {
// int data;
// Node *left, *right;

// Node (int data) {
// this->data = data;
// this->left = this->right = nullptr;
// }
// };
// // recursion function
// void inorder(Node* root) {
// if (root == nullptr)
// return;

// inorder(root->left);

// cout << root -> data << " ";

// inorder(root->right);
// }

// int main() {
// /* Construct the following tree
// 1
// / \
// / \
// 2 3
// / / \
// / / \
// 4 5 6
// / \
// / \
// 7 8
// */

// Node* root = new Node(1);
// root->left = new Node(2);
// root->right = new Node(3);
// root->left->left = new Node(4);
// root->right->left = new Node(4);
// root->right->right = new Node(4);
// root->right->left->left = new Node(4);
// root->right->left->right = new Node(4);

// inorder(root);

// return 0;
// }




//**************************************************************
// Iterative method

#include <iostream>
#include <stack>
using namespace std;

struct Node {
int data;
Node *left, *right;

Node(int data) {
this->data = data;
this->left = this->right = nullptr;
}
};

void inorderIterative (Node* root) {
stack<Node*> stack;

Node* curr = root;

while (!stack.empty() || curr != nullptr) {

if (curr != nullptr) {
stack.push(curr) ;
curr = curr->left;
}
else {
curr = stack.top();
stack.pop();
cout << curr->data << " ";
curr = curr-> right;
}
}
}

int main()
{
/* Construct the following tree
1
/ \
/ \
2 3
/ / \
/ / \
4 5 6
/ \
/ \
7 8
*/

Node *root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->right->left = new Node(5);
root->right->right = new Node(6);
root->right->left->left = new Node(7);
root->right->left->right = new Node(8);

inorderIterative(root);

return 0;
}
73 changes: 73 additions & 0 deletions longestCommonSubsequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// https: //www.techiedelight.com/longest-common-subsequence/

// Longest Common Subsequence Problem


//without dp : complexity O(2^(m+n)) where m, n are length of two strings

// #include <iostream>
// #include <string>
// using namespace std;

// int LCSLength(string x, string y, int m, int n) {

// if (m == 0 || n == 0)
// return 0;

// if (x[m-1] == y[n-1]) {
// return LCSLength(x, y, m-1, n-1) + 1;
// }

// return max( LCSLength(x,y,m,n-1), LCSLength(x,y,m-1,n)) ;
// }

// int main(void) {
// string x = "abcbdab", y = "bdcaaba";

// cout << "The length of the LCS is " << LCSLength(x,y,x.length(), y.length());

// return 0;
// }

//*************************************************************
// Using DP:

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;

// int lcslength(string x, string y, int m, int n, auto &lookup) {
int lcslength(string x, string y, int m, int n, unordered_map<string,int> &lookup) {
if (m == 0 || n == 0)
return 0;

string key = to_string(m) + "|" + to_string(n);

if (lookup.find(key) == lookup.end()) {
if (x[m-1] == y[n-1])
lookup[key] = lcslength(x,y, m-1, n-1, lookup) +1;

else
{
lookup[key] = max(lcslength(x,y, m, n-1, lookup),
lcslength(x,y,m-1, n, lookup));

}

}

//return the subproblem solution from the map
return lookup[key];
}

int main(void) {
string x = "abcbdab", y = "bdcaba";

unordered_map<string, int> lookup;

cout << "The length of the LCS is "
<< lcslength(x,y,x.length(), y.length(), lookup);

return 0;
}