Skip to content

Commit f575f14

Browse files
Федорец Илья - Лабораторная работа №2 (#415)
* initial * fixes * fixes * fixes * fixes * fixes
1 parent b82fba7 commit f575f14

File tree

7 files changed

+518
-0
lines changed

7 files changed

+518
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Declare variables for binaries' names
2+
get_filename_component(DIR_NAME ${CMAKE_CURRENT_LIST_DIR} NAME)
3+
set(MODULE "${DIR_NAME}")
4+
set(LIBRARY "lib_${MODULE}")
5+
set(TESTS "test_${MODULE}")
6+
7+
# Include directory with public headers
8+
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
9+
10+
# Add all submodules
11+
add_subdirectory(src)
12+
add_subdirectory(test)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2024 Fedorets Ilya
2+
3+
#ifndef MODULES_FEDORETS_I_LEFTHEAP_INCLUDE_LEFTHEAP_H_
4+
#define MODULES_FEDORETS_I_LEFTHEAP_INCLUDE_LEFTHEAP_H_
5+
6+
#include <algorithm>
7+
#include <iostream>
8+
9+
class LeftistHeap {
10+
private:
11+
struct Node {
12+
int data;
13+
int rank;
14+
Node* left;
15+
Node* right;
16+
17+
explicit Node(int val)
18+
: data(val), rank(1), left(nullptr), right(nullptr) {}
19+
20+
explicit Node(const Node* other)
21+
: data(other->data), rank(other->rank), left(nullptr), right(nullptr) {
22+
if (other->left) left = new Node(other->left);
23+
if (other->right) right = new Node(other->right);
24+
}
25+
};
26+
27+
Node* root;
28+
29+
Node* deepCopy(const Node* node);
30+
Node* merge(Node* h1, Node* h2);
31+
void deleteNode(Node* node);
32+
33+
public:
34+
LeftistHeap() : root(nullptr) {}
35+
36+
LeftistHeap(const LeftistHeap& other) : root(deepCopy(other.root)) {}
37+
38+
LeftistHeap(LeftistHeap&& other) noexcept : root(other.root) {
39+
other.root = nullptr;
40+
}
41+
42+
LeftistHeap& operator=(const LeftistHeap& other);
43+
LeftistHeap& operator=(LeftistHeap&& other) noexcept;
44+
45+
~LeftistHeap() { deleteNode(root); }
46+
47+
void insert(int val);
48+
void merge(LeftistHeap& other);
49+
int deleteMin();
50+
bool isEmpty() const;
51+
int findMin() const;
52+
int size() const;
53+
54+
private:
55+
int countNodes(Node* node) const;
56+
};
57+
58+
#endif // MODULES_FEDORETS_I_LEFTHEAP_INCLUDE_LEFTHEAP_H_
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
set(target ${LIBRARY})
2+
3+
file(GLOB srcs "*.cpp")
4+
file(GLOB hdrs "../include/*.h")
5+
set_source_files_properties(${srcs} ${hdrs} PROPERTIES
6+
LABELS "${MODULE};Library")
7+
8+
add_library(${target} STATIC ${srcs} ${hdrs})
9+
set_target_properties(${target} PROPERTIES
10+
OUTPUT_NAME ${MODULE}
11+
LABELS "${MODULE};Library")
12+
13+
if (UNIX)
14+
target_link_libraries(${target} ${CMAKE_THREAD_LIBS_INIT})
15+
endif (UNIX)
16+
target_link_libraries(${target} ${LIBRARY_DEPS})
17+
18+
set(LIBRARY_DEPS "${LIBRARY_DEPS};${target}" PARENT_SCOPE)
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright 2024 Fedorets Ilya
2+
3+
#include "include/leftheap.h"
4+
5+
LeftistHeap::Node* LeftistHeap::deepCopy(const Node* node) {
6+
return node ? new Node(node) : nullptr;
7+
}
8+
9+
LeftistHeap::Node* LeftistHeap::merge(Node* h1, Node* h2) {
10+
if (h1 == nullptr) return h2;
11+
if (h2 == nullptr) return h1;
12+
13+
if (h1->data > h2->data) {
14+
std::swap(h1, h2);
15+
}
16+
17+
h1->right = merge(h1->right, h2);
18+
19+
if (h1->left == nullptr ||
20+
(h1->right != nullptr && h1->right->rank > h1->left->rank)) {
21+
std::swap(h1->left, h1->right);
22+
}
23+
24+
h1->rank = (h1->right ? h1->right->rank : 0) + 1;
25+
26+
return h1;
27+
}
28+
29+
void LeftistHeap::deleteNode(Node* node) {
30+
if (node == nullptr) return;
31+
deleteNode(node->left);
32+
deleteNode(node->right);
33+
delete node;
34+
}
35+
36+
LeftistHeap& LeftistHeap::operator=(const LeftistHeap& other) {
37+
if (this != &other) {
38+
deleteNode(root);
39+
40+
root = deepCopy(other.root);
41+
}
42+
return *this;
43+
}
44+
45+
LeftistHeap& LeftistHeap::operator=(LeftistHeap&& other) noexcept {
46+
if (this != &other) {
47+
deleteNode(root);
48+
49+
root = other.root;
50+
other.root = nullptr;
51+
}
52+
return *this;
53+
}
54+
55+
void LeftistHeap::insert(int val) {
56+
Node* newNode = new Node(val);
57+
root = merge(root, newNode);
58+
}
59+
60+
void LeftistHeap::merge(LeftistHeap& other) {
61+
root = merge(root, other.root);
62+
other.root = nullptr;
63+
}
64+
65+
int LeftistHeap::deleteMin() {
66+
if (root == nullptr) {
67+
throw std::runtime_error("Heap is empty");
68+
}
69+
70+
int minVal = root->data;
71+
Node* oldRoot = root;
72+
root = merge(root->left, root->right);
73+
delete oldRoot;
74+
75+
return minVal;
76+
}
77+
78+
bool LeftistHeap::isEmpty() const { return root == nullptr; }
79+
80+
int LeftistHeap::findMin() const {
81+
if (root == nullptr) {
82+
throw std::runtime_error("Heap is empty");
83+
}
84+
return root->data;
85+
}
86+
87+
int LeftistHeap::size() const { return countNodes(root); }
88+
int LeftistHeap::countNodes(Node* node) const {
89+
if (node == nullptr) return 0;
90+
return 1 + countNodes(node->left) + countNodes(node->right);
91+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
set(target ${TESTS})
2+
3+
file(GLOB srcs "*.cpp")
4+
set_source_files_properties(${srcs} PROPERTIES
5+
LABELS "${MODULE};Test")
6+
7+
add_executable(${target} ${srcs} ${hdrs})
8+
set_target_properties(${target} PROPERTIES
9+
LABELS "${MODULE};Test")
10+
11+
if (UNIX)
12+
target_link_libraries(${target} gtest ${CMAKE_THREAD_LIBS_INIT} pthread)
13+
endif (UNIX)
14+
target_link_libraries(${target} gtest ${LIBRARY})
15+
16+
# VS2012 doesn't support correctly the tuples yet,
17+
# see http://code.google.com/p/googletest/issues/detail?id=412
18+
if(MSVC)
19+
target_compile_definitions(${target} PUBLIC _VARIADIC_MAX=10)
20+
endif()
21+
22+
add_test(
23+
NAME ${MODULE}_gtest
24+
COMMAND ${target}
25+
)
26+
set_tests_properties (${MODULE}_gtest PROPERTIES
27+
LABELS "${MODULE}")

0 commit comments

Comments
 (0)