[英]Is this a bug in Database System Concepts 6th edition-Figure 11.11 Querying a B+-tree.-procedure printAll(value V)?
在 Database System Concepts,第 6 版,第 11 章,在“圖 11.11 查詢 B+-樹”中,有一個過程printAll procedure printAll(value V)
。 它用於打印搜索鍵值為V的所有記錄(可以有重復)。 它調用find(V)
,它返回具有鍵V的第一個葉節點,以及該葉中具有該鍵的第一個索引i 。
為什么當i > number of keys in L
時代碼不包含Set i = 1
?
程序printAll(值 V )
/* 打印搜索鍵值為V的所有記錄 */
設置完成=假;
設置 ( L,i ) = 查找( V );
如果(( L,i ) 為空)返回
重復
重復
打印L.P i指向的記錄
設置i = i + 1
直到( i > L或LK i > V中的鍵數)
if ( i > L中的鍵數)
然后L = LP n // 不需要將i設置為 1?
否則設置完成=真;
直到(完成或L為空)
你是絕對正確的。 當移動到 B+ 樹的下一個葉子時, i
需要重置為 1。
在“數據庫系統概念的勘誤和更新,第6 版”中也沒有更正它
受本書啟發, 在 github 上有一個 C++ 實現,它確實重置了i 。 當然,在 C++ 中,索引是從零開始的:
void BPlusTree::printAll(string v) {
//
// B+ tree described in the book may contain duplicate keys
// printAl prints all the records with key v
// but here I modified it to print all records with key >= v
//
bool done = false;
auto pair = find(v);
auto p = pair.first;
auto i = pair.second;
if (i == -1) { // not found
cout << "no such record" << endl;
return;
}
cout << "i is " << i << endl;
while (!done and p != NULL) {
// be careful with special cases, may encounter NULL pointer, size is also a problem (#pointer = #key + 1)
for (; (i < p->_ptrs.size() && i < p->_keys.size() && v <= p->_keys[i]) or (i == p->_keys.size()); i++) {
if (p->_ptrs[i] == NULL) {
continue;
}
cout << "record: " << *reinterpret_cast<int*>(p->_ptrs[i]) << endl;
}
if (i >= _num_of_keys(p)) {
p = p->_next; // move to next leaf node
i = 0; // adjust i
}
else {
done = true;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.