高级数据结构_B树

B树

B代表Balanced,所有叶子节点都在同一层。

B树代码示例

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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include<iostream>
using namespace std;
#define M 5
#define MAXSIZE (M-1)
#define MINSIZE (M/2)
typedef char KeyType;
typedef struct Record {} Record;
typedef struct ElemType
{
KeyType key;
Record* recptr;
}ElemType;
typedef struct BNode
{
int num;
struct BNode* parent;
ElemType data[M + 1];
struct BNode* sub[M + 1];
}BNode;
typedef struct BTree
{
struct BNode* root;
int cursize;
}BTree;
typedef struct Result
{
struct BNode* pnode;
int index;
bool tag;
}Result;
BNode* Buynode()
{
BNode* s = (BNode*)malloc(sizeof(BNode));
if (nullptr == s)exit(1);
memset(s, 0, sizeof(BNode));
return s;
}
void Init_BTree(BTree& tree)
{
tree.root = nullptr;
tree.cursize = 0;
}
Result FindKey(BTree& tree, KeyType kx)
{
Result res = { nullptr, -1,false };
struct BNode* ptr = tree.root;
while (ptr != nullptr)
{
ptr->data[0].key = kx;
int i = ptr->num;
while (i>=0 && ptr->data[i].key)
{
--i;
}
res.pnode = ptr;
res.index = i;
if (i > 0 && kx == ptr->data[i].key)
{
res.tag = true;
break;
}
ptr = ptr->sub[i];
}
return res;
}
BNode* MakeRoot(const ElemType& item, BNode* left, BNode* right)
{
BNode* s = Buynode();
s->num = 1;
s->parent = nullptr;
s->data[1] = item;
s->sub[0] = left;
if (left != nullptr)left->parent = s;
s->sub[1] = right;
if (right != nullptr)right->parent = s;
return s;
}
void Insert_Item(BNode* ptr, int pos, const ElemType& item, BNode* right)
{
for (int i = ptr->num; i > pos; --i)
{
ptr->data[i + 1] = ptr->data[i];
ptr->sub[i + 1] = ptr->sub[i];
}
ptr->data[pos + 1] = item; //?
ptr->sub[pos + 1] = right; //?
ptr->num += 1;
}
ElemType Move_Item(BNode* s, BNode* ptr, int pos)
{
for (int i = 0, j = pos + 1; j <= ptr->num; ++i, ++j)
{
s->data[i] = ptr->data[j];
s->sub[i] = ptr->sub[j];
if (s->sub[i] != nullptr)
{
s->sub[i]->parent = s;
}
}
s->num = MINSIZE;
ptr->num = MINSIZE;
s->parent = ptr->parent;
return s->data[0];
}
BNode* Splice(BNode* ptr)
{
BNode* s = Buynode();
ElemType item = Move_Item(s, ptr, MINSIZE);
if (ptr->parent == nullptr)
{
return MakeRoot(item, ptr, s);
}
BNode* pa = ptr->parent;
int pos = pa->num;
pa->data[0] = item;
while (pos > 0 && item.key < pa->data[pos].key) { --pos; }
Insert_Item(pa, pos, item, s);
if (pa->num > MAXSIZE)
{
return Splice(pa);
}
else
{
return nullptr;
}
}
bool Insert(BTree& tree, const ElemType& item)
{
if (tree.root == nullptr)
{
tree.root = MakeRoot(item, nullptr, nullptr);
tree.cursize = 1;
return true;
}
//不为空
Result res = FindKey(tree, item.key);
if (res.pnode != nullptr && res.tag)return false;
//在res.pnode处插入
BNode* ptr = res.pnode;
int pos = res.index;
Insert_Item(ptr, pos, item, nullptr);

if (ptr->num > MAXSIZE)
{
BNode* newroot = Splice(ptr); //分裂
if (newroot != nullptr)
{
tree.root = newroot;
}
}
tree.cursize += 1;
return true;
}
BNode* FindPrev(BNode* ptr, int pos)
{
BNode* p = ptr->sub[pos];
while (p != nullptr && p->sub[p->num])
{
p = p->sub[p->num];
}
return p;
}
BNode* FindNext(BNode* ptr, int pos)
{
BNode* p = ptr->sub[pos];
while (p != nullptr && p->sub[0])
{
p = p->sub[0];
}
return p;
}
bool Remove(BTree& tree, KeyType kx)
{
Result res = FindKey(tree, kx);
if (res.pnode == nullptr || !res.tag)return false;
BNode* ptr = res.pnode;
int pos = res.index;
BNode* pre = FindPrev(ptr, pos - 1);
BNode* nt = FindNext(ptr, pos);
if (pre != nullptr && ptr->num > MINSIZE)
{
ptr->data[pos] = pre->data[ptr->num];
ptr = pre;
pos = pre->num;
}
else if (nt != nullptr && nt->num > MINSIZE)
{
ptr->data[pos] = nt->data[1];
ptr = nt;
pos = 1;
}
else if (pre != nullptr)
{
ptr->data[pos] = pre->data[pre->num];
ptr = pre;
pos = pre->num;
}
Delete_Leaf(ptr, pos);
if (ptr->parent == nullptr && ptr->num == 0)
{
free(ptr);
tree.root = nullptr;
}
else if (ptr->num < MINSIZE)
{
BNode* newroot = Merge_Leaf(ptr);
if (newroot != nullptr)
{
tree.root = newroot;
}
}
}
int main()
{
BTree myt;
Init_BTree(myt);
char ch[] = { "qwertyuiopasdfghjklzxcvbnm" };
int i = 0;
while (ch[i] != '\0')
{
ElemType item = { ch[i],nullptr };
cout << Insert(myt, item);
i++;
}
cout << endl;
}

