intmain(){ // algorithms: given an array, for each query, reach O(1) time complexity int arr_length; int random_input = 0; std::cin >> arr_length; std::memset(arr, 0, sizeof(arr));
for(int i = 0; i < arr_length; i++){ random_input = generate_random(); arr[random_input] ++; }
// for the query part int query_num = 0; int query_value = 0; int not_found = 0; std::cin >> query_num; for(int i = 0; i < query_num; i++){ query_value = generate_random(); if(arr[query_value] >= 1){ // the value is found std::cout << query_value << " found." << std::endl; }else{ // std::cout << query_value << " not found" << std::endl; not_found ++; } } std::cout << "There are " << not_found << " elements that are not found." << std::endl;
return0; }
程序输出(随机):
1 2 3 4 5 6 7 8 9 10
200 10000 24185 found. 79030 found. 83479 found. 19472 found. 2067 found. 50925 found. 59530 found. There are 9993 elements that are not found.
/* * @Author: Xiyuan Yang xiyuan_yang@outlook.com * @Date: 2025-04-17 14:22:30 * @LastEditors: Xiyuan Yang xiyuan_yang@outlook.com * @LastEditTime: 2025-04-17 20:51:35 * @FilePath: /Data_structure/Class_implementation/close_Hash_Table.cpp * @Description: * Do you code and make progress today? * Copyright (c) 2025 by Xiyuan Yang, All Rights Reserved. */
template<classKEY, classOTHER> structset { KEY key; OTHER other; };
// Dynamic Search Table template<classKey, classOther> classdynamicSearchTable { public: // finding elements of x virtual set<Key, Other> *find(const Key &x)const= 0;
// insert element, maintaining the order virtualvoidinsert(const set<Key, Other> &x)= 0;
// remove an element, maintaining the order virtualvoidremove(const Key &x)= 0;
virtual ~dynamicSearchTable() {} };
template<classKey, classOther> classclose_Hash_Table : public dynamicSearchTable<Key, Other> { private: structNode { set<Key, Other> data; int status; // status: 0 for empty, 1 for active and 2 for deleted Node() { status = 0; } };
Node *array; int size;
// function pointer (hash function) int (*key)(const Key &x);
// if the key value is int iteself, we no need for another function staticintdefaultKey(constint &x){ return x; }
voidinsert(const set<Key, Other> &x){ int init_pos, pos;
// get the position after the mapping init_pos = pos = key(x.key) % size; do { if (array[pos].status != 1) { // then we can insert the value! array[pos].data = x; array[pos].status = 1; return; } else { if (array[pos].data.key == x.key) { // we have insert the same key value! // return directly return; } } pos = (pos + 1) % size; } while (pos != init_pos); // if there is no free space, then it will jump out of the while loop // you can throw somwething here throw std::runtime_error("The table has been occupied!"); }
voidremove(const Key &x){ int init_pos, pos = 0; init_pos = pos = key(x) % size; do { if (array[pos].status == 0) { // no element will be deleted return; } if (array[pos].status == 1 && array[pos].data.key == x) { // find the specific element array[pos].status = 2; return; } pos = (pos + 1) % size; } while (pos != init_pos); } }; intgenerate_random(int range = 100000){ std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution<> dis(1, range); returndis(gen); }
for (int i = 0; i < alldata_num; i++) { set<int, std::string> element; element.key = generate_random(alldata_range); element.other = generate_random_string(); s.insert(element); storage.push(element.key); }
// for the searching period for (int i = 0; i < query_num; i++) { int search_key = generate_random(alldata_range); if (s.find(search_key) != nullptr) { std::cout << "Find the key value " << search_key << std::endl; }
if (generate_random(1000) % 3 == 0 && !storage.empty()) { // randomly remove some elements int dele = storage.front(); s.remove(dele); if (s.find(dele) == nullptr) { std::cout << "The key value " << dele << " has been deleted successfully." << std::endl; } storage.pop(); } }
// initialize the array for (int i = 0; i < size; i++) { array[i] = nullptr; } }
~open_Hash_Table() { // delete all the array Node *p, *q; for (int i = 0; i < size; i++) { p = array[i]; // delete the linked list of p while (p != nullptr) { q = p->next; delete p; p = p->next; } } }
set<Key, Other> *find(const Key &x)const{ int pos; Node *p; pos = key(x) % size; p = array[pos]; while (p != nullptr && !(p->data.key == x)) { p = p->next; }
if (p == nullptr) { returnnullptr; // we don't find the element. } else { return (set<Key, Other> *) p; } }
voidinsert(const set<Key, Other> &x){ int pos; Node *p;
if (array[pos] == nullptr) { // no elements return; } else { p = array[pos]; // search for the linked list of p
// for the single element if (p->data.key == x) { array[pos] = p->next; delete p; return; } else { while (p->next != nullptr && !(p->next->data.key == x)) { p = p->next; }
if (p->next != nullptr) { // we find the element q = p->next; p->next = q->next; delete q; } } } } };
for (int i = 0; i < alldata_num; i++) { set<int, std::string> element; element.key = generate_random(alldata_range); element.other = generate_random_string(); s.insert(element); storage.push(element.key); }
// for the searching period for (int i = 0; i < query_num; i++) { int search_key = generate_random(alldata_range); if (s.find(search_key) != nullptr) { std::cout << "Find the key value " << search_key << std::endl; }
if (generate_random(1000) % 3 == 0 && !storage.empty()) { // randomly remove some elements int dele = storage.front(); s.remove(dele); if (s.find(dele) == nullptr) { std::cout << "The key value " << dele << " has been deleted successfully." << std::endl; } storage.pop(); } }
std::string random_string; for (size_t i = 0; i < length; ++i) { random_string += characters[dis(gen)]; } return random_string; }
voidtest_set(int size = 1000){ std::set<int> s; std::set<int>::iterator p; int element, target; longlong count = 0; std::cout << "Test for STL: set BEGIN \n\n\n";
// insert elements in the set for (int i = 0; i < size; i++) { element = generate_random(); s.insert(element); std::cout << element << " is inserted in the set." << std::endl; }
// find elements in the set for (int i = 0; i < size * 10; i++) { target = generate_random(); if (s.find(target) != s.end()) { // find the element std::cout << target << " is found in the set." << std::endl; count++; } else { std::cout << target << " is not found in the set." << std::endl; } } std::cout << count << " elements is found in the set." << std::endl;
// delete elements in the set for (auto iter = s.begin(); iter != s.end();) { std::cout << *iter << " is in the set." << std::endl; iter = s.erase(iter); if (s.empty()) { std::cout << "The set is empty now!" << std::endl; } }
std::cout << "Test for STL: set END \n\n\n"; }
voidtest_map(int size = 1000){ std::cout << "Test for STL: map BEGIN \n\n\n"; std::map<int, std::string> s; int element; std::string element_string; longlong count = 0;
// insert element in s for (int i = 0; i < size; i++) { element = generate_random(); element_string = generate_random_string(); s.insert(std::pair<int, std::string>{element, element_string}); std::cout << element_string << " with the value " << element << " is inserted in the map." << std::endl; }
// find elements in s for (int i = 0; i < 10 * size; i++) { element = generate_random(); auto iter = s.find(element); if (iter != s.end()) { // find the element! std::string target_string = iter->second; std::cout << target_string << " is found with the key value of " << element << std::endl; count++; } else { std::cout << "Key value " << element << " is not found in this map." << std::endl; } } std::cout << count << " elements are found in the map." << std::endl;
// delete element in the map for (auto iter = s.begin(); iter != s.end();) { std::cout << iter->second << " with the string value " << iter->first << " is found in the map." << std::endl; iter = s.erase(iter); if (s.empty()) { std::cout << "The map is empty now!" << std::endl; } }
std::cout << "Test for STL: map END \n\n\n"; }
voidtest_unordered_set(int size = 1000){ std::unordered_set<int> s; int element, target; longlong count = 0; std::cout << "Test for STL: unordered_set BEGIN \n\n\n";
// Insert elements into the unordered_set for (int i = 0; i < size; i++) { element = generate_random(); s.insert(element); std::cout << element << " is inserted in the unordered_set." << std::endl; }
// Find elements in the unordered_set for (int i = 0; i < size * 10; i++) { target = generate_random(); if (s.find(target) != s.end()) { // Element found std::cout << target << " is found in the unordered_set." << std::endl; count++; } else { std::cout << target << " is not found in the unordered_set." << std::endl; } } std::cout << count << " elements are found in the unordered_set." << std::endl;
// Delete elements from the unordered_set for (auto iter = s.begin(); iter != s.end();) { std::cout << *iter << " is in the unordered_set." << std::endl; iter = s.erase(iter); if (s.empty()) { std::cout << "The unordered_set is empty now!" << std::endl; } }
std::cout << "Test for STL: unordered_set END \n\n\n"; }
voidtest_unordered_map(int size = 1000){ std::unordered_map<int, std::string> s; int element; std::string element_string; longlong count = 0; std::cout << "Test for STL: unordered_map BEGIN \n\n\n";
// Insert elements into the unordered_map for (int i = 0; i < size; i++) { element = generate_random(); element_string = generate_random_string(); s.insert({element, element_string}); std::cout << element_string << " with the value " << element << " is inserted in the unordered_map." << std::endl; }
// Find elements in the unordered_map for (int i = 0; i < 10 * size; i++) { element = generate_random(); auto iter = s.find(element); if (iter != s.end()) { // Element found std::string target_string = iter->second; std::cout << target_string << " is found with the key value of " << element << std::endl; count++; } else { std::cout << "Key value " << element << " is not found in this unordered_map." << std::endl; } } std::cout << count << " elements are found in the unordered_map." << std::endl;
// Delete elements from the unordered_map for (auto iter = s.begin(); iter != s.end();) { std::cout << iter->second << " with the string value " << iter->first << " is found in the unordered_map." << std::endl; iter = s.erase(iter); if (s.empty()) { std::cout << "The unordered_map is empty now!" << std::endl; } }
std::cout << "Test for STL: unordered_map END \n\n\n"; }
intmain(){ // test for stl dynamic search test_set(); test_map(); test_unordered_set(); test_unordered_map(); return0; }