/******************************************************************************* Função recursiva que pesquisa o elemento pretendido na arvore. *******************************************************************************/ static PtAVLNode SearchRec (int elemento, PtAVLNode no) { if (no == NULL) return NULL; if (no->Elem > elemento) return SearchRec (elemento, no->PtLeft); else if (no->Elem < elemento) return SearchRec (elemento, no->PtRight); else return no; }
/******************************************************************************* Função recursiva que pesquisa o elemento pretendido na arvore. Recursive function that searches the given element in a tree. *******************************************************************************/ static PtABPNode SearchRec (PtABPNode proot, int pelem) /* pesquisa recursiva - recursive search */ { if (proot == NULL) return NULL; /* pesquisa sem sucesso - search without success */ if (proot->Elem == pelem) return proot; /* pesquisa com sucesso - search with success */ else if (proot->Elem > pelem) return SearchRec (proot->PtLeft, pelem); else return SearchRec (proot->PtRight, pelem); }
int AVLSearch (PtAVLNode proot, int elemento) { if (proot == NULL) { Error = AVL_EMPTY; return 0; } if (SearchRec (elemento, proot) == NULL) { Error = NO_ELEM; return 0; } else { Error = OK; return 1; } }
int ABPSearch (PtABPNode proot, int pelem) { if (proot == NULL) { Error = ABP_EMPTY; return 0; } if (SearchRec (proot, pelem) == NULL) { Error = NO_ELEM; return 0; } else { Error = OK; return 1; } }
/** * Recursively accumulate the k_closest points to query_point_ into results_. * @param Level level in tree of sub-tree to be searched * @param SubTree sub-tree to be searched */ void KDTreeSearch::SearchRec(int level, KDNODE *sub_tree) { if (level >= tree_->KeySize) level = 0; if (!BoxIntersectsSearch(sb_min_, sb_max_)) return; results_->insert(DistanceSquared(tree_->KeySize, tree_->KeyDesc, query_point_, sub_tree->Key), sub_tree->Data); if (query_point_[level] < sub_tree->BranchPoint) { if (sub_tree->Left != NULL) { FLOAT32 tmp = sb_max_[level]; sb_max_[level] = sub_tree->LeftBranch; SearchRec(NextLevel(tree_, level), sub_tree->Left); sb_max_[level] = tmp; } if (sub_tree->Right != NULL) { FLOAT32 tmp = sb_min_[level]; sb_min_[level] = sub_tree->RightBranch; SearchRec(NextLevel(tree_, level), sub_tree->Right); sb_min_[level] = tmp; } } else { if (sub_tree->Right != NULL) { FLOAT32 tmp = sb_min_[level]; sb_min_[level] = sub_tree->RightBranch; SearchRec(NextLevel(tree_, level), sub_tree->Right); sb_min_[level] = tmp; } if (sub_tree->Left != NULL) { FLOAT32 tmp = sb_max_[level]; sb_max_[level] = sub_tree->LeftBranch; SearchRec(NextLevel(tree_, level), sub_tree->Left); sb_max_[level] = tmp; } } }
/// Locate the k_closest points to query_point_, and return their distances and /// data into the given buffers. void KDTreeSearch::Search(int *result_count, FLOAT32 *distances, void **results) { if (tree_->Root.Left == NULL) { *result_count = 0; } else { for (int i = 0; i < tree_->KeySize; i++) { sb_min_[i] = tree_->KeyDesc[i].Min; sb_max_[i] = tree_->KeyDesc[i].Max; } SearchRec(0, tree_->Root.Left); int count = results_->elements_count(); *result_count = count; for (int j = 0; j < count; j++) { distances[j] = (FLOAT32) sqrt((FLOAT64)results_->elements()[j].key); results[j] = results_->elements()[j].value; } } }
int main() { printf("******************************************\n"); printf("* Record Manager Application *\n"); printf("* Xinyi He *\n"); printf("******************************************\n"); char c; do { printf("\n"); printf("a. Open database\n"); printf("b. Get first record\n"); printf("c. Get next record \n"); printf("d. Get previous record\n"); printf("e. Get Nth record\n"); printf("f. Insert record\n"); printf("g. Bulk insert records in file\n"); printf("h. Delete record\n"); printf("i. Update record\n"); printf("j. Find record with first attribute value\n"); printf("k. Show catalog file\n"); printf("l. Get first page\n"); printf("m. Get next page\n"); printf("n. Show buf stats\n"); printf("o. Commit changes\n"); printf("p. Exit\n"); printf("Please press letter to choose an action.\n"); c = getchar(); fflush(stdin); char filename[100]; switch (c) { //a. Open database case 'a': { printf("Please input the database name (don't include extension): "); gets(filename); OpenStore(filename); break; } //b. Get first record case 'b': { Output(FirstRec()); break; } //c. Get next record case 'c': { Output(NextRec()); break; } //d. Get previous record case 'd': { Output(PriorRec()); break; } //e. Get Nth record case 'e': { printf("\nInput N: "); int n; scanf("%d", &n); Output(NRec(n)); fflush(stdin); break; } //f. Insert record case 'f': { printf("\nInput a comma-delimited Record:\n"); char statement[100]; scanf("%s", &statement); fflush(stdin); InsertRec(statement); break; } //g. Bulk insert records in file case 'g': { printf("\nInput the filename(include extension): "); char file[100]; scanf("%s", &file); InsertRecFromFile(file); fflush(stdin); break; } //h. Delete record case 'h': { DeleteRec(); break; } //i. Update record case 'i': { printf("\nInput a comma-delimited Record:\n"); char statement[100]; scanf("%s", &statement); fflush(stdin); UpdateRec(statement); break; } //j. Find record with first attribute value case 'j': { printf("\nInput value:\n"); char statement[100]; scanf("%s", &statement); fflush(stdin); SearchRec(statement); break; } //k. Show catalog file case 'k': { DisplyCatFile(filename); break; } //l. Get first page case 'l': { GetPage(0); break; } //m. Get next page case 'm': { GetPage(1); break; } //n. Show buf stats case 'n': { ShowBuf(); break; } //o. Commit changes case 'o': { Commit(filename); break; } //p. Exit case 'p': { CloseStore(filename); } }; }while(c != 'p'); return 0; }