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

Laba25 26 #415

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ad6daad
Лаба 5
adamarselgov2609 Oct 20, 2023
f7541d0
Update лаба 5 МТ.txt
adamarselgov2609 Oct 30, 2023
02193f4
Лаба 5 мт
adamarselgov2609 Oct 31, 2023
f7ce877
Лаба 5 МТ 1
adamarselgov2609 Oct 31, 2023
492d094
Адам Арсельгов ЛР9
adamarselgov2609 Nov 17, 2023
c111d3a
ЛР9 Адам Арсельгов
adamarselgov2609 Nov 17, 2023
a6087b2
Адам Арсельгов 12 лаба
adamarselgov2609 Dec 16, 2023
a17354c
Адам Арсельгов лаба 12
adamarselgov2609 Dec 16, 2023
4451ad6
ЛР 15 Адам Арсельгов
adamarselgov2609 Dec 23, 2023
5b4b8ac
lab13 14
adamarselgov2609 Dec 23, 2023
c0bd745
Test
adamarselgov2609 Dec 23, 2023
94ae717
Адам Арсельгов ЛР 13
adamarselgov2609 Dec 24, 2023
c608add
Лаба 13 Адам Арсельгов
adamarselgov2609 Dec 24, 2023
cb1428f
ЛР 21 Адам Арсельгов
adamarselgov2609 Feb 24, 2024
1064ad5
ЛР21 Адам Арсельгов Исправленная
adamarselgov2609 Feb 24, 2024
d0a4f90
Адам Арсельгов ЛР23
adamarselgov2609 Mar 11, 2024
d3447a6
ЛР23 Адам Арсельгов
adamarselgov2609 Mar 11, 2024
443c82c
Адам Арсельгов ЛР23
adamarselgov2609 Mar 11, 2024
1516f8b
ЛР23 Адам Арсельгов
adamarselgov2609 Mar 12, 2024
2c54353
Адам Арсельгов ЛР24
adamarselgov2609 Jun 7, 2024
e2260bd
Адам Арсельгов ЛР25_26
adamarselgov2609 Jun 11, 2024
beef689
Адам Арсельгов ЛР25-26
adamarselgov2609 Jun 18, 2024
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
167 changes: 167 additions & 0 deletions labs/lab23/lab_work23.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node {
int data;
unsigned char height;

struct node *left;
struct node *right;
} node;

node *create_node(int data) {

struct node *node = (struct node *)malloc(sizeof(struct node));

node->data = data;

node->left = NULL;
node->right = NULL;

node->height = 1;

return node;
}

unsigned char height(node *p) {

if (p != NULL) {
return p->height;
} else {
return 0;
}
}

int bfactor(node *p) { return height(p->right) - height(p->left); }

void fixheight(node *p) {

unsigned char hl = height(p->left);
unsigned char hr = height(p->right);

p->height = (hl > hr ? hl : hr) + 1;
}

node *rotateright(node *p) {

node *q = p->left;
p->left = q->right;
q->right = p;

fixheight(p);
fixheight(q);

return q;
}

node *rotateleft(node *q) {
node *p = q->right;

q->right = p->left;
p->left = q;

fixheight(q);
fixheight(p);

return p;
}

node *balance(node *p) {

fixheight(p);

if (bfactor(p) == 2) {
if (bfactor(p->right) < 0) {
p->right = rotateright(p->right);
}
return rotateleft(p);
}

if (bfactor(p) == -2) {
if (bfactor(p->left) > 0) {
p->left = rotateleft(p->left);
}
return rotateright(p);
}

return p;
}

void print_tree(node *root) {

if (root->left != NULL) {
print_tree(root->left);
}

if (root->right != NULL) {
print_tree(root->right);
}

printf("%d ", root->data);
}

bool is_binary_search_tree(node *root) {

bool is_left_bin_search = true;
bool is_right_bin_search = true;

if (root->left != NULL) {
if (root->left->data > root->data) {
return false;
}
is_left_bin_search = is_binary_search_tree(root->left);
}

if (root->right != NULL) {
if (root->right->data <= root->data) {
return false;
}
is_right_bin_search = is_binary_search_tree(root->right);
}

return is_left_bin_search && is_right_bin_search;
}

