Skip to content

Commit b3bb6ae

Browse files
committed
add heap data structure
1 parent 51515ed commit b3bb6ae

File tree

6 files changed

+190
-0
lines changed

6 files changed

+190
-0
lines changed

Data_structures/heap/.clang-format

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Language: Cpp
2+
BasedOnStyle: Microsoft
3+
AlignTrailingComments: true
4+
BreakBeforeBraces: Custom
5+
BraceWrapping:
6+
AfterEnum: true
7+
AfterStruct: true
8+
AfterClass: true
9+
AfterFunction: true
10+
AfterUnion: true
11+
AfterExternBlock: false
12+
BeforeCatch: true
13+
BeforeElse: true
14+
BeforeLambdaBody: true
15+
BeforeWhile: false
16+
AfterNamespace: false
17+
SplitEmptyFunction: true
18+
IndentWidth: 4
19+
KeepEmptyLinesAtTheStartOfBlocks: false
20+
PointerBindsToType: false
21+
SpacesBeforeTrailingComments: 1
22+
TabWidth: 4
23+
UseTab: Never
24+
IndentCaseLabels: true
25+
NamespaceIndentation: All
26+
AccessModifierOffset: -4
27+
AlignAfterOpenBracket: Align
28+
AlignConsecutiveAssignments: Consecutive
29+
AlignConsecutiveMacros:
30+
Enabled: true
31+
AcrossEmptyLines: true
32+
AcrossComments: false
33+
AllowShortCaseLabelsOnASingleLine: true
34+
AlignEscapedNewlines: Right
35+
AllowShortBlocksOnASingleLine: Always
36+
AllowShortEnumsOnASingleLine: false
37+
AlignConsecutiveDeclarations: true
38+
AlwaysBreakTemplateDeclarations: true
39+
Cpp11BracedListStyle: false
40+
PackConstructorInitializers: Never
41+
AllowShortFunctionsOnASingleLine: Empty
42+
ReflowComments: true
43+
PenaltyBreakComment: 0
44+
PenaltyBreakOpenParenthesis: 1

Data_structures/heap/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.cache
2+
build/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
project(heap)
4+
5+
add_executable(heap main.cpp)
6+
7+
target_include_directories(heap PRIVATE
8+
${CMAKE_CURRENT_SOURCE_DIR}/headers
9+
${CMAKE_CURRENT_SOURCE_DIR}/src )
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#ifndef THEAP_H
2+
#define THEAP_H
3+
4+
#include <cstddef>
5+
#include <functional>
6+
#include <iostream>
7+
#include <vector>
8+
9+
namespace my {
10+
template <typename T, typename Cmp = std::less<T>>
11+
class Heap
12+
{
13+
public:
14+
~Heap() = default;
15+
Heap(std::vector<T> &input, const Cmp &cmp = Cmp());
16+
17+
template <typename RandomAccessIterator>
18+
Heap(RandomAccessIterator first, RandomAccessIterator last);
19+
20+
public:
21+
size_t parent(size_t ind);
22+
size_t left(size_t ind);
23+
size_t right(size_t i);
24+
void push(const T &val);
25+
void pop();
26+
const T &top();
27+
28+
private:
29+
void make_heap();
30+
void heapify_down(size_t ind);
31+
32+
public:
33+
void print();
34+
35+
private:
36+
std::vector<T> m_heap;
37+
size_t m_size;
38+
Cmp m_cmp;
39+
};
40+
41+
#include "../src/heap.hpp"
42+
43+
} // namespace my
44+
45+
#endif // THEAP_H

Data_structures/heap/main.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <heap.h>
2+
#include <vector>
3+
4+
int main()
5+
{
6+
std::vector<int> vec = { 3, 2, 4, 1, 5, 9 };
7+
my::Heap<int> min(vec);
8+
min.print();
9+
10+
my::Heap<int, std::greater<int>> max (vec);
11+
max.print();
12+
13+
my::Heap<int> h(vec.begin() + 3, vec.end());
14+
h.print();
15+
}

Data_structures/heap/src/heap.hpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
template <typename T, typename Cmp>
2+
my::Heap<T, Cmp>::Heap(std::vector<T> &input, const Cmp &cmp)
3+
: m_heap(input),
4+
m_size(input.size()),
5+
m_cmp(cmp)
6+
{
7+
make_heap();
8+
}
9+
10+
template <typename T, typename Cmp>
11+
template <typename RandomAccessIterator>
12+
my::Heap<T, Cmp>::Heap(RandomAccessIterator first, RandomAccessIterator last)
13+
: m_heap(first, last),
14+
m_size(last - first),
15+
m_cmp()
16+
{
17+
make_heap();
18+
}
19+
20+
template <typename T, typename Cmp>
21+
void my::Heap<T, Cmp>::make_heap()
22+
{
23+
for (int i = (m_size / 2) - 1; i >= 0; --i)
24+
{
25+
heapify_down(i);
26+
}
27+
}
28+
29+
template <typename T, typename Cmp>
30+
size_t Heap<T, Cmp>::parent(size_t ind)
31+
{
32+
return (ind - 1) / 2;
33+
}
34+
35+
template <typename T, typename Cmp>
36+
size_t my::Heap<T, Cmp>::left(size_t ind)
37+
{
38+
return 2 * ind + 1;
39+
}
40+
41+
template <typename T, typename Cmp>
42+
size_t my::Heap<T, Cmp>::right(size_t ind)
43+
{
44+
return 2 * ind + 2;
45+
}
46+
47+
template <typename T, typename Cmp>
48+
void my::Heap<T, Cmp>::heapify_down(size_t ind)
49+
{
50+
size_t largest = ind;
51+
size_t l = left(ind);
52+
size_t r = right(ind);
53+
54+
if (l < m_size && m_cmp(m_heap[l], m_heap[largest]))
55+
largest = l;
56+
57+
if (r < m_size && m_cmp(m_heap[r], m_heap[largest]))
58+
largest = r;
59+
60+
if (largest != ind)
61+
{
62+
std::swap(m_heap[ind], m_heap[largest]);
63+
heapify_down(largest);
64+
}
65+
}
66+
67+
template <typename T, typename Cmp>
68+
void my::Heap<T, Cmp>::print()
69+
{
70+
for (int i = 0; i < m_size; ++i)
71+
{
72+
std::cout << m_heap[i] << " ";
73+
}
74+
std::cout << std::endl;
75+
}

0 commit comments

Comments
 (0)