B*

与B+树的结构一致。

但是

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
bool Move_Left_Right(BNode * ptr)
{
bool res = false; //是否移动
BNode * left = ptr->prev;
BNode * right = ptr->next;
if(left != nullptr && left->num < LEAFMAX)
{
//左移
res = true;
}else if(right != nullptr && right->num < LEAFMAX)
{
//右移
res = true;
}
return res;
}
bool Insert(BTree& tree, KeyType kx, Record * rec)
{
if (tree.root == nullptr)
{
BNode *s = BuyLeaf();
s->key[0] = kx;
s->recptr[0] = rec;
s->num = 1;
tree.root = tree.first = s;
return true;
}
//不为空
Result resr = FindRoot(tree.root, kx);
Result resf = FindLeaf(tree.first, kx);
if(resf.pnode == nullptr){cout << "BTree struct err"<<endl;return false;}
if (resf.tag){cout << "exist"<<endl;return false;}
//在res.pnode处插入
BNode* ptr = resf.pnode;
int pos = resf.index;
Insert_Leaf_Item(ptr, pos, kx, rec);

if (ptr->num > LEAFMAX)
{
// 移动
BNode* newroot = Splice_Leaf(ptr); //分裂
if (newroot != nullptr)
{
tree.root = newroot;
}
}
tree.cursize += 1;
return true;
}

C语言_restrict

参考文章:C++ 需要 restrict 关键字吗? - 蓝色的回答 - 知乎

演示

C/Cpp编译器,无论是GCC,Clang,VCpp,IBM XL Cpp等,这些主流的Cpp编译器都提供了restrict关键字的支持,只是书写的形式有所变化,如可能是__restrict__,__restrict等 ,而restrict是限制Pointer Alias的,限制Pointer Alias有助于编译器做优化,这和unique_ptr完全是两码事。

restrict

以GCC产生汇编指令的例子来补充一下,比较直观

1
2
3
4
5
void f(int *a, int *b, int *c)
{
*a += *c;
*b += *c;
}

-O3后的汇编代码

1
2
3
4
5
6
f(int*, int*, int*):
movl (%rdx), %eax
addl %eax, (%rdi)
movl (%rdx), %eax
addl %eax, (%rsi)
ret

加上restrict

1
2
3
4
5
void f(int * __restrict__ a, int* __restrict__ b, int* __restrict__ c)
{
*a += *c;
*b += *c;
}

-O3后

1
2
3
4
5
f(int*, int*, int*):
movl (%rdx), %eax
addl %eax, (%rdi)
addl %eax, (%rsi)
ret

可以很清楚的看见是4条指令变为了3条指令,而少掉的一条就是第二次的load c

unique_ptr

1
2
3
4
5
6
7
#include <memory>
using namespace std;
void f(std::unique_ptr<int> a, std::unique_ptr<int>b, std::unique_ptr<int> c)
{
*a += *c;
*b += *c;
}

-O3 -std=c++11

1
2
3
4
5
6
7
8
9
10
11
12
13
14
f(std::unique_ptr<int, std::default_delete<int> >, std::unique_ptr<int, std::default_delete<int> >, std::unique_ptr<int, std::default_delete<int> >):
movq (%rdx), %rdx
movq (%rdi), %rax

; *a += *c
movl (%rdx), %ecx
addl %ecx, (%rax)

movq (%rsi), %rax

; *b += *c
movl (%rdx), %edx
addl %edx, (%rax)
ret

所以,可见,unique_ptr和restrict完全是两码事。

个人理解

至于为什么会和unique_ptr混淆,因为他两个语义上有共同之处:只有一个指针指向某个变量。但是restrict的目的是给编译器做优化,而且它的限制效果与const修饰符很类似,需要自己去体会;


评论区见解:

提问者 - dawnmist:按照unique_ptr的语义,它是资源的唯一持有者。如果资源是内存,那也只能通过这一个unique_ptr访问。用unique_ptr时,编译器能不能像restrict那样优化这个指针访问呢?

回答者 - 蓝色:不行,unique_ptr可以通过get()方法转为原始指针;unique_ptr只是一个智能指针,所以你不能这样限制unique_ptr的语义。