bool is_avl_tree(node *root) {
if (is_binary_search_tree(root) == false) {
return false;
}

fixheight(root);

if (bfactor(root) >= 2 || bfactor(root) <= -2) {

return false;
}

return true;
}

int main() {
node *mynode = create_node(10);
mynode->left = create_node(5);
mynode->left->left = create_node(3);
mynode->right = create_node(15);
mynode->left->left->left = create_node(1);

mynode->height = 4;
mynode->left->height = 3;
mynode->left->left->height = 2;
mynode->right->height = 1;
mynode->left->left->left->height = 1;

printf("Is it a AVL tree: %d\n", is_avl_tree(mynode));

print_tree(mynode);

node *root = balance(mynode);

puts("\nBalanced");

print_tree(root);
printf("\nIs it a AVL tree: %d\n", is_avl_tree(root));

return 0;
}
194 changes: 194 additions & 0 deletions labs/lab23/laba_23.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>


typedef struct node
{
int data;
unsigned char height;

struct node* left;
struct node* right;
} node;



node* create_node(int data) {

struct node* node = (struct node*)malloc(sizeof(struct node));
node->data = data;


node->left = NULL;
node->right = NULL;


node->height = 1;

return node;
}


unsigned char height(node* p) {

if(p != NULL) {
return p->height;
} else {
return 0;
}

}

int bfactor(node* p) {

return height(p->right) - height(p->left);

}


void fixheight(node* p) {

unsigned char hl = height(p->left);
unsigned char hr = height(p->right);

p->height = (hl > hr ? hl : hr) + 1;
}


node* rotateright(node* p) {


node* q = p->left;
p->left = q->right;
q->right = p;


fixheight(p);
fixheight(q);


return q;
}
node* rotateleft(node* q)
{
node* p = q->right;

q->right = p->left;
p->left = q;

fixheight(q);
fixheight(p);

return p;
}



node* balance(node* p) {

fixheight(p);


if (bfactor(p) == 2) {
if( bfactor(p->right) < 0 ) {
p->right = rotateright(p->right);
}
return rotateleft(p);
}


if (bfactor(p) == -2) {
if( bfactor(p->left) > 0 ) {
p->left = rotateleft(p->left);
}
return rotateright(p);
}

return p;
}


void print_tree(node* root) {

if(root->left != NULL) {
print_tree(root->left);
}

if(root->right != NULL) {
print_tree(root->right);
}

printf("%d ", root->data);
}


bool is_binary_search_tree(node *root) {

bool is_left_bin_search = true;
bool is_right_bin_search = true;

if(root->left != NULL) {
if(root->left->data > root->data) {
return false;
}
is_left_bin_search = is_binary_search_tree(root->left);
}

if(root->right != NULL) {
if(root->right->data <= root->data) {
return false;
}
is_right_bin_search = is_binary_search_tree(root->right);
}

return is_left_bin_search && is_right_bin_search;
}

bool is_avl_tree(node* root) {
if(is_binary_search_tree(root) == false) {
return false;
}


fixheight(root);


if (bfactor(root) >= 2 || bfactor(root) <= -2) {


return false;
}

return true;
}

int main(){
node* mynode = create_node(10);
mynode->left = create_node(5);
mynode->left->left = create_node(3);
mynode->right = create_node(15);
mynode->left->left->left = create_node(1);

mynode->height = 4;
mynode->left->height = 3;
mynode->left->left->height = 2;
mynode->right->height = 1;
mynode->left->left->left->height = 1;


printf("Is it a AVL tree: %d\n", is_avl_tree(mynode));

print_tree(mynode);

node* root = balance(mynode);

puts("\nBalanced");

print_tree(root);
printf("\nIs it a AVL tree: %d\n", is_avl_tree(root));

return 0;
}

Loading