简体   繁体   English

BTree中的二分搜索以提高性能

[英]Binary search within BTree to improve performance

I'm reading through Cormen et.我正在阅读 Cormen 等。 al.人。 again, specifically the chapter on BTrees, and I am implementing one in C.再次,特别是关于 BTrees 的章节,我正在 C 中实现一个。 However, I had a question about whether I could improve performance in searching by using binary search rather than linear search.但是,我有一个问题是我是否可以通过使用二进制搜索而不是线性搜索来提高搜索性能。 The pseudocode given in the book looks something like:书中给出的伪代码如下所示:

ordered_pair* btree_search(btreenode* root, double val){
  i = 0;
  while(i < root->num_vals && val > x->vals[i]) i++;
  if(i < x->num_keys && val == x->vals[i]) return ordered_pair(x, i);
  else if (x->leaf) return NULL;
  else {
    disk_read(x->children[i]);
    return btree_search(x->children[i], val);
  }
}

(I have modified it to look like C and used index 0 rather than 1) (我已将其修改为 C 并使用索引 0 而不是 1)

My question(s):我的问题:

This looks like a linear search.这看起来像一个线性搜索。 However, since each collection of keys in a BTree node is implemented as an array, couldn't I use binary search instead?但是,由于 BTree 节点中的每个键集合都是作为数组实现的,所以我不能使用二进制搜索来代替吗? Would that lessen the time complexity of searching each node from O(n) to O(lg(n))?这会减少从 O(n) 到 O(lg(n)) 搜索每个节点的时间复杂度吗? Or does reading from the disk make a binary search here rather pointless.或者从磁盘读取是否使二进制搜索在这里变得毫无意义。 The reason I'm asking this is because it seems relatively trivial to implement, and I am confused why Cormen et.我问这个的原因是因为实现起来似乎相对微不足道,我很困惑为什么 Cormen et. al., doesn't mention it at all. al.,根本没有提到它。 Or perhaps I am just missing something.或者也许我只是错过了一些东西。

If you take the time to answer or attempt to answer this question, thank you for your time!如果您花时间回答或尝试回答此问题,感谢您抽出宝贵时间!

Yes, you can definitely use a binary search.是的,您绝对可以使用二进制搜索。

Whether or not there's an advantage in it depends on how big your blocks are and how much it cost to read them, but as you say, it's not difficult, and it's not going to be slower.它是否有优势取决于您的块有多大以及读取它们的成本是多少,但是正如您所说,这并不难,而且不会变慢。

I would always do this with a binary search.我总是用二进制搜索来做到这一点。

Perhaps the authors just didn't want to complicate the lesson on B-Trees, or maybe they they are assuming that you've already spent a lot more time reading those blocks in the first place.也许作者只是不想使 B-Trees 的课程复杂化,或者他们可能假设您已经花了更多时间阅读这些块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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