// Funcionando Node* create_node(int nl, Node_type t, char* lex, void* att, int nbc, Node** children) { // Testes nbc = numero de filhos if (nbc >= MAX_CHILDREN_NUMBER) { printf("ERRO! Número de filhos da árvore é maior que o permitido."); exit(-1); } else if (nbc <= 0) { printf("ERRO! Número de filhos deve ser maior ou igual a 0."); exit(-1); } // Testa o tipo do nodo else if ((t < 299) || (t > 363)) { printf("ERRO! Tipo do nodo incorreto."); exit(-1); } // Cria novo nodo e aloca memória Node * newNode = create_leaf(nl, t, lex, att); // Termina de repassar últimos atributos recebidos na função newNode->num_child = nbc; newNode->child = children; // Retorna o novo nodo criado return newNode; }
int main () { Node *pf1, *pf3, *pf0; Node **children; pf1 = create_leaf(1, int_node, "1", NULL); pf3 = create_leaf(1, int_node, "2", NULL); pack_nodes(&children, 0, pf1); pack_nodes(&children, 1, pf3); printf("h = %d\n", height(pf1)); /* deve imprimir 1 */ pf0 = create_node(1, int_node, "0", NULL, 2, children); /* pf0 é o pai de pf1 e pf3 */ printf("h = %d\n", height(pf0)); /* deve imiprimir 2 */ printf("n = %d\n", nb_of_children(pf0)); /* deve imprimir 2 */ printf("n = %d\n", nb_of_children(child(pf0, 0))); /* deve imprimir 0, porque o retorno de child é o filho de pf0, que é uma folha. */ deep_free_node(pf0); }
bst_node_t *convert_arr_to_bst(int *arr, int start, int end) { if (start > end) { return NULL; } int mid = (start + end)/2; bst_node_t *root=create_leaf(); root->data = arr[mid]; root->left = convert_arr_to_bst(arr, start, mid-1); root->right = convert_arr_to_bst(arr, mid+1, end); return root; }
/* * EFFECT: * - alloc ptrs with nonleaf or leaf * REQUIRE: * - node has height * - node has n_children */ void node_ptrs_alloc(struct node *node) { int i; for (i = 0; i < node->n_children; i++) { struct child_pointer *ptr = &node->parts[i].ptr; if (node->height > 0) ptr->u.nonleaf = create_nonleaf(); else ptr->u.leaf = create_leaf(); } }
int main(int argc, char *argv[]) { /* * n * t a * b * */ Node** children1; Node** children2; Node* b = create_leaf(4,305, "lexeme ( b ) \n", NULL); pack_nodes(&children1, 0, b); Node* t = create_node(23, 305, "lexeme ( t ) \n", NULL, 1, children1); Node* a = create_leaf(5, 302, "lexeme ( a ) \n", NULL); pack_nodes(&children2, 0, t); pack_nodes(&children2, 1, a); Node* n = create_node(5, 302, "lexeme ( n ) : root \n", NULL, 2, children2); /* imprime a altura da arvore */ printf ( "height: %d \n", height(a)); printf ( "%s" , n->lexeme); printf ( "%s" , child(n,1)->lexeme); printf ( "%s" , child(n,2)->lexeme); printf ( "%s", child(child(n,1),1)->lexeme); deep_free_node(n); return 0; }
void main(){ FILE *fcsv = fopen("data.csv", "r"); FILE *fbin = fopen("students.bin", "wb+"); struct NonLeafNode root; memset(&root, 0, sizeof(root)); fwrite(&root, sizeof(root), 1, fbin); printf("%d,", ftell(fbin)); int count = 1; int keys_count = 0; struct leafNode lp; while (!feof(fcsv)) { struct Student stds[10]; for (int i = 0; i < 10; i++){ memset(&stds[i], 0, sizeof(struct Student)); //memset(&lp.data[i], 0, sizeof(lp.data[i])); //fscanf(fcsv, "%d,%28[^,],%32[^\n]", &lp.data[i].rollno, lp.data[i].name, lp.data[i].college); fscanf(fcsv, "%d,%28[^,],%32[^\n]", &stds[i].rollno, stds[i].name, stds[i].college); } lp = create_leaf(stds); fseek(fbin, 512 * count, 0); fwrite(&lp, sizeof(lp), 1, fbin); printf("%d,", ftell(fbin)); count++; if (keys_count != 0) root.keys[keys_count - 1] = lp.data[0].rollno; root.offsets[keys_count] = keys_count * 512; keys_count++; } fseek(fbin, 0, 0); fwrite(&root, sizeof(root), 1, fbin); printf("\n%d,", ftell(fbin)); printf("%d\n", count); for (int i = 0; i < 63; i++){ printf("%d,", root.keys[i]); } fclose(fcsv); fclose(fbin); getchar(); }
bool build_rec(int d, struct list *s, struct tree **t) //@ requires list(s, ?vs) &*& pointer(t, _); /*@ ensures list(s, ?rvs) &*& pointer(t, ?rt) &*& switch (build_rec1(d, vs)) { case fail: return result == false; case success(rvt, rvs0): return result == true &*& rvs == rvs0 &*& tree(rt, rvt); }; @*/ // decreases max_func(0, fold_left(0, max_func, vs) - d); // Not yet checked by VeriFast. { //@ build_rec1_eq(d, vs); struct tree *l; struct tree *r; if (list_is_empty(s)) return false; int h = list_head(s); if (h < d) return false; if (h == d) { list_pop(s); struct tree *leaf = create_leaf(); *t = leaf; return true; } if (!build_rec(d+1, s, &l)) return false; if (!build_rec(d+1, s, &r)) { tree_dispose(l); return false; } struct tree *node = create_node(l, r); *t = node; return true; }
void main(){ FILE *fcsv = fopen("data.csv", "r"); FILE *fbin = fopen("students.bin", "wb+"); FILE *fscsv = fopen("teacherdata.csv", "r"); struct TableSector ts; memset(&ts, 0, sizeof(ts)); fwrite(&ts, sizeof(ts), 1, fbin); int std_count = 0, staff_count = 0; int std_keys_count = 0, staff_keys_count = 0; struct leafNode lp; struct TleafNode tlp; memset(&lp, 0, sizeof(struct leafNode)); memset(&tlp, 0, sizeof(struct TleafNode)); struct NonLeafNode nln, tnln; int nonleaf_count = 0; memset(&nln, 0, sizeof(struct NonLeafNode)); memset(&tnln, 0, sizeof(struct NonLeafNode)); int prev = INT_MAX; printf("Student id's are:"); while (!feof(fcsv)) { struct Student stds[10]; for (int i = 0; i < 10; i++){ memset(&stds[i], 0, sizeof(struct Student)); fscanf(fcsv, "%d,%28[^,],%32[^\n]", &stds[i].rollno, stds[i].name, stds[i].college); printf("%d,", stds[i].rollno); if (i < 7){ memset(&tlp.data[i], 0, sizeof(tlp.data[i])); tlp.pageType = 1; fscanf(fscsv, "%d,%24[^,],%12[^,],%24[^\n]", &tlp.data[i].staffid, tlp.data[i].name, tlp.data[i].college, tlp.data[i].dept); } } lp = create_leaf(stds); lp.tableid = 1; fseek(fbin, (512 * (std_count + staff_count)) + sizeof(ts), 0); //printf("std- %d\t", ftell(fbin)); fwrite(&lp, sizeof(lp), 1, fbin); //printf("%d\n", ftell(fbin)); std_count++; if (std_keys_count != 0) nln.keys[std_keys_count - 1] = lp.data[0].rollno; nln.offsets[std_keys_count] = (std_count + staff_count - 1) * 512 + sizeof(ts); std_keys_count++; if (std_count != 1){ //printf("%d\n", ftell(fbin)); int std_rootoffset = ftell(fbin); nln.pageType = 2; fwrite(&nln, sizeof(nln), 1, fbin); nonleaf_count++; std_count++; fseek(fbin, 0, 0); ts.rootid[0] = std_rootoffset; ts.tableid[0] = 1; fwrite(&ts, sizeof(ts), 1, fbin); } tlp.tableid = 2; fseek(fbin, (512 * (std_count + staff_count)) + sizeof(ts), 0); //printf("staff- %d\t", ftell(fbin)); prev = ftell(fbin); fwrite(&tlp, sizeof(tlp), 1, fbin); //printf("%d\n", ftell(fbin)); staff_count++; if (staff_keys_count != 0) tnln.keys[staff_keys_count - 1] = tlp.data[0].staffid; tnln.offsets[staff_keys_count] = (std_count + staff_count - 1) * 512 + sizeof(ts); staff_keys_count++; if (staff_count != 1){ int staff_rootoffset = ftell(fbin); tnln.pageType = 2; fwrite(&tnln, sizeof(tnln), 1, fbin); nonleaf_count++; staff_count++; fseek(fbin, 0, 0); ts.rootid[1] = staff_rootoffset; ts.tableid[1] = 2; fwrite(&ts, sizeof(ts), 1, fbin); } } fclose(fcsv); fclose(fbin); int id; printf("\nEnter Student id:"); scanf("%d", &id); getStaffCount(getCollege(id)); getchar(); getchar(); }
/* * +-----------------------------------------------+ * | 5 | 7 | 9 | * +-----------------------------------------------+ * | * +---------------+ * | 60 | 61 | 62 | * +---------------+ * * +---------------------------------------------------------------+ * | 5 | 60 | 7 | 9 | * +---------------------------------------------------------------+ * | | * +--------+ +---------+ * | 60 | | 61 | 62 | * +--------+ +---------+ * * * ENTER: * - node is already locked(L_WRITE) * EXITS: * - a is locked(L_WRITE) * - b is locked(L_WRITE) */ static void _node_split(struct tree *t, struct node *node, struct node **a, struct node **b, struct msg **split_key) { int i; int pivots_old; int pivots_in_a; int pivots_in_b; struct node *nodea; struct node *nodeb; struct msg *spk; __DEBUG("nonleaf split begin, NID %"PRIu64"" ", nodesz %d" ", nodec %d" ", children %d" , node->nid , node_size(node) , node_count(node) , node->n_children); nodea = node; pivots_old = node->n_children - 1; nassert(pivots_old > 2); pivots_in_a = pivots_old / 2; pivots_in_b = pivots_old - pivots_in_a; /* node a */ nodea->n_children = pivots_in_a + 1; /* node b */ NID nid = hdr_next_nid(t->hdr); node_create_light(nid, node->height > 0 ? 1 : 0, pivots_in_b + 1, t->hdr->version, t->e, &nodeb); cache_put_and_pin(t->cf, nid, nodeb); for (i = 0; i < (pivots_in_b); i++) nodeb->pivots[i] = nodea->pivots[pivots_in_a + i]; for (i = 0; i < (pivots_in_b + 1); i++) nodeb->parts[i] = nodea->parts[pivots_in_a + i]; /* the rightest partition of nodea */ struct child_pointer *ptr = &nodea->parts[pivots_in_a].ptr; if (nodea->height > 0) ptr->u.nonleaf = create_nonleaf(t->e); else ptr->u.leaf = create_leaf(t->e); /* split key */ spk = msgdup(&node->pivots[pivots_in_a - 1]); node_set_dirty(nodea); node_set_dirty(nodeb); __DEBUG("nonleaf split end, nodea NID %"PRIu64"" ", nodesz %d" ", nodec %d" ", children %d" , nodea->nid , node_size(nodea) , node_count(nodea) , nodea->n_children); __DEBUG("nonleaf split end, nodeb NID %"PRIu64"" ", nodesz %d" ", nodec %d" ", children %d" , nodeb->nid , node_size(nodeb) , node_count(nodeb) , nodeb->n_children); *a = nodea; *b = nodeb; *split_key = spk; }
/* build a tree as the subtree of subroot, with particles * first to first + npar. * returns the number of particles that are out of the box * in skipped. * */ static void tree_build_subtree(TreeStore * store, Node * subroot, intptr_t ifirst, intptr_t npar, intptr_t * skipped) { Node * exterior = tree_store_get_node(store, subroot->iparent); ParIter iter; par_t * i = par_iter_init_range(&iter, store->psys, ifirst, npar); intptr_t SKIPPED = 0; Node * node = subroot; /* now scan over PAR, creating nodes */ while(i) { /* the current particle is no longer in current node, * time to close parent nodes */ while(node != exterior && ! tree_node_contains_fckey(node, &i->fckey)) { node->npar = par_iter_last_index(&iter) - node->ifirst + 1; /* here we shall try to merge the children, if * doing so is intended. not merging any for now */ node = tree_store_get_node(store, node->iparent); } if(node == exterior) { /* particle is not in any nodes, skip it */ SKIPPED ++; i = par_iter_next(&iter); continue; } /* ASSERTION: * the current particle i is * 1) in the current node, * 2) not in any of the current children of node * [guarrenteed by morton key sorting] * */ if(node->type == NODE_TYPE_INNER) { /* create a child of this inner node * particle will be added later */ node = create_leaf(store, node, i); node->ifirst = par_iter_last_index(&iter); node->npar = 0; goto add_particles; } /* must be a leaf node */ if(node->npar > store->splitthresh && node->order > 0) { /* rewind, split the node */ i = par_iter_set(&iter, node->ifirst); node = create_leaf(store, node, i); node->ifirst = par_iter_last_index(&iter); node->npar = 0; /* fall through add the particles */ } /* must be a leaf and no split is required, * code will fall through */ add_particles: /* add particle to the leaf */ /* TODO: figure a bigger step size: no need to scan * every particle */ node->npar += 1; i = par_iter_next(&iter); } /* close the parent nodes of the last scanned particle */ while(node != exterior) { node->npar = par_iter_last_index(&iter) - node->ifirst + 1; node = tree_store_get_node(store, node->iparent); } if(SKIPPED && skipped) { *skipped = SKIPPED; } }