-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLeftistHeap.h
188 lines (162 loc) · 3.38 KB
/
LeftistHeap.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
LestistHeap.h
左倾堆
2016/10/17
*/
#ifndef LEFTIST_H_
#define LEFTIST_H_
#include <algorithm>
#include <ostream>
#include "dsexception.h"
template<typename Comparable>
class LeftistHeap
{
public:
LeftistHeap() :root{ nullptr } {}
LeftistHeap(const LeftistHeap & rhs) // copy construct function
:root{nullptr}
{
root = clone(rhs.root);
}
LeftistHeap(LeftistHeap && rhs) // move constrcut function
:root{rhs.root}
{
rhs.root = nullptr;
}
LeftistHeap & operator=(const LeftistHeap & rhs)
{
LeftistHeap copy = rhs;
std::swap(*this, copy);
return *this;
}
LeftistHeap& operator=(LeftistHeap && rhs)
{
std::swap(root, rhs.root);
return *this;
}
~LeftistHeap()
{
makeEmpty();
}
bool isEmpty() const { return root == nullptr; }
void makeEmpty()
{
reclaimMemory(root);
root = nullptr;
}
void insert(const Comparable & x)
{
root = merge(new LeftistNode{ x }, root);
}
void insert(Comparable && x)
{
root = merge(new LeftistNode{ std::move(x) }, root);
}
const Comparable & findMin() const
{
if (isEmpty())
{
throw UnderflowException{};
}
return root->element;
}
void deleteMin()
{
if (isEmpty())
{
throw UnderflowException{};
}
LeftistNode *tmp = root;
root = merge(root->left, root->right);
delete tmp;
}
void deleteMin(Comparable & minItem)
{
minItem = findMin();
deleteMin();
}
void merge(LeftistHeap & rhs) // 合并两个堆
{
if (this == &rhs) // 不能自己与自己合并
{
return;
}
merge(root, rhs.root);
rhs.root = nullptr;
}
private:
// 节点
struct LeftistNode
{
Comparable element;
LeftistNode *left;
LeftistNode *right;
int npl; // null path length
LeftistNode(const Comparable & x, LeftistNode *l = nullptr, LeftistNode *r = nullptr, int _npl = 0)
:element{ x }, left{ l }, right{ r }, npl{ _npl }
{}
LeftistNode(Comparable && x, LeftistNode *l = nullptr, LeftistNode *r = nullptr, int _npl = 0)
:element{ std::move(x) }, left{ l }, right{ r }, npl{ _npl }
{}
};
LeftistNode *root; // 根节点
void swapChildNode(LeftistNode *p) // 交换节点的左右孩子的位置
{
LeftistNode *tmp = p->left;
p->left = p->right;
p->right = tmp;
}
LeftistNode * merge(LeftistNode *h1, LeftistNode *h2 )
{
// 首先处理一个堆为空的情况
if (h1 == nullptr)
{
return h2;
}
if (h2 == nullptr)
{
return h1;
}
// 确保h1的根节点元素较小
if (h1->element > h2->element)
{
return merge(h2, h1);
}
if (h1->left == nullptr) // 单个节点
{
h1->left = h2; // 将h2设置为h1的左节点即可
}
else // 递归
{
h1->right = merge(h1->right, h2); // 合并h1的右子树与h2,并设置为h1的右子树
// 此时可能会造成root节点违反左倾堆的性质
if (h1->left->npl < h1->right->npl)
{
swapChildNode(h1);
}
// 更新h1的npl
h1->npl = h1->right->npl + 1;
}
return h1;
}
// 回收内存
void reclaimMemory(LeftistNode *p)
{
// 使用后序遍历,但是对于不平衡树来说,可能导致栈空间不足
if (p != nullptr)
{
reclaimMemory(p->left);
reclaimMemory(p->right);
delete p;
}
}
LeftistNode * clone(LeftistNode *p) const // 复制节点
{
if (p == nullptr)
{
return nullptr;
}
return new LeftistNode{ p->element, clone(p->left), clone(p->right), p->npl };
}
};
#endif