From 289dcaa6f45429b0c68e1490743b9d73d698678e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nicol=C3=A1s=20Dimov?= Date: Tue, 25 May 2004 21:23:47 +0000 Subject: [PATCH] primera parte del insertar --- emufs/b_plus.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/emufs/b_plus.c b/emufs/b_plus.c index 61e7b4c..ab84026 100644 --- a/emufs/b_plus.c +++ b/emufs/b_plus.c @@ -8,7 +8,8 @@ int b_plus_grabar_nodo(INDEXSPECS *idx, NODO_B_PLUS *nodo, int num_node); NODO_B_PLUS *b_plus_crearnodo(INDEXSPECS *idx); int b_plus_destruir_nodo(NODO_B_PLUS *nodo); /*int b_plus_insertar_clave(INDEXSPECS *idx, INDEX_DAT *query);*/ - +int b_plus_insert_nonfull(INDEXSPECS *idx, NODO_B_PLUS *nodo, int num_nodo, int num_nodo_padre, CLAVE clave); +int b_plus_split_child(INDEXSPECS *idx,NODO_B_PLUS *new_root, int ,NODO_B_PLUS* raiz); /**#*#*#*#*#**#*#*#*#*#*FIN PROTOTYPES*#*#*#*#*#**#*#*#*#*#**#*#*#*#*#*/ /** Crea un nuevo nodo y lo inicializa */ @@ -276,3 +277,66 @@ int b_plus_destruir_nodo(NODO_B_PLUS *nodo) free(nodo); return 0; } + +int b_plus_insert_nonfull(INDEXSPECS *idx, NODO_B_PLUS *nodo, int num_nodo, int num_nodo_padre, CLAVE clave) +{ + int i, num_nodo_hijo; + NODO_B_PLUS *hijo; + + i = nodo->cant_claves; + if ( nodo->nivel == 0 ){ + while ( i >= 1 && clave.i_clave < nodo->claves[i] ){ + nodo->claves[i+1] = nodo->claves[i]; + i--; + } + nodo->claves[i+1] = clave.i_clave; + nodo->cant_claves++; + b_plus_destruir_nodo(nodo); + b_plus_grabar_nodo(idx, nodo, num_nodo); + } else { + while ( i >= 1 && clave.i_clave < nodo->claves[i] ) + i--; + i++; + num_nodo_hijo = nodo->hijos[i-1]; + hijo = b_plus_leer_nodo(idx, num_nodo_hijo); + if ( hijo->cant_claves == idx->size_claves/sizeof(int) ) { + b_plus_split_child(idx, nodo, i, hijo); + if ( clave.i_clave > nodo->claves[i] ) + i++; + } + b_plus_insert_nonfull(idx, hijo, num_nodo_hijo, num_nodo_padre); + } + b_plus_destruir_nodo(hijo); + return 0; +} + +int b_tree_insertar(INDEXSPECS *idx, CLAVE clave) +{ + NODO_B_PLUS *raiz; + + raiz = b_plus_leer_nodo(idx, 0); + if ( raiz->cant_claves == idx->size_claves/sizeof(int) ) { + NODO_B_PLUS *new_root = b_plus_crearnodo(idx); + new_root->nivel = raiz->nivel + 1; + new_root->hijos[0] = b_plus_get_num_nodo(idx); + b_plus_grabar_nodo(idx, raiz, new_root->hijos[0]); + b_plus_grabar_nodo(idx, new_root, 0); + b_plus_split_child(idx, new_root, 1, raiz); + b_plus_insert_nonfull(idx, new_root, 0, clave); + } else b_plus_insert_nonfull(idx, raiz, 0, clave); + + return 0; +} + +int b_plus_get_num_nodo(INDEXSPECS *idx) +{ + FILE *fp; + int num; + + fp = fopen(idx->filename, "r+"); + if (fp == NULL) return -1; + + num = ftell(fp)/sizeof(NODO_B_PLUS); + fclose(fp); + return num; +} -- 2.43.0