-
Notifications
You must be signed in to change notification settings - Fork 2
/
heap.hpp
49 lines (42 loc) · 1.05 KB
/
heap.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#ifndef HEAP_H_
#define HEAP_H_
#include <cstdlib>
#include "types.hpp"
template<typename T, typename Comp>
class Heap
{
private:
Int cap_;
Int num_;
T* data_;
void heapify();
void siftDown(const Int&);
void resize(const Int&, const Int&);
public:
Heap() : cap_(1), num_(0), data_(nullptr)
{
data_ = new T [cap_];
};
Heap(T* data, const Int& num) : cap_(num), num_(num), data_(nullptr)
{
data_ = new T [num];
for(Int i = 0; i < num; ++i)
data_[i] = data[i];
heapify();
};
~Heap() { delete [] data_;}
Int size() const { return num_; }
T at(const Int& i) const { return data_[i]; }
T top() const { return data_[0]; }
bool isLeaf(const Int&) const;
bool is_empty() const;
Int leftChild(const Int&) const;
Int rightChild(const Int&) const;
Int parent(const Int&) const;
void push_back(const T&);
T pop_back();
T pop(const Int&);
void push_back(const Heap<T,Comp>&);
void push_back(T*, const Int&);
};
#endif