diff --git a/lab1/Yunakovskiy_alg1.c b/lab1/Yunakovskiy_alg1.c new file mode 100644 index 00000000..57f67e53 --- /dev/null +++ b/lab1/Yunakovskiy_alg1.c @@ -0,0 +1,238 @@ +#define STDC_WANT_LIB_EXT1 1 +#define CRT_SECURE_NO_WARNINGS +#include +#include +#include +#include +#include +#include +#include + +#define N 10 + +void printStr(char* A, int length) +{ + if (A == NULL) printf("Err: A is a null pointer!\n"); + + if (length < 1) printf("Err: String has invalid length!\n"); + else + { + printf("String = {"); + for(int i = 0; i < length; i++) + { + printf("%c",A[i]); + } + printf("}\n"); + } +} + +char* RandStr(char* A, int length) +{ + if (length < 1) + { + printf("Err: Invalid String Length\n"); + return A; + + } + char letter[26]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + A = (char*)malloc(length*sizeof(char)); + if (A == NULL) + { + printf("Err: Couldn't allocate memory!\n"); + return A; + } + for(int i = 0; i < length; i++) + { + A[i] = letter[rand()%26]; + } + printStr(A, length); + return A; +} + +int allocStrLength(char* A) +{ + if (A == NULL) + { + printf("Err: A is a null pointer!\n"); + return 0; + } + int i = 0; + do + { + i++; + } + while (A[i] <= 'Z' && A[i] >= 'A'); + return i; +} + +int Min_3(int a, int b, int c) +{ + return (a < b) ? (a < c ? a : c) : (b < c ? b : c); +} + +int A_B(char A, char B) +{ + if (A == B) return 0; + else return 1; +} + +int d(char *A, char *B) +{ + if (A == NULL || B == NULL) + { + printf("Err: A or B is a null pointer!\n"); + return 0; + + } + + int m = allocStrLength(A), n = allocStrLength(B); + + if (m < n) + { + m = m ^ n; + n = n ^ m; + m = m ^ n; + } + + int p = 0, res = 0; + int **d = (int **)malloc((m+1) * sizeof(int *)); + + for (int i = 0; i <= m; i++) + { + d[i] = (int *)malloc((m+1) * sizeof(int)); + d[i][0] = i; + } + for(int j = 0; j<= n; j++) + { + d[0][j] = j; + } + + for (int i = 1; i <= m; i++) + { + for (int j = 1; j <= n; j++) + { + d[i][j] = Min_3(d[i-1][j] + 1, // delete + d[i][j - 1] + 1, // insert + d[i - 1][j - 1] + A_B(A[i-1],B[j-1])); // substitute + } + } + + res = d[m][n]; + for (int i = 0; i <= m; i++) free(d[i]); + free(d); + + return res; +} + +void Test_AequalsB() +{ + // Arrange (A1) + char* A = (char*)malloc(N * sizeof(char)); + char* B = (char*)malloc(N * sizeof(char)); + for(int i = 0; i < N; i++) + { + A[i] = 'A'; + B[i] = A[i]; + } + + // Act (A2) + int c = d(A,B); + + // Assert (A3) + printf("Testd_AequalsB(): %d\n",c); +} + + + +void Test_AandBset_2() +{ + // A1 + char* A = (char*)malloc(N * sizeof(char)); + char* B = (char*)malloc(N * sizeof(char)); + for(int i = 0; i < N; i++) + { + A[i] = 'A'; + B[i] = A[i]; + } + B[2] = 'B'; + B[9] = 'B'; + + // A2 + int c = d(A,B); + + // A3 + printf("Testd_AandBset_2(): %d\n",c); +} + +void Test_AandBset_30() +{ + // A1 + char* A = (char*)malloc((N * N) * sizeof(char)); + char* B = (char*)malloc((N * N) * sizeof(char)); + for(int i = 0; i < N; i++) + { + A[i] = 'A'; + B[i] = A[i]; + } + for(int i = 0; i < 30; i++){ + B[i] = 'B'; + } + + // A2 + int c = d(A,B); + + // A3 + printf("Testd_AandBset_30(): %d\n",c); +} + +void Test_BbiggerthanA() +{ + // A1 + char* A = (char*)malloc(N * sizeof(char)); + char* B = (char*)malloc(20 * sizeof(char)); + for (int i = 0; i < N; i++) + { + A[i] = 'A'; + B[i] = A[i]; + } + for (int i = N; i < 20; i++) + { + B[i] = 'A'; + } + + // A2 + int c = d(A,B); + + // A3 + printf("Testd_BbiggerthanA(): %d\n",c); +} + +void Test_Negative() +{ + // A1 + int a = -2; + int b = 7; + int c = 9; + + // A2 + int r = Min_3(a, b, c); + + // A3 + printf("Test_Negative()): %d\n",r); +} +void main(void) { + setlocale(LC_CTYPE, "Russian"); + system("chcp 1251"); + srand(time(NULL)); + char* A = NULL, *B = NULL; + int a_length = 100; + int b_length = 100; + A = RandStr(A, a_length); + B = RandStr(B, b_length); + printf("Minimal Number of string Changes: %d\n",d(A,B)); + Test_AequalsB(); + Test_AandBset_2(); + Test_BbiggerthanA(); + Test_AandBset_30(); + Test_Negative(); +} diff --git a/lab2/main (2).cpp b/lab2/main (2).cpp new file mode 100644 index 00000000..3fda186c --- /dev/null +++ b/lab2/main (2).cpp @@ -0,0 +1,41 @@ +#include "treap.h" +#include "unit_tests.h" +int main() +{ + srand(time(nullptr)); + + Test_insert(); + Test_delete(); + + struct TreapNode* root = nullptr; + root = insert(root, 60); + root = insert(root, 50); + root = insert(root, 40); + root = insert(root, 20); + root = insert(root, 70); + root = insert(root, 80); + + + printf("\nFull treap:\n"); + printTreap(root, 0, 7); + printf("\n\n"); + order(root); + + + printf("\nDelete 20:\n"); + root = deleteNode(root, 20); + printTreap(root, 0, 6); + + printf("\nDelete 40:\n"); + root = deleteNode(root, 40); + printTreap(root, 0, 5); + + printf("\nDelete 60:\n"); + root = deleteNode(root, 60); + printTreap(root, 0, 4); + + printf("\nDelete 10:\n"); + root = deleteNode(root, 10); + + return 0; +} \ No newline at end of file diff --git a/lab2/main.cpp b/lab2/main.cpp new file mode 100644 index 00000000..5716b33d --- /dev/null +++ b/lab2/main.cpp @@ -0,0 +1,41 @@ +#include "treap.h" +#include "unit_tests.h" +int main() +{ + srand(time(nullptr)); + + Test_insert(); + Test_search(); + Test_delete(); + + struct TreapNode* root = nullptr; + root = insert(root, 60); + root = insert(root, 50); + root = insert(root, 40); + root = insert(root, 20); + root = insert(root, 70); + root = insert(root, 80); + + + printf("\nFull treap:\n"); + printTreap(root, 0, 7); + printf("\n\n"); + inorder(root); + + printf("\nDelete 20:\n"); + root = deleteNode(root, 20); + printTreap(root, 0, 6); + + printf("\nDelete 40:\n"); + root = deleteNode(root, 40); + printTreap(root, 0, 5); + + printf("\nDelete 60:\n"); + root = deleteNode(root, 60); + printTreap(root, 0, 4); + + printf("\nDelete 10:\n"); + root = deleteNode(root, 10); + + return 0; +} \ No newline at end of file diff --git a/lab2/treap.cpp b/lab2/treap.cpp new file mode 100644 index 00000000..c60eeb02 --- /dev/null +++ b/lab2/treap.cpp @@ -0,0 +1,154 @@ +#include "treap.h" + +TreapNode* rightRotate(TreapNode* y) +{ + TreapNode* x = y->l; + TreapNode* T2 = x->r; + x->r = y; + y->l = T2; + + return x; +} + +TreapNode* leftRotate(TreapNode* x) +{ + TreapNode* y = x->r; + TreapNode* T2 = y->l; + y->l = x; + x->r = T2; + + return y; +} + +TreapNode* newNode(int key, int degree) +{ + TreapNode *node; + node = (TreapNode *) malloc(sizeof(TreapNode)); + if (node != nullptr) + { + node->key = key; + node->degree = degree; + node->l = nullptr; + node->r = nullptr; + } + return node; +} + +TreapNode* search(TreapNode* root, int key) +{ + if (root == nullptr || root->key == key) + return root; + + if (root->key < key) + return search(root->r, key); + + return search(root->l, key); +} + +TreapNode* insert(TreapNode* root, int key) +{ + if (!root) + { + int random_priority = rand(); + return newNode(key, random_priority); + } + + if (key <= root->key) + { + root->l = insert(root->l, key); + + if (root->l->degree > root->degree) + return rightRotate(root); + } + else + { + root->r = insert(root->r, key); + + if (root->r->degree > root->degree) + return leftRotate(root); + } + return root; +} + + + + +TreapNode* deleteNode(TreapNode* root, int key) +{ + if (root == nullptr) + return root; + if (key < root->key) + { + root->l = deleteNode(root->l, key); + } + else if (key > root->key) + { + root->r = deleteNode(root->r, key); + } + else + { + if (root->l == nullptr) + { + TreapNode* temp = root->r; + free(root); + return temp; + } + else if (root->r == nullptr) + { + TreapNode* temp = root->l; + free(root); + return temp; + } + else if (root->l->degree < root->r->degree) + { + root = leftRotate(root); + root->l = deleteNode(root->l, key); + } + else + { + root = rightRotate(root); + root->r = deleteNode(root->r, key); + } + } + return root; +} + +void inorder(TreapNode* root) +{ + if (root) + { + inorder(root->l); + printf("key: %d | degree: %d", root->key, root->degree); + if (root->l) + printf(" | l child: %d", root->l->key); + if (root->r) + printf(" | r child: %d", root->r->key); + printf("\n"); + inorder(root->r); + } +} + +void printTreap(TreapNode* root, int space, int count) +{ + if (root == nullptr) + return; + space += count; + + printTreap(root->r, space, count); + + printf("\n"); + for (int i = count; i < space; ++i) + printf(" "); + + printf("(%d, %d)\n", root->key, root->degree); + printTreap(root->l, space, count); +} + +void freeTreap(TreapNode* root) +{ + if (root == nullptr) + return; + freeTreap(root->l); + freeTreap(root->r); + free(root); +} diff --git a/lab2/treap.h b/lab2/treap.h new file mode 100644 index 00000000..4cdab881 --- /dev/null +++ b/lab2/treap.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include +#include +#include + +using namespace std; + +struct TreapNode +{ + int key; + int degree; + TreapNode *l; + TreapNode *r; + +}; + + +TreapNode *rightRotate(TreapNode *y); + +TreapNode *leftRotate(TreapNode *x); + +TreapNode *newNode(int , int degree); + +TreapNode *search(TreapNode *root, int key); + +TreapNode *insert(TreapNode *root, int key); + +TreapNode *deleteNode(TreapNode *root, int key); + +void inorder(TreapNode *root); + +void printTreap(TreapNode *root, int space, int count); + +void freeTreap(TreapNode* root); \ No newline at end of file diff --git a/lab2/unit_tests.cpp b/lab2/unit_tests.cpp new file mode 100644 index 00000000..62e927d8 --- /dev/null +++ b/lab2/unit_tests.cpp @@ -0,0 +1,67 @@ + +#include "treap.h" +#include "unit_tests.h" + +void Test_insert() +{ + int key_root = 15, key_r = 10, key_l = 5; + struct TreapNode *root = nullptr; + + root = insert(root, key_root); + assert(root->key == key_root && root != nullptr); + + root = insert(root, key_left); + assert(root->l->key == key_left && root->l->degree <= root->degree && root->l != nullptr); + + root = insert(root, key_right); + assert(root->r->key == key_right && root->r->degree <= root->degree && root->r != nullptr); + + printf("All insert tests passed!\n"); + + freeTreap(root); +} + + +void Test_search() +{ + struct TreapNode *root = newNode(10, 50); + root->r = newNode(15, 40); + root->l = newNode(5, 30); + + assert(search(root, 10)->key == root->key && search(root, 10)->degree == 50); + assert(search(root, 15)->key == root->r->key && search(root, 15)->degree == 40); + assert(search(root, 5)->key == root->l->key && search(root, 5)->degree == 30); + + printf("All search tests passed!\n"); + + freeTreap(root); +} + +void Test_delete() +{ + struct TreapNode *root = newNode(10, 50); + root->r = newNode(15, 40); + root->l = newNode(5, 30); + + root = deleteNode(root, 15); + assert(root->r == nullptr); + + root = deleteNode(root, 5); + assert(root->l == nullptr); + + root = deleteNode(root, 10); + assert(root == nullptr); + + root = newNode(10, 50); + struct TreapNode *rNode = newNode(15, 40); + struct TreapNode *lNode = newNode(5, 30); + root->r = rNode; + root->l = lNode; + + root = deleteNode(root, 10); + assert(root->key == 15 && root->degree == 40 && root->r == nullptr && root->l == lNode); + + printf("All delete tests passed!\n"); + + freeTreap(root); +} \ No newline at end of file diff --git a/lab2/unit_tests.h b/lab2/unit_tests.h new file mode 100644 index 00000000..d2b24334 --- /dev/null +++ b/lab2/unit_tests.h @@ -0,0 +1,12 @@ +#pragma once +#include +#include +#include +#include +#include +using namespace std; + + +void Test_insert(); +void Test_search(); +void Test_delete();