【问题标题】:How to rotate binary trees on disk如何在磁盘上旋转二叉树
【发布时间】:2016-05-26 05:35:45
【问题描述】:

我正在设计一个仅在磁盘上的 avl 树库。

树的每个节点如下。

struct node 
{
   int key;
   unsigned char height;
   int64 left;
   int64 right;
}; 

并且每个节点在创建时都会保存到一个文件中。

左右字段是文件的偏移量 左右子节点所在的位置。

到目前为止一切正常,除了树的旋转。

如果节点在内存中,则旋转如下。

node* rotateright(node* p)
{
   node* q = p->left;
   p->left = q->right;
   q->right = p;
   fixheight(p);
   fixheight(q);
   return q;
}

但是,我在文件中使用偏移量而不是内存。

int64 rotateright(int64 p)
{
  node q_node;
  node p_node;

  seek(fp,p*sizeof(node));
  read(fp,sizeof(node),&p_node);

  seek(fp,p.left*sizeof(node));
  read(fp,sizeof(node),&q_node);

  p.left=q_node.right;

  // etc...
} 

我无法让这个功能正常工作。

【问题讨论】:

    标签: sorting data-structures binary-search-tree


    【解决方案1】:

    磁盘不是 AVL 树的合适媒介,因为:

    1) 您不能只从磁盘读取一点点数据。您将始终至少获得一个扇区,并且可以免费获得更多;和

    2) 从磁盘上的任意位置读取是非常昂贵的。

    由于这些原因,基于磁盘的搜索树(例如用于数据库索引)使用不同的数据结构。 B+树很常见:

    https://en.wikipedia.org/wiki/B%2B_tree

    你应该这样做。

    使用基于磁盘的 AVL 树查找项的访问次数大约是使用 B+ 树查找项的 10 倍。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 2021-05-18
      相关资源
      最近更新 更多