【发布时间】: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