-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.h
120 lines (114 loc) · 1.69 KB
/
heap.h
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#pragma once
#ifndef HEAP_H
#define HEAP_H
#include<vector>
#include<iostream>
#include<cassert>
using namespace std;
//template<typename T>
class Maxheap
{
private:
vector<int> Item;
int capactiy;
int count;
void shiftup(int k)
{
while (k > 1)
{
if (Item[k] > Item[k / 2])
swap(Item[k], Item[k / 2]);
k /= 2;
}
}
void shiftdown(int k)
{
while (k*2 <= count)
{
int j = 2 * k;
if (j + 1 <= count && Item[j] < Item[j + 1])
{
j = j + 1;
}
if (Item[k] < Item[j])
swap(Item[k], Item[j]);
k = j;//kûÓб仯£¬j±äÁË
}
}
public:
Maxheap(int n)
{
this->Item = vector<int>(n + 1);
this->capactiy = n;
count = 0;
};
Maxheap(vector<int> arr,int n)
{
Item = vector<int>(n + 1);
capactiy = n;
for (int i = 0; i < n; i++)
{
Item[i+1] = arr[i];
}
count = n;
int i = n / 2;
for (i; i > 0; i--)
{
shiftdown(i);
}
}
~Maxheap() {};
void isEmpty()
{
if (count <= 0)
cout << "is empty";
else
cout << "not empty";
}
int size()
{
return count;
}
void insert(int item)
{
assert(count < capactiy);
Item[count + 1] = item;
count++;
shiftup(count);
}
int out()
{
assert(count > 0);
int ret = Item[1];
swap(Item[1], Item[count]);
count--;
shiftdown(1);
return ret;
}
};
void __shiftdown(vector<int> &arr, int n, int k)
{
while (2 * k + 1 < n)
{
int j = 2 * k + 1;
if (j + 1 < n&&arr[j] < arr[j + 1])
j = j + 1;
if (arr[k] < arr[j])
swap(arr[k], arr[j]);
k = j;
}
}
void heapsort(vector<int> &arr, int n)
{
//heapify
for (int i = (n - 1) / 2; i >= 0; i--)
{
__shiftdown(arr, n, i);
}
for (int i = n - 1; i > 0; i--)
{
swap(arr[i], arr[0]);
__shiftdown(arr, n, 0);
}
};
#endif // !HEAP_H