Advertisement
Kwwiker

Кодик

Nov 7th, 2021
1,103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <fstream>
  4. #include <cstring>
  5. #include <list>
  6.  
  7. using namespace std;
  8.  
  9. struct BTNode {
  10.     long long key = 0;
  11.     int offset = 0;
  12.     BTNode* leftChild = nullptr;
  13.     BTNode* rightChild = nullptr;
  14. };
  15.  
  16. class BSTree {
  17. private:
  18.     BTNode* root = nullptr;
  19.     int countOfAdd = 0;
  20. public:
  21.     BSTree() = default;
  22.  
  23.     void print(BTNode* node, int level) {
  24.         if (node) {
  25.             print(node->rightChild, level + 1);
  26.             for (int i = 0; i < level; i++) {
  27.                 cout << "\t";
  28.             }
  29.             cout << node->key << endl;
  30.             print(node->leftChild, level + 1);
  31.         }
  32.     }
  33.  
  34.     BTNode* insert(BTNode* node, long long key, int offset) {
  35.         if (!node) {
  36.             node = new BTNode;
  37.             node->key = key;
  38.             node->offset = offset;
  39.         }
  40.         if (key < node->key) {
  41.             node->leftChild = insert(node->leftChild, key, offset);
  42.         }
  43.         else if (key > node->key) {
  44.             node->rightChild = insert(node->rightChild, key, offset);
  45.         }
  46.         return node;
  47.     }
  48.  
  49.     BTNode* find(BTNode* node, long long key, int* compares) {
  50.         (*compares)++;
  51.         if (!node) {
  52.             return 0;
  53.         }
  54.         if (node->key == key) {
  55.             return node;
  56.         }
  57.         if (key < node->key) {
  58.             return find(node->leftChild, key, compares);
  59.         }
  60.         else {
  61.             return find(node->rightChild, key, compares);
  62.         }
  63.     }
  64.  
  65.     BTNode* getNodeWithMinValue(BTNode* node) {
  66.         BTNode* current = node;
  67.         while (current && current->leftChild) {
  68.             current = current->leftChild;
  69.         }
  70.         return current;
  71.     }
  72.  
  73.     BTNode* remove(BTNode* node, long long key) {
  74.         if (!node) {
  75.             return node;
  76.         }
  77.         if (key < node->key) {
  78.             node->leftChild = remove(node->leftChild, key);
  79.         }
  80.         else if (key > node->key) {
  81.             node->rightChild = remove(node->rightChild, key);
  82.         }
  83.         else {
  84.             if (!node->leftChild && !node->rightChild) {
  85.                 return nullptr;
  86.             }
  87.             else if (!node->leftChild) {
  88.                 BTNode* ret = node->rightChild;
  89.                 delete node;
  90.                 return ret;
  91.             }
  92.             else if (!node->rightChild) {
  93.                 BTNode* ret = node->leftChild;
  94.                 delete node;
  95.                 return ret;
  96.             }
  97.  
  98.             BTNode* tmp = getNodeWithMinValue(node->rightChild);
  99.             node->key = tmp->key;
  100.             node->offset = tmp->offset;
  101.             node->rightChild = remove(node->rightChild, tmp->key);
  102.         }
  103.         return node;
  104.     }
  105.  
  106.     void insert(long long key, int offset) {
  107.         root = insert(root, key, offset);
  108.         countOfAdd++;
  109.     }
  110.  
  111.     BTNode* find(long long key, int* compares) {
  112.         return find(root, key, compares);
  113.     }
  114.  
  115.     void remove(long long key) {
  116.         root = remove(root, key);
  117.     }
  118.  
  119.     void print() {
  120.         print(root, 0);
  121.     }
  122.  
  123.     int getCountOfAdd() {
  124.         return countOfAdd;
  125.     }
  126.  
  127.     long long getMemory() {
  128.         return (sizeof(BTNode) * countOfAdd) / 1024;
  129.     }
  130.  
  131.     void removeAll(BTNode* current) {
  132.         if (!current) {
  133.             return;
  134.         }
  135.         if (current->leftChild) {
  136.             removeAll(current->leftChild);
  137.         }
  138.         if (current->rightChild) {
  139.             removeAll(current->rightChild);
  140.         }
  141.         delete current;
  142.     }
  143.  
  144.     ~BSTree() {
  145.         removeAll(root);
  146.     }
  147. };
  148.  
  149. struct RTNode {
  150.     long long key = 0;
  151.     int offset = 0;
  152.     int size = 1;
  153.     RTNode* leftChild = nullptr;
  154.     RTNode* rightChild = nullptr;
  155.     int getSize(RTNode* node) {
  156.         if (!node) {
  157.             return 0;
  158.         }
  159.         return node->size;
  160.     }
  161.     void setSize(RTNode* node) {
  162.         node->size = getSize(node->leftChild) + getSize(node->rightChild) + 1;
  163.     }
  164. };
  165.  
  166. class RTree {
  167. private:
  168.     RTNode* root = nullptr;
  169.     int countOfAdd = 0;
  170.     int countOfRotate = 0;
  171. public:
  172.     RTree() = default;
  173.  
  174.     void print(RTNode* node, int level) {
  175.         if (node) {
  176.             print(node->rightChild, level + 1);
  177.             for (int i = 0; i < level; i++) {
  178.                 cout << "\t";
  179.             }
  180.             cout << node->key << endl;
  181.             print(node->leftChild, level + 1);
  182.         }
  183.     }
  184.  
  185.     RTNode* rotateRight(RTNode* node) {
  186.         countOfRotate++;
  187.         RTNode* temp = node->leftChild;
  188.         if (!temp) {
  189.             return node;
  190.         }
  191.         node->leftChild = temp->rightChild;
  192.         temp->rightChild = node;
  193.         temp->size = node->size;
  194.         node->setSize(node);
  195.         return temp;
  196.     }
  197.  
  198.     RTNode* rotateLeft(RTNode* node) {
  199.         countOfRotate++;
  200.         RTNode* temp = node->rightChild;
  201.         if (!temp) {
  202.             return node;
  203.         }
  204.         node->rightChild = temp->leftChild;
  205.         temp->leftChild = node;
  206.         temp->size = node->size;
  207.         node->setSize(node);
  208.         return temp;
  209.     }
  210.  
  211.     RTNode* insertRoot(RTNode* node, long long key, int offset) {
  212.         if (!node) {
  213.             node = new RTNode;
  214.             node->key = key;
  215.             node->offset = offset;
  216.         }
  217.         if (key < node->key) {
  218.             node->leftChild = insertRoot(node->leftChild, key, offset);
  219.             return rotateRight(node);
  220.         }
  221.         else if (key > node->key) {
  222.             node->rightChild = insertRoot(node->rightChild, key, offset);
  223.             return rotateLeft(node);
  224.         }
  225.         return node;
  226.     }
  227.  
  228.     RTNode* insert(RTNode* node, long long key, int offset) {
  229.         if (!node) {
  230.             node = new RTNode;
  231.             node->key = key;
  232.             node->offset = offset;
  233.         }
  234.         if (rand() % (node->size + 1) == 0) {
  235.             return insertRoot(node, key, offset);
  236.         }
  237.         if (key < node->key) {
  238.             node->leftChild = insert(node->leftChild, key, offset);
  239.         }
  240.         else if (key > node->key) {
  241.             node->rightChild = insert(node->rightChild, key, offset);
  242.         }
  243.         node->setSize(node);
  244.         return node;
  245.     }
  246.  
  247.     RTNode* find(RTNode* node, long long key, int* compares) {
  248.         (*compares)++;
  249.         if (!node) {
  250.             return 0;
  251.         }
  252.         if (node->key == key) {
  253.             return node;
  254.         }
  255.         if (key < node->key) {
  256.             return find(node->leftChild, key, compares);
  257.         }
  258.         else {
  259.             return find(node->rightChild, key, compares);
  260.         }
  261.     }
  262.  
  263.     RTNode* join(RTNode* node, RTNode* temp)
  264.     {
  265.         if (!node) {
  266.             return temp;
  267.         }
  268.         if (!temp) {
  269.             return node;
  270.         }
  271.         if (rand() % (node->size + temp->size) < node->size)
  272.         {
  273.             node->rightChild = join(node->rightChild, temp);
  274.             node->setSize(node);
  275.             return node;
  276.         }
  277.         else {
  278.             temp->leftChild = join(node, temp->leftChild);
  279.             temp->setSize(temp);
  280.             return temp;
  281.         }
  282.     }
  283.  
  284.     RTNode* remove(RTNode* node, int key) {
  285.         if (!node) {
  286.             return node;
  287.         }
  288.         if (node->key == key)
  289.         {
  290.             RTNode* temp = join(node->leftChild, node->rightChild);
  291.             delete node;
  292.             return temp;
  293.         }
  294.         else if (key < node->key) {
  295.             node->leftChild = remove(node->leftChild, key);
  296.         }
  297.         else if (key > node->key) {
  298.             node->rightChild = remove(node->rightChild, key);
  299.         }
  300.         return node;
  301.     }
  302.  
  303.     void insert(long long key, int offset) {
  304.         root = insert(root, key, offset);
  305.         countOfAdd++;
  306.     }
  307.  
  308.     RTNode* find(long long key, int* compares) {
  309.         return find(root, key, compares);
  310.     }
  311.  
  312.     void remove(long long key) {
  313.         root = remove(root, key);
  314.     }
  315.  
  316.     void print() {
  317.         print(root, 0);
  318.     }
  319.  
  320.     int getCountOfAdd() {
  321.         return countOfAdd;
  322.     }
  323.  
  324.     int getAverageCountOfRotate() {
  325.         return countOfRotate / countOfAdd;
  326.     }
  327.  
  328.     long long getMemory() {
  329.         return (sizeof(RTNode) * countOfAdd) / 1024;
  330.     }
  331.  
  332.     void removeAll(RTNode* current) {
  333.         if (!current) {
  334.             return;
  335.         }
  336.         if (current->leftChild) {
  337.             removeAll(current->leftChild);
  338.         }
  339.         if (current->rightChild) {
  340.             removeAll(current->rightChild);
  341.         }
  342.         delete current;
  343.     }
  344.  
  345.     ~RTree() {
  346.         removeAll(root);
  347.     }
  348. };
  349.  
  350. struct HashNode {
  351.     long long key = 0;
  352.     int offset = 0;
  353. };
  354.  
  355. class HashTable
  356. {
  357. public:
  358.     int size;
  359.     int countOfNotes = 0;
  360.     int countOfAdd = 0;
  361.     list<HashNode>* table;
  362.  
  363.     HashTable(int size)
  364.     {
  365.         this->size = size;
  366.         table = new list<HashNode>[size];
  367.     }
  368.  
  369.     int getCountOfNotes() {
  370.         return countOfNotes;
  371.     }
  372.  
  373.     void insert(long long key, int offset)
  374.     {
  375.         HashNode node;
  376.         node.key = key;
  377.         node.offset = offset;
  378.         int index = hashFunction(key);
  379.         table[index].push_back(node);
  380.  
  381.         countOfNotes++;
  382.         countOfAdd++;
  383.  
  384.         if (countOfNotes / size > 0.75) {
  385.             rehash();
  386.         }
  387.     }
  388.  
  389.     HashNode* find(long long key, int* compares)
  390.     {
  391.         int index = hashFunction(key);
  392.  
  393.         list<HashNode>::iterator i;
  394.         for (i = table[index].begin(); i != table[index].end(); i++) {
  395.             (*compares)++;
  396.             if (i->key == key) {
  397.                 HashNode* node = new HashNode;
  398.                 node->key = i->key;
  399.                 node->offset = i->offset;
  400.                 return node;
  401.             }
  402.         }
  403.     }
  404.  
  405.     void remove(long long key)
  406.     {
  407.         int index = hashFunction(key);
  408.  
  409.         list<HashNode>::iterator i;
  410.         for (i = table[index].begin(); i != table[index].end(); i++) {
  411.             if (i->key == key)
  412.                 break;
  413.         }
  414.  
  415.         if (i != table[index].end())
  416.             table[index].erase(i);
  417.  
  418.         countOfNotes--;
  419.     }
  420.  
  421.     int hashFunction(int key)
  422.     {
  423.         return key % size;
  424.     }
  425.  
  426.     void rehash() {
  427.         HashTable newHT(size * 2);
  428.         for (int i = 0; i < size; i++) {
  429.             for (HashNode node : table[i]) {
  430.                 newHT.insert(node.key, node.offset);
  431.             }
  432.         }
  433.         size = size * 2;
  434.         table = newHT.table;
  435.     }
  436.  
  437.     void displayHash() {
  438.         cout << "HashTable now:" << endl;
  439.         for (int i = 0; i < size; i++) {
  440.             cout << i;
  441.             for (HashNode node : table[i])
  442.                 cout << " --> " << node.key;
  443.             cout << endl;
  444.         }
  445.         cout << endl;
  446.     }
  447.  
  448.     int getCountOfAdd() {
  449.         return countOfAdd;
  450.     }
  451.  
  452.     long long getMemory() {
  453.         return (sizeof(HashNode) * countOfAdd) / 1024;
  454.     }
  455.  
  456.     void removeAll() {
  457.         delete[] table;
  458.     }
  459.  
  460.     ~HashTable() {
  461.         removeAll();
  462.     }
  463. };
  464.  
  465.  
  466. struct Car {
  467.     long long num;
  468.     char brand[5];
  469.     char ownerName[5];
  470.  
  471.     void print() {
  472.         cout << "==========================" << endl;
  473.         cout << "Car's num: " << num << endl;
  474.         cout << "Brand: " << brand << endl;
  475.         cout << "Owner's name: " << ownerName << endl;
  476.         cout << "==========================" << endl;
  477.     }
  478. };
  479.  
  480. class File {
  481. private:
  482.     string fileName;
  483.     int fileSize;
  484.  
  485. public:
  486.     File(string& fn, int fz) {
  487.         fileName = fn;
  488.         fileSize = fz;
  489.     }
  490.  
  491.     void generateFile() {
  492.         int brandLen = 4, ownerNameLen = 4, numLen = 4;
  493.         char symbol;
  494.  
  495.         ofstream file(fileName, ios::binary | ios::trunc);
  496.  
  497.         for (int i = 0; i < fileSize; i++) {
  498.             Car car;
  499.  
  500.             car.num = 0;
  501.             char tmp[4];
  502.             for (int j = 0; j < numLen; j++) {
  503.                 tmp[j] = (char)('1' + rand() % 9);
  504.             }
  505.             for (int j = numLen - 1; j >= 0; j--) {
  506.                 car.num += (long long)((tmp[j] - '0') * pow(10, numLen - j - 1));
  507.             }
  508.  
  509.             if (i == 0) {
  510.                 cout << "First generated key: " << car.num << endl;
  511.             }
  512.             else if (i == fileSize / 2) {
  513.                 cout << "Middle generated key: " << car.num << endl;
  514.             }
  515.             else if (i == fileSize - 1) {
  516.                 cout << "Last generated key: " << car.num << endl;
  517.             }
  518.  
  519.  
  520.             for (int j = 0; j < brandLen; j++) {
  521.                 symbol = (char)('a' + rand() % 26);
  522.                 car.brand[j] = symbol;
  523.             }
  524.             car.brand[brandLen] = '\0';
  525.  
  526.             for (int j = 0; j < ownerNameLen; j++) {
  527.                 symbol = (char)('a' + rand() % 26);
  528.                 car.ownerName[j] = symbol;
  529.             }
  530.             car.ownerName[ownerNameLen] = '\0';
  531.  
  532.             file.write((char*)&car, sizeof(Car));
  533.         }
  534.  
  535.         file.close();
  536.     }
  537.  
  538.     template<typename T>
  539.     bool insertFromFile(T& container) {
  540.         ifstream file(fileName, ios::binary);
  541.         if (file) {
  542.             for (size_t i = 0; i < fileSize; i++) {
  543.                 Car car;
  544.                 file.read((char*)&car, sizeof(Car));
  545.                 container.insert(car.num, i);
  546.             }
  547.         }
  548.         else {
  549.             return false;
  550.         }
  551.         file.close();
  552.         return true;
  553.     }
  554.  
  555.     template<typename T>
  556.     bool insertInFile(T& container, long long num, char brand[], char ownerName[]) {
  557.         ofstream file(fileName, ios::binary | ios::app | ios::ate);
  558.         if (file) {
  559.             Car car;
  560.             car.num = num;
  561.             strcpy_s(car.brand, brand);
  562.             strcpy_s(car.ownerName, ownerName);
  563.             file.write((char*)&car, sizeof(Car));
  564.             container.insert(num, container.getCountOfAdd());
  565.             file.close();
  566.             return true;
  567.         }
  568.         file.close();
  569.         return false;
  570.     }
  571.  
  572.     template<typename T, typename N>
  573.     Car* findInFile(T& container, long long num, int* compares) {
  574.         N* value = container.find(num, compares);
  575.         if (value) {
  576.             int offset = value->offset;
  577.             ifstream file(fileName, ios::binary);
  578.             if (file) {
  579.                 Car* car = new Car;
  580.                 file.seekg(offset * sizeof(Car), ios::beg);
  581.                 file.read((char*)car, sizeof(Car));
  582.                 file.close();
  583.                 return car;
  584.             }
  585.             file.close();
  586.         }
  587.         return nullptr;
  588.     }
  589.  
  590.     template<typename T, typename N>
  591.     void testFind(T& container, long long num) {
  592.         int compares_count = 0;
  593.         long long start = clock();
  594.         findInFile<T, N>(container, num, &compares_count);
  595.         long long end = clock();
  596.  
  597.         long long time = end - start;
  598.         cout << "Number of compares: " << compares_count << endl;
  599.         cout << "Time: " << time << "us" << endl;
  600.         cout << "==========================" << endl;
  601.     }
  602.  
  603.     template<typename T, typename N>
  604.     bool removeFromFile(T& container, long long num) {
  605.         int plug = 0;
  606.         N* value = container.find(num, &plug);
  607.         if (value) {
  608.             int offset = value->offset;
  609.             fstream file(fileName, ios::binary | ios::out | ios::in);
  610.             if (file) {
  611.                 file.seekp(sizeof(Car) * offset, ios::beg);
  612.                 Car blank;
  613.                 string blankBrand, blankOwnerName;
  614.                 blankBrand.append(4, '-');
  615.                 blankOwnerName.append(4, '-');
  616.                 blank.num = 1000;
  617.                 strcpy_s(blank.brand, blankBrand.c_str());
  618.                 strcpy_s(blank.ownerName, blankOwnerName.c_str());
  619.                 file.write((char*)&blank, sizeof(Car));
  620.             }
  621.             file.close();
  622.             container.remove(num);
  623.             return true;
  624.         }
  625.         return false;
  626.     }
  627. };
  628.  
  629. void mainMenu() {
  630.     cout << "Menu" << endl;
  631.     cout << "1. Task1" << endl;
  632.     cout << "2. Task2" << endl;
  633.     cout << "3. Task3" << endl;
  634.     cout << "0. Exit" << endl;
  635.     cout << endl;
  636. }
  637.  
  638. void firstTaskMenu() {
  639.     cout << endl;
  640.     cout << "Task1" << endl;
  641.     cout << "1. Insert in file and BinarySearchTree" << endl;
  642.     cout << "2. Find in file using BinarySearchTree" << endl;
  643.     cout << "3. Remove from file and BinarySearchTree" << endl;
  644.     cout << "4. Print BinarySearchTree" << endl;
  645.     cout << "0. Exit" << endl;
  646.     cout << endl;
  647. }
  648.  
  649. void secondTaskMenu() {
  650.     cout << endl;
  651.     cout << "Task2" << endl;
  652.     cout << "1. Insert in file and RandomTree" << endl;
  653.     cout << "2. Find in file using RandomTree" << endl;
  654.     cout << "3. Remove from file and RandomTree" << endl;
  655.     cout << "4. Print RandomTree" << endl;
  656.     cout << "5. Get average count of rotations" << endl;
  657.     cout << "0. Exit" << endl;
  658.     cout << endl;
  659. }
  660.  
  661. void thirdTaskMenu() {
  662.     cout << endl;
  663.     cout << "Task3" << endl;
  664.     cout << "1. Find data using 3 containers" << endl;
  665.     cout << "2. Get used memory for 3 containers" << endl;
  666.     cout << "3. Print HashTable" << endl;
  667.     cout << "0. Exit" << endl;
  668.     cout << endl;
  669. }
  670.  
  671. int main()
  672. {
  673.     srand(time(0));
  674.     string fileName = "file.bin";
  675.     int fileSize = 0;
  676.     cout << "Enter file size:" << endl;
  677.     cin >> fileSize;
  678.  
  679.     File file(fileName, fileSize);
  680.     file.generateFile();
  681.  
  682.     long long key = 0;
  683.     string tempBrand, tempOwnerName;
  684.     char brand[5], ownerName[5];
  685.  
  686.     int plug = 0;
  687.  
  688.     int mainOptions = -1;
  689.     while (mainOptions != 0) {
  690.         mainMenu();
  691.         cin >> mainOptions;
  692.         if (mainOptions == 1) {
  693.             int firstTaskOptions = -1;
  694.             BSTree bst;
  695.             file.insertFromFile(bst);
  696.  
  697.             while (firstTaskOptions != 0) {
  698.                 firstTaskMenu();
  699.                 cin >> firstTaskOptions;
  700.                 if (firstTaskOptions == 1) {
  701.                     cout << "Enter car's num, brand and owner's name :" << endl;
  702.                     cin >> key >> tempBrand >> tempOwnerName;
  703.                     strcpy_s(brand, tempBrand.c_str());
  704.                     strcpy_s(ownerName, tempOwnerName.c_str());
  705.                     if (file.insertInFile(bst, key, brand, ownerName)) {
  706.                         cout << "Successful insert" << endl;
  707.                     }
  708.                     else {
  709.                         cout << "Unsuccessful insert" << endl;
  710.                     }
  711.                 }
  712.                 else if (firstTaskOptions == 2) {
  713.                     cout << "Enter num:" << endl;
  714.                     cin >> key;
  715.                     Car* res = file.findInFile<BSTree, BTNode>(bst, key, &plug);
  716.                     if (res) {
  717.                         res->print();
  718.                     }
  719.                     else {
  720.                         cout << "Not found" << endl;
  721.                     }
  722.                 }
  723.                 else if (firstTaskOptions == 3) {
  724.                     cout << "Enter num:" << endl;
  725.                     cin >> key;
  726.                     if (file.removeFromFile<BSTree, BTNode>(bst, key)) {
  727.                         cout << "Successful remove" << endl;
  728.                     }
  729.                     else {
  730.                         cout << "Unsuccessful remove" << endl;
  731.                     }
  732.                 }
  733.                 else if (firstTaskOptions == 4) {
  734.                     bst.print();
  735.                 }
  736.             }
  737.         }
  738.         else if (mainOptions == 2) {
  739.             int secondTaskOptions = -1;
  740.             RTree rt;
  741.             file.insertFromFile(rt);
  742.  
  743.             while (secondTaskOptions != 0) {
  744.                 secondTaskMenu();
  745.                 cin >> secondTaskOptions;
  746.                 if (secondTaskOptions == 1) {
  747.                     cout << "Enter car's num, brand and owner's name:" << endl;
  748.                     cin >> key >> tempBrand >> tempOwnerName;
  749.                     strcpy_s(brand, tempBrand.c_str());
  750.                     strcpy_s(ownerName, tempOwnerName.c_str());
  751.                     if (file.insertInFile(rt, key, brand, ownerName)) {
  752.                         cout << "Successful insert" << endl;
  753.                     }
  754.                     else {
  755.                         cout << "Unsuccessful insert" << endl;
  756.                     }
  757.                 }
  758.                 else if (secondTaskOptions == 2) {
  759.                     cout << "Enter num:" << endl;
  760.                     cin >> key;
  761.                     Car* res = file.findInFile<RTree, RTNode>(rt, key, &plug);
  762.                     if (res) {
  763.                         res->print();
  764.                     }
  765.                     else {
  766.                         cout << "Not found" << endl;
  767.                     }
  768.                 }
  769.                 else if (secondTaskOptions == 3) {
  770.                     cout << "Enter num:" << endl;
  771.                     cin >> key;
  772.                     if (file.removeFromFile<RTree, RTNode>(rt, key)) {
  773.                         cout << "Successful remove" << endl;
  774.                     }
  775.                     else {
  776.                         cout << "Unsuccessful remove" << endl;
  777.                     }
  778.                 }
  779.                 else if (secondTaskOptions == 4) {
  780.                     rt.print();
  781.                 }
  782.                 else if (secondTaskOptions == 5) {
  783.                     cout << "Average rotations: " << rt.getAverageCountOfRotate() << endl;
  784.                 }
  785.             }
  786.         }
  787.         else if (mainOptions == 3) {
  788.             int thirdTaskOptions = -1;
  789.             BSTree bst;
  790.             RTree rt;
  791.             HashTable ht(fileSize*2);
  792.             file.insertFromFile(bst);
  793.             file.insertFromFile(rt);
  794.             file.insertFromFile(ht);
  795.  
  796.             while (thirdTaskOptions != 0) {
  797.                 thirdTaskMenu();
  798.                 cin >> thirdTaskOptions;
  799.                 if (thirdTaskOptions == 1) {
  800.                     cout << "Enter num:" << endl;
  801.                     cin >> key;
  802.                     cout << "BinarySearchTree results:" << endl;
  803.                     file.testFind<BSTree, BTNode>(bst, key);
  804.                     cout << "RandomTree results:" << endl;
  805.                     file.testFind<RTree, RTNode>(rt, key);
  806.                     cout << "HashTable results:" << endl;
  807.                     file.testFind<HashTable, HashNode>(ht, key);
  808.                 }
  809.                 else if (thirdTaskOptions == 2) {
  810.                     cout << "BinarySearchTree used memory: " << bst.getMemory() << "kB" << endl;
  811.                     cout << "RandomTree used memory: " << rt.getMemory() << "kB" << endl;
  812.                     cout << "HashTable used memory: " << ht.getMemory() << "kB" << endl;
  813.                 }
  814.                 else if (thirdTaskOptions == 3) {
  815.                     ht.displayHash();
  816.                     cout << endl;
  817.                 }
  818.             }
  819.         }
  820.     }
  821. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement