ValueType& operator[](const KeyType& key) { Node* head = &buckets[hash(key)]; Node* n = findNodeByKey(head, key); if(n) return n->v; n = new Node(key); n->next = head->next; head->next = n; return n->v; }
Node *findNodeByKey(Node *from, char *key) { if (key == NULL) return NULL; if (!strcmp(key, from->key)) return from; unsigned i; for (i=0; i<from->numOfChildren; i++) { Node *res = findNodeByKey(from->children[i], key); if (res != NULL) return res; } return NULL; }
bool isKeyExist(const KeyType& key) { Node* head = &buckets[hash(key)]; return findNodeByKey(head, key); }
//--------------------------------------------------------------------------------------- // hlavni funkce se spoustou testu na overeni funkcnosti //--------------------------------------------------------------------------------------- int main(void) { Node *treeSet = InitTree(); simplePrintAll(treeSet, 0); // test 1 printf("Test 1.:"); Node *r = findNodeByKey(treeSet, "IB002 Algorithms and data structures I"); Node *algoritmy = findNodeByKey(treeSet, "algorithms"); Node *found = findMinimalRootOfNodes(r, algoritmy); if (found == r) { printf("OK \n"); } else { printf("Chyba: nalezeny uzel mel byt (%s), ale vas uzel byl (%s) \n", r->key, found->key); } // test 2 printf("Test 2.:"); found = findMinimalRootOfNodes(algoritmy, algoritmy); if(found == algoritmy) { printf("OK \n"); } else { printf("Chyba: nalezeny uzel mel byt (%s), ale vas uzel byl (%s) \n", algoritmy->key, found->key); } // test 3 printf("Test 3.:"); Node *heapSort = findNodeByKey(r, "heap sort"); Node *selectSort = findNodeByKey(r, "select sort"); found = findMinimalRootOfNodes(heapSort, selectSort); if (found == findNodeByKey(r, "searching algorithms")) { printf("OK \n"); } else { printf("Chyba, nalezeny uzel mel byt (%s), ale vas uzel byl (%s) \n", findNodeByKey(r, "searching algorithms")->key, found->key); } // test 4 printf("Test 4.:"); Node *graphs = findNodeByKey(r, "graphs"); Node *directedGraph = findNodeByKey(r, "directed graph"); found = findMinimalRootOfNodes(graphs, directedGraph); if (found == graphs) { printf("OK \n"); } else { printf("Chyba, nalezeny uzel mel byt (%s), ale vas uzel byl (%s) \n", graphs->key, found->key); } // test 5 printf("Test 5.:"); Node *BPlusTree = findNodeByKey(r, "B+ tree"); Node *power = findNodeByKey(r, "power"); found = findMinimalRootOfNodes(BPlusTree, power); if (found == r) { printf("OK \n"); } else { printf("Chyba, nalezeny uzel mel byt (%s), ale vas uzel byl (%s) \n", r->key, found->key); } // test 6 printf("Test 6.:"); Node *dataStructures = findNodeByKey(r, "data structures"); Node *mergeSort = findNodeByKey(r, "merge sort"); found = findMinimalRootOfNodes(dataStructures, mergeSort); if (found == r) { printf("OK \n"); } else { printf("Chyba, nalezeny uzel mel byt (%s), ale vas uzel byl (%s) \n", r->key, found->key); } DestroyTree(treeSet); return 0; }