/* * NAME: vol->catsearch() * DESCRIPTION: search catalog tree */ int v_catsearch(hfsvol *vol, long parid, const char *name, CatDataRec *data, char *cname, node *np) { CatKeyRec key; byte pkey[HFS_CATKEYLEN]; const byte *ptr; node n; int found; if (np == 0) np = &n; r_makecatkey(&key, parid, name); r_packcatkey(&key, pkey, 0); found = bt_search(&vol->cat, pkey, np); if (found <= 0) return found; ptr = HFS_NODEREC(*np, np->rnum); if (cname) { r_unpackcatkey(ptr, &key); strcpy(cname, key.ckrCName); } if (data) r_unpackcatdata(HFS_RECDATA(ptr), data); return 1; }
int main (int argc, char *argv[]) { struct node *tree = NULL; struct node *lookup; struct in_addr ip; int idx; int random; char text[64]; printf ("Binary tree test\n"); for (idx = 0; idx < 10; idx++) { sprintf (text, "chiave %d", rand() % 16); bt_insert (&tree, text, &ip, idx); } printf ("Printing\n"); print_tree (tree); for (idx = 0; idx < 16; idx++) { sprintf (text, "chiave %d", idx); lookup = bt_search (tree, text); if (lookup == NULL) printf ("%s not found\n", text); else printf ("%s has been found with idx %d\n", lookup->key, lookup->expires); } bt_delete_tree (tree); printf ("Done\n"); }
/* * NAME: hfs->opendir() * DESCRIPTION: prepare to read the contents of a directory */ hfsdir *hfs_opendir(hfsvol *vol, const char *path) { hfsdir *dir = 0; CatKeyRec key; CatDataRec data; byte pkey[HFS_CATKEYLEN]; if (getvol(&vol) == -1) goto fail; dir = ALLOC(hfsdir, 1); if (dir == 0) ERROR(ENOMEM, 0); dir->vol = vol; if (*path == 0) { #ifdef CP_NO_STATIC /* meta-directory containing root dirs from all mounted volumes */ dir->dirid = 0; dir->vptr = hfs_mounts; #else assert(0); #endif } else { if (v_resolve(&vol, path, &data, 0, 0, 0) <= 0) goto fail; if (data.cdrType != cdrDirRec) ERROR(ENOTDIR, 0); dir->dirid = data.u.dir.dirDirID; dir->vptr = 0; r_makecatkey(&key, dir->dirid, ""); r_packcatkey(&key, pkey, 0); if (bt_search(&vol->cat, pkey, &dir->n) <= 0) goto fail; } dir->prev = 0; dir->next = vol->dirs; if (vol->dirs) vol->dirs->prev = dir; vol->dirs = dir; return dir; fail: FREE(dir); return 0; }
//Search in the binary tree node* bt_search(char *query, node *root){ if (root == NULL){ printf("\nStudent Record not found\n\n"); return NULL; } if (strcmp(root->data->identity, query) == 0){ return root; } else{ if ( strcmp(query, root->data->identity) == -1 ){ bt_search(query, root->left); } else{ bt_search(query, root->right); } } }
BTree* bt_search(BTree* a, int x, int* pos) { int found = findpos(a,x,pos); if (found) return a; else if (isleaf(a)) return NULL; else return bt_search(a->p[*pos], x, pos); }
//Element deletion void delete_element(char *query, node *root){ if (root == NULL){ printf("Empty Tree\n"); return; } node* del = bt_search(query, root); node* tmp; if (del == NULL) return; //The node has two children if (del->left!=NULL && del->right!=NULL){ tmp = findMax(del->left); overwrite(del, tmp); //Checking if the current node is the left or right parent's child if (tmp->parent->left == tmp) tmp->parent->left=NULL; else tmp->parent->right = NULL; free(tmp->data); free(tmp); } //The node has not any childFILE* fo = fopen( "treedata.txt", "a+" ); else if (del->left==NULL && del->right==NULL){ //Checking if the current node is the left or right parent's child if (del->parent->left == del) del->parent->left=NULL; else del->parent->right = NULL; free(del->data); free(del); } //The node has one child else{ if (del->left == NULL){ tmp = del->right; del->right = NULL; } else if (del->right == NULL){ tmp = del->left; del->left = NULL; } overwrite(del, tmp); del->left = tmp->left; del->right = tmp->right; free(tmp->data); free(tmp); } }
/* * NAME: vol->extsearch() * DESCRIPTION: search extents tree */ int v_extsearch(hfsfile *file, unsigned int fabn, ExtDataRec *data, node *np) { ExtKeyRec key; ExtDataRec extsave; unsigned int fabnsave; byte pkey[HFS_EXTKEYLEN]; const byte *ptr; node n; int found; if (np == 0) np = &n; r_makeextkey(&key, file->fork, file->cat.u.fil.filFlNum, fabn); r_packextkey(&key, pkey, 0); /* in case bt_search() clobbers these */ memcpy(&extsave, &file->ext, sizeof(ExtDataRec)); fabnsave = file->fabn; found = bt_search(&file->vol->ext, pkey, np); memcpy(&file->ext, &extsave, sizeof(ExtDataRec)); file->fabn = fabnsave; if (found <= 0) return found; if (data) { ptr = HFS_NODEREC(*np, np->rnum); r_unpackextdata(HFS_RECDATA(ptr), data); } return 1; }
int identity(){ binary_tree *tree = init_bt(); read_db_to_tree(tree); node* tmp; char buffer[50]; float fbuff; int cmd; FILE *f; while(1){ printf("########################\n"); printf("# 1. Print In-Order #\n"); printf("# 2. Search #\n"); printf("# 3. Modify #\n"); printf("# 4. Delete #\n"); printf("# 5. Exit #\n"); printf("########################\n"); printf("Enter your choice: "); scanf("%d",&cmd); switch (cmd){ case 1: inorder(tree->root); break; case 2: printf("Enter IDENTITY NUMBER: "); scanf("%s",&buffer); tmp = bt_search(buffer, tree->root); if (tmp != NULL) { printf("\nResult: "); print_node(tmp); printf("\n"); } break; case 3: printf("Enter IDENTITY: "); scanf("%s", &buffer); tmp = bt_search(buffer, tree->root); if (tmp != NULL){ printf("Enter Name: "); scanf("%s",&buffer); strcpy(tmp->data->name, buffer); printf("Enter Sirname: "); scanf("%s",&buffer); strcpy(tmp->data->sirname, buffer); printf("Enter Average Grade: "); scanf("%f",&fbuff); tmp->data->avg_grade = fbuff; printf("\n\n"); print_node(tmp); } fflush(stdin); break; case 4: printf("Enter IDENTITY: "); scanf("%s",&buffer); delete_element(buffer, tree->root); break; case 5: // Clear the previous database file f = fopen("student_database.txt", "w"); fclose(f); print_preorder(tree->root); return 0; default: printf("Bad Command\n"); break; } } }
/* * NAME: hfs->opendir() * DESCRIPTION: prepare to read the contents of a directory */ hfsdir *hfs_opendir(hfsvol *vol, char *path) { hfsdir *dir; CatKeyRec key; CatDataRec data; unsigned char pkey[HFS_CATKEYLEN]; if (v_getvol(&vol) < 0) return 0; dir = ALLOC(hfsdir, 1); if (dir == 0) { ERROR(ENOMEM, 0); return 0; } dir->vol = vol; if (*path == 0) { /* meta-directory containing root dirs from all mounted volumes */ dir->dirid = 0; dir->vptr = hfs_mounts; } else { if (v_resolve(&vol, path, &data, 0, 0, 0) <= 0) { FREE(dir); return 0; } if (data.cdrType != cdrDirRec) { FREE(dir); ERROR(ENOTDIR, 0); return 0; } dir->dirid = data.u.dir.dirDirID; dir->vptr = 0; r_makecatkey(&key, dir->dirid, (char *)""); r_packcatkey(&key, pkey, 0); if (bt_search(&vol->cat, pkey, &dir->n) <= 0) { FREE(dir); return 0; } } dir->prev = 0; dir->next = vol->dirs; if (vol->dirs) vol->dirs->prev = dir; vol->dirs = dir; return dir; }