diff --git a/lab1/Khayaynen_13var.c b/lab1/Khayaynen_13var.c new file mode 100644 index 0000000..b4c5602 --- /dev/null +++ b/lab1/Khayaynen_13var.c @@ -0,0 +1,109 @@ +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include + + + + +void SortedAndDeleting(int* a, int n) +{ + int max = -821712, MaxIndex = 0; + int* c = (int*)malloc(sizeof(int) * (n)); + int* b = (int*)malloc(sizeof(int) * (n)); + for (int i = 0; i < n; i++) { + b[i] = 0; + } + + c[0] = -1; + c[1] = 0; + if (n != 1) { + for (int i = 1; i <= n; i++) { + for (int j = 0; j < i; j++) { + if (a[j] < a[i] && b[i] < b[j] + 1) { + c[i] = j; + b[i] = b[j] + 1; + if (b[i] > max) { + max = b[i]; + MaxIndex = i; + } + } + } + } + + } + + printf("\n \n\n%d\n", a[MaxIndex]); + + while (MaxIndex != 0) { + + MaxIndex = c[MaxIndex]; + printf("%d\n", a[MaxIndex]); + + } + free(b); +} + +void Tests(int n) +{ + + printf("1. , 1 \n"); + int m = 1; + int* a = malloc(m * sizeof(int)); + a[0] = 1; + SortedAndDeleting(a, m); + free(a); + printf("\n"); + + printf("2. , \n"); + m = 5; + a = malloc(m * sizeof(int)); + printf(" = "); + for (int i = 0;i < m; i++) + { + a[i] = 5 - i; + printf("%d ", a[i]); + } + + SortedAndDeleting(a, m); + free(a); + printf("\n"); + + printf("3. , , , + \n"); + m = 13; + int b[13] = {1,2,2,2,4,5,6,3,4,7,8,9,6}; + printf(" = {1,2,2,2,4,5,6,3,4,7,8,9,6}\n"); + SortedAndDeleting(b, m); + printf("\n"); + +} + +int main() +{ + int n,k=0; + setlocale(LC_CTYPE, "Rus"); + Tests(k); + printf(" \n\n"); + scanf("%d", &n); + printf("\n"); + + if (n == 0) { + printf(" \n"); + exit(1); + } + printf(" \n\n"); + int* a = malloc(n * sizeof(int)); + for (int i = 0; i < n; i++) + { + scanf("%d", &a[i]); + if (a[i] == " " || a[i] == '\0') { + exit(1); + printf(""); + } + } + + printf("\n\n\n"); + SortedAndDeleting(a,n); + free(a); + return 0; +} \ No newline at end of file diff --git a/lab1/Khayaynen_7var.c b/lab1/Khayaynen_7var.c new file mode 100644 index 0000000..609adca --- /dev/null +++ b/lab1/Khayaynen_7var.c @@ -0,0 +1,160 @@ +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include +#include + + + + +void SearchSequence(int* a, int n) +{ + + int max = -10, MaxIndex = 0; + int* b = (int*)malloc(sizeof(int) * (n)); + for (int i = 0; i < n; i++) { + b[i] = 1; + + } + if (n != 1) { + int i = n - 1; + while(i>=0) { + int j = i, flag = 1; + while ((j >= 0) && (flag == 1)) + { + if (a[j] == 0) + { + flag = 0; + } + else + { + + if (a[j - 1] != 0 && a[j] % a[j - 1] == 0) + { + b[i]++; + + } + else flag = 0; + if (b[i] > max) { + max = b[i]; + MaxIndex = i; + } + + j--; + } + } + + i=i-b[i]; + } + + } + + + printf("\n = "); + + /*for (int i = 0;i <= n;i++) + { + printf("%d ", b[i]); + } + printf("\n\n");*/ + + + //printf("\nmax = %d\nMaxInd = %d\n", max, MaxIndex); + if (n == 1) + { + printf("%d", a[0]); + } + else + { + for (int i = MaxIndex - max + 1;i <= MaxIndex;i++) + { + printf("%d ", a[i]); + } + + printf("\n"); + } + free(b); +} + +void Tests() +{ + + printf("1. , 1 \n = {1}"); + int m = 1; + int* a = malloc(m * sizeof(int)); + a[0] = 1; + SearchSequence(a, m); + free(a); + printf("\n"); + + printf("\n2. , \n"); + m = 5; + a = malloc(m * sizeof(int)); + printf(" = "); + for (int i = 0;i < m; i++) + { + a[i] = 0; + printf("%d ", a[i]); + } + + SearchSequence(a, m); + free(a); + printf("\n"); + + printf("3. , + \n"); + m = 12; + int c[12] = {1,2,4,5,6,48,96,2,4,8,16,32}; + printf(" = {1,2,4,5,6,48,96,2,4,8,16,32}\n"); + SearchSequence(c, m); + printf("\n"); + + + printf("4. , \n"); + m = 10; + int d[10] = {1,0,4,5,6,48,96,2,0,8}; + printf(" = { 1,0,4,5,6,48,96,2,0,8 }\n"); + SearchSequence(d, m); + printf("\n"); + +} + +int main() +{ + int n, k = 0; + setlocale(LC_CTYPE, "Rus"); + Tests(); + printf(" \n\n"); + scanf("%d", &n); + printf("\n"); + + if (n == 0) { + printf(" \n"); + exit(1); + } + printf(" \n\n"); + int* a = malloc(n * sizeof(int)); + for (int i = 0; i < n; i++) + { + scanf("%d", &a[i]); + if (a[i] == " " || a[i] == "\0") { + printf(""); + exit(5); + + } + if (a[i] == 0) + { + k++; + } + } + if (k == n) + { + printf("\n \n"); + } + else + { + printf("\n"); + SearchSequence(a, n); + } + free(a); + return 0; +} \ No newline at end of file diff --git a/lab1/readme.txt b/lab1/readme.txt new file mode 100644 index 0000000..7344991 --- /dev/null +++ b/lab1/readme.txt @@ -0,0 +1,10 @@ +Хаяйнен Никита 5030102/20003 +Вариант 13 +Из последовательности, состоящей из N чисел, вычеркнуть минимальное количество элементов так, +чтобы оставшиеся образовали строго возрастающую последовательность. Подсказка: В массиве A[1..N] помещаются сами числа последовательности. +Заведем массив B[1..N] такой, что элемент B[i] имеет значение длины максимальной возрастающей подпоследовательности, завершающейся элементом A[i]. + +Переделал вариант 13 в 7, так как я частично скопировал код Дани #64 + +Вариант 7 +В заданной последовательности целых чисел найти максимально длинную подпоследовательность чисел такую, что каждый последующий элемент подпоследовательности делился нацело на предыдущий. diff --git a/lab2/lab2.c b/lab2/lab2.c new file mode 100644 index 0000000..82bfa6b --- /dev/null +++ b/lab2/lab2.c @@ -0,0 +1,383 @@ +#include +#include +#include +#include +#include +#include + +struct TreeAVLNode { + int key; + int height; + struct TreeAVLNode* parent; + struct TreeAVLNode* left; + struct TreeAVLNode* right; +}; + + +int Max(int a, int b) { + if (a > b) + { + return a; + } + else + { + return b; + } +} + +int GetHeight(struct TreeAVLNode* Node) { + if (Node) + { + return Node->height; + } + else {return 0;} + +} + +int bfactor(struct TreeAVLNode* Node) { + return (GetHeight(Node->right) - GetHeight(Node->left)); +} + +void FixHeight(struct TreeAVLNode* Node) { + int rheight = GetHeight(Node->right); + int lheight = GetHeight(Node->left); + Node->height = Max(lheight,rheight) + 1; +} +struct TreeAVLNode* NewNodeCreating(int key) { + + struct TreeAVLNode* Node = (struct TreeAVLNode*)malloc(sizeof(struct TreeAVLNode)); + if (!Node) { + exit(EXIT_FAILURE); + } + Node->key = key; + Node->height = 1; + Node->parent = NULL; + Node->left = NULL; + Node->right = NULL; + + + return Node; +} + +void FindingRoot(struct TreeAVLNode** Node) { + while ((*Node)->parent) { + (*Node) = (*Node)->parent; + } +} +struct TreeAVLNode* SmallRotationRight(struct TreeAVLNode* p) { + struct TreeAVLNode* q = p->left; + struct TreeAVLNode* parent = p->parent; + p->left = q->left; + q->right = p; + q->parent = parent; + p->left = NULL; + FixHeight(p); + FixHeight(q); + return q; +} + +struct TreeAVLNode* SmallRotationLeft(struct TreeAVLNode* q) { + + struct TreeAVLNode* p = q->right; + struct TreeAVLNode* parent = q->parent; + q->right = p->right; + + p->left = q; + p->parent = parent; + q->right = NULL; + FixHeight(q); + FixHeight(p); + return p; + + +} + +struct TreeAVLNode* Balance(struct TreeAVLNode* Node) { + + FixHeight(Node); + + if (bfactor(Node) == 2) { + if (bfactor(Node->right) < 0) + { + + Node->right = SmallRotationRight(Node->right); + Node->right->parent->right = Node->right; + } + + Node= SmallRotationLeft(Node); + Node->parent->left = Node; + } + if (bfactor(Node) == -2) { + if (bfactor(Node->left) > 0) + { + + Node->left = SmallRotationLeft(Node->left); + Node->left->parent->left = Node->left; + } + + Node=SmallRotationRight(Node); + Node->parent->left = Node; + } + return Node; +} + + +void InsertFind(struct TreeAVLNode** Node, struct TreeAVLNode** q, struct TreeAVLNode* FindingRoot, int key) { + struct TreeAVLNode* current = FindingRoot; + + while (current) { + *Node = current; + if (key < current->key) current = current->left; + else current = current->right; + } + + if (key < (*Node)->key) (*Node)->left = *q; + else (*Node)->right = *q; + + (*q)->parent = (*Node); +} + +void BalanceTree( struct TreeAVLNode* parent, struct TreeAVLNode* new) { + while (parent) { + FixHeight(parent); + int balance = bfactor(parent); + + if (balance == 0) break; + + else if (abs(balance) == 1) { + new = parent; + parent = parent->parent; + } + else { + parent= Balance(parent); + + break; + } + } +} + + + +struct TreeAVLNode* InsertNode(struct TreeAVLNode* Node, int key) { + struct TreeAVLNode* new = NewNodeCreating(key); + if (!Node) return new; + + struct TreeAVLNode* parent = NULL; + + InsertFind(&parent, &new, Node, key); + BalanceTree(parent, new); + return Node; +} + +void NodeDelete(struct TreeAVLNode* Node, struct TreeAVLNode* current, struct TreeAVLNode* parent) { + + struct TreeAVLNode* child; + if (current->right) + { + child = current->right; + + } + else + { + child = current->left; + } + + + if (!parent) Node = child; + else if (parent->left == current) { + parent->left = child; + child->left = current->left; + } + else { + parent->right = child; + child->right = Node->right; + + } + + if (child) child->parent = parent; + + free(current); +} + +struct TreeAVLNode* Delete(struct TreeAVLNode* Node, int k) { + if (!Node) return NULL; + + struct TreeAVLNode* parent = NULL; + struct TreeAVLNode* current = Node; + + while (current && current->key != k) { + parent = current; + if (k < current->key) current = current->left; + else current = current->right; + } + if (!current) return Node; + + NodeDelete(Node, current, parent); + + return Balance(Node); +} + +bool isSame(struct TreeAVLNode* root1, struct TreeAVLNode* root2) { + if (root1 == NULL && root2 == NULL) { + return true; + } + if (root1 != NULL && root2 != NULL) { + return (root1->key == root2->key && isSame(root1->left, root2->left) && isSame(root1->right, root2->right)); + } + return false; +} + + +void PrintAVLTree(struct TreeAVLNode* root, int height) { + if (root == NULL) return; + + + int levelNodes = 1; // + + struct TreeAVLNode** queue = (struct TreeAVLNode*)malloc(sizeof(struct TreeAVLNode*) * 100); + int front = 0, rear = 0; + queue[rear++] = root; + + for (int level = 1; level <= height; level++) { + // + int spaces = pow(2, height - level) - 1; + for (int i = 0; i < spaces; i++) printf(" "); + + for (int i = 0; i < levelNodes; i++) { + struct TreeAVLNode* current = queue[front++]; + if (current == NULL) { + printf(" "); // + queue[rear++] = NULL; + queue[rear++] = NULL; + } + else { + printf("%2d", current->key); + queue[rear++] = current->left; + queue[rear++] = current->right; + } + for (int j = 0; j < 2 * spaces ; j++) printf(" "); + } + printf("\n"); + levelNodes *= 2; + } + + free(queue); +} + + + + + +void TestInsert() +{ + + struct TreeAVLNode* NodeCheck = NewNodeCreating(10); + NodeCheck->left = NewNodeCreating(2); + NodeCheck->left->parent = NodeCheck; + NodeCheck->right = NewNodeCreating(11); + NodeCheck->right->parent = NodeCheck; + NodeCheck->left->right = NewNodeCreating(6); + NodeCheck->left->right->parent = NodeCheck->left; + //PrintAVLTree(NodeCheck, 5); + + struct TreeAVLNode* NodeCheck8 = NewNodeCreating(10); + NodeCheck8->left = NewNodeCreating(6); + NodeCheck8->left->parent = NodeCheck8; + NodeCheck8->right = NewNodeCreating(11); + NodeCheck8->right->parent = NodeCheck8; + NodeCheck8->left->right = NewNodeCreating(8); + NodeCheck8->left->right->parent = NodeCheck8->left; + NodeCheck8->left->left = NewNodeCreating(2); + NodeCheck8->left->left->parent = NodeCheck8->left; + //PrintAVLTree(NodeCheck8, 5); + + printf("\n\nTests\n\n"); + printf(" \n \n\n"); + struct TreeAVLNode* Node = NewNodeCreating(10); + Node->left = NewNodeCreating(2); + Node->left->parent = Node; + Node->right = NewNodeCreating(11); + Node->right->parent = Node; + PrintAVLTree(Node, 5); + printf(" 6\n\n"); + InsertNode(Node, 6); + PrintAVLTree(Node, 5); + if (isSame(Node, NodeCheck) == true) + { + printf(" \n\n"); + } + else + { + printf(" \n\n"); + } + + printf(" 8\n\n"); + InsertNode(Node, 8); + PrintAVLTree(Node, 5); + if (isSame(Node, NodeCheck8) == true) + { + printf(" \n\n"); + } + else + { + printf(" \n\n"); + } + + free(Node); + free(NodeCheck); + free(NodeCheck8); +} + +void TestRemove() +{ + struct TreeAVLNode* NodeCheck5 = NewNodeCreating(10); + NodeCheck5->left = NewNodeCreating(6); + NodeCheck5->left->parent = NodeCheck5; + NodeCheck5->right = NewNodeCreating(11); + NodeCheck5->right->parent = NodeCheck5; + NodeCheck5->right->right = NewNodeCreating(12); + NodeCheck5->right->right->parent = NodeCheck5->right;; + NodeCheck5->left->left = NewNodeCreating(2); + NodeCheck5->left->left->parent = NodeCheck5->left; + NodeCheck5->left->left->left = NewNodeCreating(1); + NodeCheck5->left->left->left->parent = NodeCheck5->left->left; + NodeCheck5->left->left->right = NewNodeCreating(3); + NodeCheck5->left->left->right->parent = NodeCheck5->left->left; + + printf(" \n \n\n"); + struct TreeAVLNode* Node = NewNodeCreating(10); + Node->left = NewNodeCreating(5); + Node->left->parent = Node; + Node->right = NewNodeCreating(11); + Node->right->parent = Node; + + InsertNode(Node, 3); + InsertNode(Node, 6); + InsertNode(Node, 12); + InsertNode(Node, 2); + InsertNode(Node, 1); + PrintAVLTree(Node, 5); + printf(" 5\n\n"); + Delete(Node, 5); + PrintAVLTree(Node, 5); + if (isSame(Node, NodeCheck5) == true) + { + printf(" \n\n"); + } + else + { + printf(" \n\n"); + } + free(Node); + free(NodeCheck5); +} + +int main() { + setlocale(LC_CTYPE, "Rus"); + printf(" 5030102/20003 2 \n\n"); + + TestInsert(); + TestRemove(); + + return 0; +} \ No newline at end of file diff --git a/lab2/readme.txt b/lab2/readme.txt new file mode 100644 index 0000000..9f9ca73 --- /dev/null +++ b/lab2/readme.txt @@ -0,0 +1,3 @@ +Хаяйнен Никита 5030102/20003 +Вариант 2 +Реализовать операции добавления и удаления в АВЛ дереве со ссылками на предков без рекурсии. \ No newline at end of file