//-------------------------------------------------------------------------- // BSTNode* searchBST(BSTNode* BSTRoot, const string keyword); // Searches the BSTNode BSTRoot to determine if a keyword exists. If it // exists, then return a pointer to that node. However, if it doesn't exist, // return a pointer to the parent. Thus, must check while exiting which // node is being returned. // Preconditions: BST exists // Postconditions: BST is unchanged // Return value: BSTNode // Functions called: Recursively calls itself BST::BSTNode* BST::searchBST( BSTNode* BSTroot, string keyword) { // base case if (isEmpty()) { return BSTroot; } //base case 2: if equal, will also return. else if (keyword == BSTroot->keyword){ return BSTroot; } //recursive left calls else if (keyword < BSTroot->keyword) { if (BSTroot->left == nullptr) { return BSTroot; } return searchBST(BSTroot->left, keyword); } //recursive right calls else { if (BSTroot->right == nullptr) { return BSTroot; } return searchBST(BSTroot->right, keyword); } return nullptr; }
BST searchBST(keytype k,BST F) { BST p=F; if(p==NULL||k==p->data.key) return p; if(k<p->data.key) return (searchBST(k,p->lchild)); else return (searchBST(k,p->rchild)); }
int searchBST(int x, tree_node *s, tree_node *f, SplayTree p){ if(s == NULL){ p = f; return 0; } if(x == s->item){ p = s; return 1; } else if(x<s->item) return searchBST(x, s->left, s, p); else return searchBST(x, s->right, s, p); }
int testInsertBST(BST *tree, int isVerbose) { printf("TESTCASE: INSERTION\n"); // Print the tree if (isVerbose == TRUE) { printf("PRE-INSERTION - TREE OVERVIEW\n"); inOrderPrintBST(tree); } // Create test word WORD *insertionTestWord = newWord(TEST_WORD, newWordLocation(1, 1, 1)); if (isVerbose == TRUE) { printf("INSERTION - TEST WORD\n"); printWord(insertionTestWord, 15, maxPageDigit, maxLineDigit); } // Run the insert function insertBST(tree, insertionTestWord); // Print the tree if (isVerbose == TRUE) { printf("POST-INSERTION - TREE OVERVIEW\n"); inOrderPrintBST(tree); } if (searchBST(tree, TEST_WORD) != NULL) { return SUCCESS; } return FAILED; }
//-------------------------------------------------------------------------- // bool searchBSTKey(const string keyword); // Searches the BST in-order to find the keyword. // Preconditions: BST exists // Postconditions: BST unchanged // Return value: True if keyword is found, false if otherwise // Functions called: searchBST() bool BST::searchBSTKey(const string keyword) { BSTNode* toCheck = searchBST(root, keyword); if (toCheck->keyword == keyword){ return true; } return false; }
//-------------------------------------------------------------------------- // bool addBSTNode(string toAdd, string pre, string post); // Adds BSTNode to the tree // Preconditions: BST exists // Postconditions: One BSTNode has been added // Return value: None -- will break if out of memory // Functions called: newBSTNode() void BST::addBSTNode(string toAdd, string pre, string post) { PrePost* newData = new PrePost; newData->preKey = pre; newData->postKey = post; if (isEmpty()) { BSTNode* newNode = newBSTNode(toAdd, newData); root = newNode; } else { BSTNode* toInsert = searchBST(root, toAdd); //check to see if node already there if (toAdd == toInsert->keyword) { toInsert->concord.addLast(newData); } //check if less than, left insert else if (toAdd < toInsert->keyword) { BSTNode* newNode = newBSTNode(toAdd, newData); toInsert->left = newNode; } //otherwise, right insert else { BSTNode* newNode = newBSTNode(toAdd, newData); toInsert->right = newNode; } } }
int main() { Tree* tree; printf("välkommen till skagettikåd\n\n"); printf("No.items, Sekvential, BinaryST, HashTable\n"); //loop for (int numberOfitems = 50000; numberOfitems <= 5000000; numberOfitems = numberOfitems * 10) { int* arr = (int*)calloc(numberOfitems, sizeof(int)); FILE* fp; fp = fopen("5 000 000 unika slumpade tal.txt", "r"); for (int i = 0; i < numberOfitems; i++) { fscanf(fp, "%d", &arr[i]); } fclose(fp); //stuff makeHashTable(arr, numberOfitems); tree = makeBST(arr, numberOfitems); //time variables double timeSekventiall = 0; double timeBST = 0; double timeHash = 0; //Genomför sköningen 100 gånger for (int i = 0; i < 100; i++) { int searchfor = 1; timeSekventiall += sekventiell(arr, searchfor, numberOfitems); timeBST += searchBST(tree, searchfor, numberOfitems); timeHash += searchHashTable(arr, searchfor, numberOfitems); } //dela på 100 för att få genomsnitt timeSekventiall = timeSekventiall / 100; timeBST = timeBST / 100; timeHash = timeHash / 100; //Print it! printf("%8d%10Lf%12Lf%10Lf\n", numberOfitems, timeSekventiall, timeBST, timeHash); //free free(arr); emptyTree(tree, tree->root); } system("pause"); return 0; }
int main() { printf("please in put the key\n"); BST F; F=new node; F=CreatBST(); Preorder(F); printf("which key do you want to search\n"); keytype k; scanf("%d",&k); BST temp; temp=new node; temp=searchBST(k,F); if(temp) { printf("serach successfully\n"); } printf("which key do you want to delete\n"); scanf("%d",&k); int shorter; DeleteAVL(F,k,shorter); Preorder(F); }
BiTree searchBST(BiTree T, KeyType key) { if ((!T) || EQ(key, T->data.key)) return T; else if LT(key, T->data.key) // 在左子树中继续查找 return searchBST(T->lchild, key); else return searchBST(T->rchild, key); // 在右子树中继续查找
/* Recirsive version. * NULL is returned if element with corresponding key is not found. */ tree searchBST ( tree t, element key ) { if (emptyTree (t)) { return EMPTYTREE; } else if (root (t) == key) { return t; } else if (lessThan( key, root( t ))) { return (searchBST (left (t), key)); } else { return (searchBST (right (t), key)); } }
int testSearchBST(BST *tree, char** validWords, char** invalidWords, int isVerbose) { printf("\nTESTCASE: SEARCH\n"); // Search for valid words printf("Search - Valid Words\n"); // Start - Long Way char *searchWord = validWords[3]; WORD *w = searchBST(tree, searchWord); printWord(w, FRAME_SIZE, maxPageDigit, maxLineDigit); // End - Long way printWord(searchBST(tree, "mnm"), FRAME_SIZE, maxPageDigit, maxLineDigit); // Using String Literal printWord(searchBST(tree, validWords[6]), FRAME_SIZE, maxPageDigit, maxLineDigit); // Using Short Array Access -> char* printWord(searchBST(tree, validWords[9]), FRAME_SIZE, maxPageDigit, maxLineDigit); printf("\nSearch - Invalid Words\n"); for (int i = 0; i < 5; i++) { // Print Word if invalid word is in the BST, else, print message that word is not in the BST if ( (w = searchBST(tree, invalidWords[i])) != NULL) printWord(w, FRAME_SIZE, maxPageDigit, maxLineDigit); else printf("Cannot find '%s' in the Search Tree!\n", invalidWords[i]); } return SUCCESS; }
tree_node *insertBST(int x, SplayTree tree){ tree_node *p, *t; if(!searchBST(x, tree, NULL, p)){ t = (SplayTree)malloc(sizeof(tree_node)); t->item = x; t->left = t->right = NULL; t->father = p; if(!p) tree = t; else if(x < p->item) p->left = t; else p->right = t; return t; } return NULL; }