Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
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
2 changes: 2 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
service_name: github
repo_token: ZRdiHKTOaRoRIWGWShUwUJx499MHpc4XL
70 changes: 70 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"files.associations": {
"iostream": "cpp",
"vector": "cpp",
"xstring": "cpp",
"stdexcept": "cpp",
"algorithm": "cpp",
"atomic": "cpp",
"bit": "cpp",
"cctype": "cpp",
"charconv": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"exception": "cpp",
"format": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"iterator": "cpp",
"limits": "cpp",
"locale": "cpp",
"memory": "cpp",
"new": "cpp",
"optional": "cpp",
"ostream": "cpp",
"set": "cpp",
"sstream": "cpp",
"streambuf": "cpp",
"string": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"utility": "cpp",
"xfacet": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocbuf": "cpp",
"xlocinfo": "cpp",
"xlocmes": "cpp",
"xlocmon": "cpp",
"xlocnum": "cpp",
"xloctime": "cpp",
"xmemory": "cpp",
"xtr1common": "cpp",
"xtree": "cpp",
"xutility": "cpp",
"array": "cpp",
"deque": "cpp",
"ranges": "cpp",
"span": "cpp",
"stack": "cpp",
"chrono": "cpp",
"forward_list": "cpp",
"ratio": "cpp",
"random": "cpp",
"list": "cpp"
}
}
41 changes: 38 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,55 @@
cmake_minimum_required(VERSION 3.15 FATAL_ERROR)

# название проекта
project(Algorithms-and-Data-Structures)
project(Ans)

# Добавление флагов компиляции для покрытия
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")

if(MSVC)
add_compile_options(/utf-8) # Для Visual Studio (MSVC)
else()
add_compile_options(-finput-charset=UTF-8 -fexec-charset=UTF-8) # Для GCC/Clang
endif()

# затем следует список инструкций для подключения проектов из подкаталогов

include(cmake/function.cmake) # подхватываем функции, реализованные в файле function.cmake
# для простоты мы объединили наборы команд для создания статической библиотеки
# и для создания исполняемого проекта в отдельные функции

# и для создания исполняемого проекта в отдельные функции
add_subdirectory(lib_pair)
add_subdirectory(lib_easy_example) # подключаем дополнительный CMakeLists.txt из подкаталога с именем lib_easy_example
add_subdirectory(main) # подключаем дополнительный CMakeLists.txt из подкаталога с именем main
add_subdirectory(lib_dmassive)
add_subdirectory(lib_stack)
add_subdirectory(lib_vector)
add_subdirectory(lib_queue)
add_subdirectory(lib_list)
add_subdirectory(lib_check_brackets)
add_subdirectory(lib_stack_list)
add_subdirectory(lib_polinom)
add_subdirectory(lib_matrix)
add_subdirectory(lib_dsu)
add_subdirectory(lib_merge_sorted_lists)
add_subdirectory(lib_itable)
add_subdirectory(lib_tbsttable)
add_subdirectory(lib_bin_search_tree)
add_subdirectory(lib_heap)
add_subdirectory(lib_avl)

option(BTEST "build test?" ON) # указываем подключаем ли google-тесты (ON или YES) или нет (OFF или NO)

if(BTEST) # если тесты подключены
add_subdirectory(gtest) # подключаем дополнительный CMakeLists.txt из подкаталога с именем gtest
add_subdirectory(tests) # подключаем дополнительный CMakeLists.txt из подкаталога с именем test
endif()

# Добавление цели для генерации отчета о покрытии
add_custom_target(coverage
COMMAND ${CMAKE_COMMAND} -E remove_directory coverage
COMMAND mkdir -p coverage
COMMAND lcov --capture --directory . --output-file coverage.info
COMMAND genhtml coverage.info --output-directory coverage
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
DEPENDS my_tests
)
2 changes: 1 addition & 1 deletion gtest/gtest-all.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9589,4 +9589,4 @@ const char* TypedTestCasePState::VerifyRegisteredTestNames(
#endif // GTEST_HAS_TYPED_TEST_P

} // namespace internal
} // namespace testing
} // namespace testing
2 changes: 1 addition & 1 deletion gtest/gtest.h
Original file line number Diff line number Diff line change
Expand Up @@ -20060,4 +20060,4 @@ inline int RUN_ALL_TESTS() {
return ::testing::UnitTest::GetInstance()->Run();
}

#endif // GTEST_INCLUDE_GTEST_GTEST_H_
#endif // GTEST_INCLUDE_GTEST_GTEST_H_
1 change: 1 addition & 0 deletions lib_avl/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
create_project_lib(AVL)
2 changes: 2 additions & 0 deletions lib_avl/avl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Copyright 2024 Anvar
#include "../lib_avl/avl.h"
236 changes: 236 additions & 0 deletions lib_avl/avl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
#ifndef LIB_AVL_TREE_H_
#define LIB_AVL_TREE_H_

