0% found this document useful (0 votes)
19 views21 pages

Data Structures and Algorithms

Uploaded by

Bruce Banner
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views21 pages

Data Structures and Algorithms

Uploaded by

Bruce Banner
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

NAME: VARUN SAPRA

B11+B12+B13

DATA STRUCTURES AND ALGORITHMS

TASK 1: HUFFMAN DECODING

Code:
#include<bits/stdc++.h>
using namespace std;

typedef struct node {


int freq;
char data;
node * left;
node * right;
} node;

struct deref:public binary_function<node*, node*, bool> {


bool operator()(const node * a, const node * b)const {
return a->freq > b->freq;
NAME: VARUN SAPRA
B11+B12+B13

}
};

typedef priority_queue<node *, vector<node*>, deref> spq;

node * huffman_hidden(string s) {

spq pq;
vector<int>count(256,0);

for(int i = 0; i < s.length(); i++ ) {


count[s[i]]++;
}

for(int i=0; i < 256; i++) {

node * n_node = new node;


n_node->left = NULL;
n_node->right = NULL;
n_node->data = (char)i;
n_node->freq = count[i];

if( count[i] != 0 )
pq.push(n_node);

while( pq.size() != 1 ) {

node * left = pq.top();


pq.pop();
node * right = pq.top();
pq.pop();
node * comb = new node;
comb->freq = left->freq + right->freq;
comb->data = '\0';
comb->left = left;
comb->right = right;
pq.push(comb);

}
NAME: VARUN SAPRA
B11+B12+B13

return pq.top();

void print_codes_hidden(node * root, string code, map<char, string>&mp) {

if(root == NULL)
return;

if(root->data != '\0') {
mp[root->data] = code;
}

print_codes_hidden(root->left, code+'0', mp);


print_codes_hidden(root->right, code+'1', mp);

void decode_huff(node * root,string s)


{
string ans = "";
node* n = root;
for(auto itr = s.begin(); itr != s.end();itr++){
node* next;
if(*itr == '0'){
next = n -> left;
}
else{
next = n -> right;
}
if(next -> data == '\0'){
n = next;
}
else{
ans += next -> data;
n = root;
}
}
cout << ans << endl;
}
NAME: VARUN SAPRA
B11+B12+B13

int main() {

string s;
std::cin >> s;

node * tree = huffman_hidden(s);


string code = "";
map<char, string>mp;

print_codes_hidden(tree, code, mp);

string coded;

for( int i = 0; i < s.length(); i++ ) {


coded += mp[s[i]];
}

decode_huff(tree,coded);

return 0;
}

TASK 2 : Binary Search Tree : Lowest Common Ancestor


NAME: VARUN SAPRA
B11+B12+B13

Code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

struct node {
int data;
struct node *left;
struct node *right;
};

struct node* create_node(int val){


if(val == -1){
return NULL;
}
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp->data=val;
temp->left=NULL;
temp->right=NULL;
return temp;
}

void inorder(struct node *root){


if(!root){
return;
}
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}

int max(int a, int b){


if(a>b){
return a;
} else {
return b;
}
}
NAME: VARUN SAPRA
B11+B12+B13

int height(struct node * root){


if(!root){
return 0;
}
return(1+max(height(root->left),height(root->right)));
}

void swap_nodes_at_level(struct node *root, int inc, int level, int heigh
t){
struct node *tnode;
if(!root){
return;
}
if(level > height){
return;
}
if(!(level%inc)){
tnode=root->left;
root->left=root->right;
root->right=tnode;
}
swap_nodes_at_level(root->left, inc, level+1, height);
swap_nodes_at_level(root->right, inc, level+1, height);
}

int tail=0;
int head=0;

void enqueue(struct node **queue, struct node *root){

queue[tail]=root;
tail++;
}

struct node* dequeue(struct node **queue){

struct node *temp = queue[head];


head++;
return temp;
}

int main() {
NAME: VARUN SAPRA
B11+B12+B13

int nodes_count, i, temp, h, tc_num, index, inc, temp1, temp2;

scanf("%d", &nodes_count);

struct node *root_perm, *root_temp;

struct node *q[nodes_count];


for(i=0;i<nodes_count;i++){
q[i]=NULL;
}

i=0,index=1;
root_temp=root_perm=create_node(1);
enqueue(q, root_temp);

while(index<=2*nodes_count) {

root_temp=dequeue(q);

scanf("%d", &temp1);
if(temp1 == -1){

} else {
root_temp->left=create_node(temp1);
enqueue(q, root_temp->left);
}

scanf("%d", &temp2);
if(temp2==-1) {

} else {
root_temp->right=create_node(temp2);
enqueue(q, root_temp->right);
}
index=index+2;
NAME: VARUN SAPRA
B11+B12+B13

h = height(root_perm);

scanf("%d", &tc_num);

while(tc_num){
scanf("%d",&inc);
temp=inc;

swap_nodes_at_level(root_perm, inc, 1, h);

inorder(root_perm);
printf("\n");
tc_num--;
}

return 0;
}

Task 3 : Is this a binary search tree?


NAME: VARUN SAPRA
B11+B12+B13

Code:

vector<int> nodes;
void inOrderTraversal(Node* node) {
if(node == NULL) {
return;
}
inOrderTraversal(node->left);
nodes.push_back(node->data);
inOrderTraversal(node->right);
}
bool checkBST(Node* root) {
inOrderTraversal(root);
for(int i = 1; i < nodes.size(); i++) {
NAME: VARUN SAPRA
B11+B12+B13

if(nodes[i] <= nodes[i-1]) {


return false;
}
}
return true;
}

Task 4: Balanced Brackets

Code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
NAME: VARUN SAPRA
B11+B12+B13

#define MAX_SIZE 1000

struct stack {
char stk[MAX_SIZE];
int top;
} s;

void push(char c) {
if (s.top < MAX_SIZE) {
s.stk[++s.top] = c;
}
}

char pop() {
if (s.top > -1) {
return s.stk[s.top--];
} else {
return -1;
}
}

int balancedParentheses(char *s, int length) {


char target, c;
if (length % 2 != 0) {
return 0;
}
for(int i = 0; i < length; i++) {
if ((s[i]=='(') || (s[i]=='{') || (s[i]=='[')) {
push(s[i]);
} else {
switch(s[i]) {
case ')': target = '('; break;
case '}': target = '{'; break;
case ']': target = '['; break;
}
c = pop();
if (c == -1 || c != target) {
return 0;
}
}
}
NAME: VARUN SAPRA
B11+B12+B13

return pop() == -1;


}

int main() {
int t, length;
char str[1000], c;
scanf("%d\n", &t);
for (int i = 0; i < t; i++) {
s.top = -1;
length = 0;
for (;;) {
c = getchar();
if (c == EOF || c == '\n') {
break;
}
str[length] = c;
length++;
}
printf("%s\n", balancedParentheses(str, length) == 0 ? "NO" : "YE
S");
}
return 0;
}

Task 5 : game of 2 stacks


NAME: VARUN SAPRA
B11+B12+B13

Code:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
int g;
scanf("%d",&g);
for(int a0 = 0; a0 < g; a0++){
int n;
int m;
int x;
scanf("%d %d %d",&n,&m,&x);
int *a = malloc(sizeof(int) * n);
for(int a_i = 0; a_i < n; a_i++){
scanf("%d",&a[a_i]);
}
int *b = malloc(sizeof(int) * m);
for(int b_i = 0; b_i < m; b_i++){
scanf("%d",&b[b_i]);
}
NAME: VARUN SAPRA
B11+B12+B13

int max = 0;
int sum = 0;
int items = 0;
int apos = 0, bpos = 0;
while (sum <= x && apos < n) {
if (sum + a[apos] > x)
break;
sum += a[apos];
apos++;
items++;
}
while (sum <= x && bpos < m) {
if (sum + b[bpos] > x)
break;
sum += b[bpos];
bpos++;
items++;
}
max = items;
while (1) {
if (apos <= 0)
break;
apos--;
sum -= a[apos];
items--;
while (sum <= x && bpos < m) {
if (sum + b[bpos] > x)
break;
sum += b[bpos];
bpos++;
items++;
}
if (items > max)
max = items;
if (bpos == m)
break;
}
printf ("%d\n", max);
}
return 0;
}
NAME: VARUN SAPRA
B11+B12+B13

Task 6 : Largest rectangle

Code:
#include <stdio.h>
#define SIZ 100000
int m[SIZ+1];
long long st[SIZ];

int main(){
int N,j,ptr;
long long r,h;
scanf("%d",&N);
for(j=0;j<N;j++)scanf("%d",m+j);m[j]=0;
for(r=ptr=j=0;j<=N;j++){
int left=j;
for(;ptr && (h=st[ptr-1]>>32)>m[j];){
int l=st[--ptr]&0xffffffff;
if(r<h*(j-l))r=h*(j-l);
left=l;
}
st[ptr++]=((long long)m[j]<<32)|left;
}
NAME: VARUN SAPRA
B11+B12+B13

printf("%lld\n",r);
return 0;
}

Task 7 : Simple Text Editor

Code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>

int main() {

FILE * in = stdin;
FILE * out = stdout;

int command;
int k;
char buf[1000000];
NAME: VARUN SAPRA
B11+B12+B13

char ** undos;
int n, i, ptr = 0;
size_t len;
fscanf(in, "%d", &n);
undos = calloc(sizeof(char *), n);
undos[ptr] = malloc(sizeof(char));
strcpy(undos[ptr], "");
for (i = 0; i < n; ++i) {
fscanf(in, "%d", &command);
switch(command) {
case 1:
fscanf(in, "%1000000s\n", buf);
++ptr;
undos[ptr] = malloc(sizeof(char) * (strlen(undos[ptr-1])
+ strlen(buf) + 1));
strcpy(undos[ptr], undos[ptr-1]);
strcat(undos[ptr], buf);
break;
case 2:
fscanf(in, "%d", &k);
++ptr;
undos[ptr] = malloc(sizeof(char) * (strlen(undos[ptr - 1]
) - k + 1));
len = strlen(undos[ptr - 1]);
memcpy(undos[ptr], undos[ptr-1], strlen(undos[ptr - 1]) -
k);
undos[ptr][len - k] = 0;
break;
case 3:
fscanf(in, "%d", &k);
fprintf(out, "%c\n", undos[ptr][k-1]);
break;
case 4:
--ptr;
break;
}
}
return 0;
}
NAME: VARUN SAPRA
B11+B12+B13

Task 8 : Waiter

Code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

#define DSTACK 2
#define OP1 0
#define OP2 1

int sp[3];
int *stack[3];

void initS(int n) {
for (int i=0;i<3;++i) {
stack[i]= (int *) malloc(sizeof(int)*n);
sp[i]=0;
}
}

void pushS(int i, int v) {


stack[i][sp[i]++]=v;
}
NAME: VARUN SAPRA
B11+B12+B13

int popS(int i) {
return stack[i][--sp[i]];
}

void closeS() {
for (int i=0;i<3;++i) free(stack[i]);
}

int emptyS(int i) {
return sp[i]==0;
}

int itemsS(int i) {
return sp[i];
}

int isPrime(int n) {

for (int i=2;i*i<=n;++i) {


if (n%i==0) return 0;
}

return 1;

int nextPrime(int n) {

++n;

while(!isPrime(n))
++n;

return n;
}
NAME: VARUN SAPRA
B11+B12+B13

int main() {

int n, q;
int i;
int v;
int ins, outs, prime;

scanf("%d %d",&n,&q);

int items[q];

initS(n); // init stack

for (i=0;i<n;++i) {
scanf("%d",&v);
pushS(0,v); // stack 0
}

ins=0;
outs=1;
prime=2;

for(i=0;i<q;++i) {

while(!emptyS(ins)) {
int v= popS(ins);
if (v%prime==0) pushS(DSTACK,v); //stack2
else pushS(outs,v);
}

items[i]=itemsS(DSTACK);

// SWAP stacks
int tmp=ins;
ins=outs;
outs=tmp;

prime=nextPrime(prime);

while(!emptyS(DSTACK)) {
v= popS(DSTACK);
printf("%d\n",v);
NAME: VARUN SAPRA
B11+B12+B13

}
}

while(!emptyS(ins)) {
v= popS(ins);
printf("%d\n",v);
}

closeS();

return 0;
}

You might also like