Indexing

Indexing is a way to organize records in a file

 

struct Index{

KEY key;

long int record;

};

 

 

Files in C

C supports two types of files:

Operations on files:

FILE* fopen (char * name, char mode)

void fclose (FILE* file)

size_t fread (void* b, size_t s, size_t n, FILE* f)

size_t fwrite (void* b, size_t s, size_t n, FILE* f)

 

int fseek (FILE* f, long offset, int base)

 

base can be:

SEEK_SET - from the beginning

SEEK_CUR - from current position

SEEK_END - from the end

int feof (FILE* f)

 

Example Create a file containing records of the following form:

struct Records{

char first[20] ;

char last[20];

int id_num; // this is the key

int age;

}

together with an index file

Linear indexing

 

Tree Indexing

2-3 Trees

Definition. 2-3 tree:

q a node contains one or two keys

q every internal node contains two or three children

q the leaves are all at the same level in the tree

q the tree has the search property:

structure TTNode{

int nkeys;

KEY lkey;

KEY rkey;

TTNode* left;

TTNode* center;

TTNode* right;

};

 

Searching in a 2-3 tree

Inserting and deleting in a 2-3 tree

 

Insertion

B-Trees

Definition. A B-Tree of order m is a tree with the following properties:

  1. The root is either a leaf, or it has at least two children
  2. Each node, except for the root has between m/2 and m children.
  3. All leaves are at the same level in the tree, so that the tree is always height balanced.

B+-Trees