繁体   English   中英

这是 Database System Concepts 6th edition-Figure 11.11 Querying a B+-tree.-procedure printAll(value V) 中的错误吗?

[英]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 > LLK 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM