简体   繁体   English

如何在 B-Tree 上实现 Disk-Read() 和 Disk-Write() 的操作?

[英]How can i implement the operations of Disk-Read() and Disk-Write() on B-Tree?

I'm implementing B-trees in c language.我正在用 c 语言实现 B 树。 To implement the b-trees I am following a certain pseudocode.为了实现 b 树,我遵循了某个伪代码。 In following this pseudocode i came across Disk-Read () and Disk-Write() operations that i don't know how to implement.在遵循这个伪代码时,我遇到了我不知道如何实现的 Disk-Read () 和 Disk-Write() 操作。 The idea is to save all the nodes in secondary memory excluding the root of the B-tree and every time I have to read a node I perform a Disk-Read () operation in secondary memory and every time I want to write to it to modify its value I perform a Disk-Write () operation in secondary memory.这个想法是将所有节点保存在辅助 memory 中,不包括 B 树的根,每次我必须读取一个节点时,我都会在辅助 memory 中执行磁盘读取()操作,并且每次我想写入它时修改它的值 我在辅助 memory 中执行 Disk-Write() 操作。 Could anyone help me to implement these two procedures in c language?谁能帮我用 c 语言实现这两个程序?

I insert the pseudocodes of the search operation and the creation of an empty b-tree where these two procedures are called.我插入了搜索操作的伪代码并创建了一个空的 b 树,这两个过程被调用。

B-TreeCreate(T)
  x = Allocate()
  x.leaf = True
  x.n = 0
  DiskWrite(x)
  T.root = x

B-TreeSearch(x,k)
 i = 1
 while ((i ≤ x.n) and (k > x.keyi )) i = i + 1
  if ((i ≤ x.n) and (k = x.keyi ))
    then return (x, i)
  if (x.leaf = True)
   then return nil
  DiskRead(x.ci )
return BTreeSearch(x.ci,k)

Thanks again再次感谢

Typically you would use either open() coupled with read()/write() or fopen() coupled with fread()/fwrite().通常你会使用 open() 和 read()/write() 或者 fopen() 和 fread()/fwrite()。 If trying to make more than a toy implementation you likely want to abstract this part away so that the IO system is easily replaced.如果尝试制作的不仅仅是一个玩具实现,您可能希望将这部分抽象出来,以便轻松替换 IO 系统。 (For example, if building for Windows there may be reason to use CreateFile() along with ReadFile()/WriteFile() instead). (例如,如果为 Windows 构建,则可能有理由同时使用 CreateFile() 和 ReadFile()/WriteFile())。 With proper I/O abstraction it would also be possible to have your btree backed by a compressed file.通过适当的 I/O 抽象,您的 btree 也可以由压缩文件支持。

Thee three sets of functions take different arguments, in different orders, but in the end all perform the same operations, that is opening a file and either transferring bytes from secondary storage to memory or transferring bytes from memory to secondary storage.这三组函数采用不同的 arguments,顺序不同,但最终都执行相同的操作,即打开文件并将字节从辅助存储传输到 memory 或将字节从 ZCD69B4957F06CD818D7BF3D61980E2 传输到辅助存储。

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

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