简体   繁体   中英

creating index file using B+ trees

I have this interesting challenge where a B+ tree is the index for a data file. This index has to be saved in a index file, and later we have to load the index into memory from this index file.

This is the B+ tree structure I have, from http://www.amittai.com/prose/bpt.c

typedef struct node 
{
    void ** pointers;
    int * keys;
    struct node * parent;
    bool is_leaf;
    int num_keys;
} node; 

As you can see the code for the tree is pretty neat and works properly, so now I have a b+ tree functioning as index. But I can't simply write each node from the tree into a file... The pointers written there wouldn't work on a new execution. How implementation wise I even start to create an index file using a B+ tree? Reminder that after creating it, index has to be loaded back into memory using the index file.

Translate your pointers to id's (should be a one-to-one correspondence between memory address of a node and a unique id). Then traverse your tree in postfix order or something writing the ids instead of the pointers.

To reconstruct the tree, you simply read the data from file, and each new id you see, malloc some memory for it. (If your ids are sequential from zero, then you can do this real quick and easy using a fixed size array for a hash table).

There are probably much clever-er ways to do this if you want to load the index real fast.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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