#include <iostream>
#include <stdexcept>
#include <algorithm>
#include "../lib_avl/avl_node.h"
template<typename T>
class AVLTree {
AVLNode<T>* root;
void clear(AVLNode<T>* node);
AVLNode<T>* insert(AVLNode<T>* node , T key);
AVLNode<T>* remove(AVLNode<T>* node,T key );
bool search(const AVLNode<T>* node,T key ) const ;
AVLNode<T>* rightRotate(AVLNode<T>* node);
AVLNode<T>* leftRotate(AVLNode<T>* node);
AVLNode<T>* min_value_node(AVLNode<T>* node);
int height(AVLNode<T>* node) const;
int balance(AVLNode<T>* node) const;
public:
AVLTree() : root(nullptr) {}

~AVLTree();

void insert(T key);

void remove(T key);
bool search(T key) const;
AVLNode<T>* getRoot() const {
return root;
}
};
template<typename T>
bool AVLTree<T>:: search(T key) const { return search(root, key); }
template<typename T>
void AVLTree<T>::clear(AVLNode<T>* node) {
if (node) {
clear(node->left);
clear(node->right);
delete node;
}
}
template<typename T>
AVLTree<T>::~AVLTree() {
clear(root);
}
template<typename T>
AVLNode<T>* AVLTree<T>::insert(AVLNode<T>* node , T key){
if(node == nullptr){
return new AVLNode<T>(key);
}
if(key < node->key) {
node ->left = insert(node ->left , key);
}
else if(key > node->key) {
node->right= insert(node->right, key);
}
else {
throw std::invalid_argument("double key: ");
}
node->height = 1 + (std::max(height(node->left) , height(node->right)));
int balanceFactor = balance(node);
if(balanceFactor > 1 && key < node->left->key){
return rightRotate(node);
// B A
// / \ / \
// A C D B
// / \ > / / \
// D E X E C
// \
// X
}
if(balanceFactor < -1 && key > node->right->key){
return leftRotate(node);
// A B
// / \ / \
// C B A E
// / \ > / \ /
// D E C D X
// \
// X
}
if(balanceFactor > 1 && key > node->left->key){
node->left = leftRotate(node->left);
return rightRotate(node);
// C C B
// / \ / \ / \
// A L B L A C
// / \ > / \ > / \ / \
// D B A F D E F L
// / \ / \ /
// E F D E X
// / /
// X X
}
if((balanceFactor < -1 )&& key < node->right->key){
node->right = rightRotate(node->right);
return leftRotate(node);
// A (balance=-2) A B
// / \ / \ / \
// K C (disbalanced) K B (new right) A C
// / \ => / \ => / \ / \
// B E D C K D F E
// / \ / \ /
// D F F E X
// / /
// X X
}
return node;
}
template<typename T>
void AVLTree<T>::insert(T key) {
root = insert(root, key);
}
template<typename T>
AVLNode<T>* AVLTree<T>::remove(AVLNode<T>* node,T key ){
if (!node) {
throw std::out_of_range("Ключ не найден: ");
}
if (key < node->key) {
node->left = remove(node->left, key);
} else if (key > node->key) {
node->right = remove(node->right, key);
} else {
if ((node->left == nullptr) || (node->right == nullptr)) {
AVLNode<T>* temp = node->left ? node->left : node->right;
if (temp == nullptr) {
temp = node;
node = nullptr;
} else {
*node = *temp;
}
delete temp;
} else {
AVLNode<T>* temp = min_value_node(node->right);
node->key = temp->key;
node->right = remove(node->right, temp->key);
}
}

if (node == nullptr) {
return node;
}

node->height = 1 + std::max(height(node->left), height(node->right));
if (balance(node) > 1 && balance(node->left) >= 0) {
return rightRotate(node);
}
if (balance(node) > 1 && balance(node->left) < 0) {
node->left = leftRotate(node->left);
return rightRotate(node);
}
if (balance(node) < -1 && balance(node->right) <= 0) {
return leftRotate(node);
}
if (balance(node) < -1 && balance(node->right) > 0) {
node->right = rightRotate(node->right);
return leftRotate(node);
}

return node;
}
template<typename T>
void AVLTree<T>::remove(T key) {
root = remove(root, key);
}
template<typename T>
bool AVLTree<T>::search(const AVLNode<T>* node, T key) const {
if (!node) return false;
if (key == node->key) return true;
return key < node->key ? search(node->left, key) : search(node->right, key);
}

template<typename T>
AVLNode<T>* AVLTree<T>::rightRotate(AVLNode<T>* B ){
// A B
// / / \
// B > C A
// / \ /
// C D D
if (!B || !B->left) {
throw std::logic_error("Невозможно выполнить правый поворот");
}
AVLNode<T>* A = B->left;
AVLNode<T>* D = A->right;
A->right = B;
B->left =D;
B->height = std::max(height(B->left), height(B->right)) + 1;
A->height = std::max(height(A->left),height(A->right)) + 1;
return A;
}
template<typename T>
AVLNode<T>* AVLTree<T>::leftRotate(AVLNode<T>* A){
// A B
// \ / \
// B > A D
// / \ \
// C D C
if (!A || !A->right) {
throw std::logic_error("Невозможно выполнить левый поворот");
}
AVLNode<T>* B = A->right;
AVLNode<T>* C = B->left;
B->left = A;
A->right = C;
A->height = std::max(height(A->left),height(A->right)) + 1;
B->height= std::max(height(B->left),height(B->right)) + 1;
return B;
}

template<typename T>
AVLNode<T>* AVLTree<T>::min_value_node(AVLNode<T>* node){
if (!node) {
throw std::invalid_argument("Узел не существует");
}
AVLNode<T>* current = node;
while (current->left) {
current = current->left;
}
return current;
}
template<typename T>
int AVLTree<T>:: height(AVLNode<T>* node) const{
if(node == nullptr){
return 0;
}
return node ->height;
}
template<typename T>
int AVLTree<T>:: balance(AVLNode<T>* node) const{
if(node == nullptr) {
return 0;
}
return height(node->left) - height(node->right);
}
#endif // LIB_AVL_TREE_H_
2 changes: 2 additions & 0 deletions lib_avl/avl_node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Copyright 2024 Anvar
#include "../lib_avl/avl_node.h"
Loading
Loading