Нужно исправить класс Tree, представляющий АВЛ-дерево, так, чтобы:
Каждый элемент дерева хранил хеш-таблицу класса HashTable из элементов типа Cell;
В дерево добавлялись элементы типа Sorts, считываемые из файла (чтение файла уже реализовано корректно) и распределялись по дереву по полю productname, а затем добавлялись в хеш-таблицы соответствующих элементов. Соответственно, при поиске по дереву и удалении из него так же применяется поле productname.
Вот код:
#include
#include
#include
#include
#include
#include
using namespace std;
struct VendorCode
{
int vendorcode;
code
VendorCode(int vc) : vendorcode(vc) {}
};
struct ProductName {
std::string productname;
code
ProductName(std::string pn) : productname(pn) {}
};
struct TIN
{
int tin;
code
TIN(int t) : tin(t) {}
};
struct Date {
int day;
int month;
int year;
code
Date(int d, int m, int y) {
bool correct = true;
if (y > 2024 || y < 1900)
correct = false;
else
{
switch (m)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
if (d < 1 || d > 31)
correct = false;
break;
case 4: case 6: case 9: case 11:
if (d < 1 || d > 30)
correct = false;
break;
case 2:
if (d < 1 || d > 28)
correct = false;
break;
default:
correct = false;
break;
}
}
if (correct)
{
day = d;
month = m;
year = y;
}
else
{
std::cerr << "Invalid date. Setting default date to 1.1.2000." << std::endl;
day = 1;
month = 1;
year = 2000;
}
}
if (file.is_open()) {
int vc;
std::string pn;
int t;
int day;
int month;
int year;
while (file >> vc >> pn >> t >> day >> month >> year) {
VendorCode vendorcode(vc);
ProductName productname(pn);
TIN tin(t);
Date date(day, month, year);
sorts.push_back(Sorts(vendorcode, productname, tin, date));
}
file.close();
}
else {
std::cerr << "Enable open file" << filename << std::endl;
}
return sorts;
}
int digitsCount(int n)
{
int c = 0;
while (n != 0)
{
c++;
n /= 10;
}
return c;
}
int hash1(int key1, int key2, int key3, int n)
{
int d = digitsCount(n - 1);
int l = digitsCount(key1);
int e = (2 * l - d) / 2;
int r = pow(10, e);
int h1 = (key1 * key1) / r;
int mod = pow(10, d);
h1 %= mod;
l = digitsCount(key2);
e = (2 * l - d) / 2;
r = pow(10, e);
int h2 = (key2 * key2) / r;
mod = pow(10, d);
h2 %= mod;
l = digitsCount(key3);
e = (2 * l - d) / 2;
r = pow(10, e);
int h3 = (key3 * key3) / r;
mod = pow(10, d);
h3 %= mod;
int h = (h1 + h2 + h3) % n;
return h;
}
int hash2(int key1, int key2, int key3, int j, int k, int n)
{
int hash = (hash1(key1, key2, key3, n) + j * k) % n;
return hash;
}
int collisionInsert(int key1, int key2, int key3, int n, int hash)
{
int h = hash;
int j = 0;
int k = 3;
do
{
if (table[h].sorts.dates.day == key1 && table[h].sorts.dates.month == key2 && table[h].sorts.dates.year == key3)
return -2;
if (table[h].flag != 1)
{
return h;
}
else
{
h = hash2(key1, key2, key3, j, k, n);
++j;
}
} while (j != n);
return -1;
}
int collisionSearch(int key1, int key2, int key3, int n, int hash)
{
int h = hash;
int j = 0;
int k = 3;
do
{
if (table[h].sorts.dates.day == key1 && table[h].sorts.dates.month == key2 && table[h].sorts.dates.year == key3)
{
return h;
}
else
{
h = hash2(key1, key2, key3, j, k, n);
++j;
}
} while (j != n);
return -1;
}
public:
int insertItem(Sorts s)
{
int n = table.size();
int key1 = s.dates.day;
int key2 = s.dates.month;
int key3 = s.dates.year;
int hash = hash1(key1, key2, key3, n);
s.firstHash = hash;
if (table[hash].flag != 1)
{
table[hash].sorts = s;
table[hash].firstHash = s.firstHash;
table[hash].flag = 1;
return 0;
}
else
{
int secondhash = collisionInsert(key1, key2, key3, n, hash);
if (secondhash == -2)
{
std::cout << "Item is already in the table." << std::endl;
return -2;
}
else if (secondhash == -1)
{
std::cout << "Can't insert, table is full." << std::endl;
return -1;
}
else
{
for (int i = 0; i < table.size(); i++)
{
if (table[i].sorts.dates.day == key1 && table[i].sorts.dates.month == key2 && table[i].sorts.dates.year == key3)
return -2;
}
table[secondhash].sorts = s;
table[secondhash].firstHash = s.firstHash;
table[secondhash].flag = 1;
return 1;
}
}
}
int removeItem(int key1, int key2, int key3)
{
int n = table.size();
int hash = hash1(key1, key2, key3, n);
if (table[hash].sorts.dates.day == key1 && table[hash].sorts.dates.month == key2 && table[hash].sorts.dates.year == key3)
{
table[hash].flag = 2;
std::cout << "Removed" << std::endl;
return 0;
}
else
{
int secondhash = collisionSearch(key1, key2, key3, n, hash);
if (secondhash != -1)
{
if (table[secondhash].flag == 1)
{
table[secondhash].flag = 2;
std::cout << "Removed" << std::endl;
return 1;
}
}
std::cout << "Item is not in the table." << std::endl;
return -1;
}
}
HashTable(const std::string readFile, int n)
{
table.resize(n);
std::vector sorts = readFromFile(readFile);
code
for (int i = 0; i < n; i++)
{
if (i < sorts.size())
insertItem(sorts[i]);
}
}
~HashTable()
{
table.clear();
}
int searchItem(int key1, int key2, int key3)
{
int n = table.size();
int hash = hash1(key1, key2, key3, n);
if (table[hash].flag == 1 && table[hash].sorts.dates.day == key1 && table[hash].sorts.dates.month == key2 && table[hash].sorts.dates.year == key3)
{
std::cout << "Found: " << hash << " | " << table[hash].sorts.vendorcodes.vendorcode << " " << table[hash].sorts.productnames.productname << " " << table[hash].sorts.tins.tin << " " << table[hash].sorts.dates.day << "." << table[hash].sorts.dates.month << "." << table[hash].sorts.dates.year << " | first hash: " << table[hash].firstHash << " | flag: " << table[hash].flag << std::endl;
return 0;
}
else
{
int secondhash = collisionSearch(key1, key2, key3, n, hash);
if (secondhash != -1)
{
if (table[secondhash].flag == 1)
{
std::cout << "Found: " << hash << " | " << table[hash].sorts.vendorcodes.vendorcode << " " << table[hash].sorts.productnames.productname << " " << table[hash].sorts.tins.tin << " " << table[hash].sorts.dates.day << "." << table[hash].sorts.dates.month << "." << table[hash].sorts.dates.year << " | first hash: " << table[hash].firstHash << " | flag: " << table[hash].flag << std::endl;
return 1;
}
}
std::cout << "Item is not in the table." << std::endl;
return -1;
}
}
void printTable()
{
for (int i = 0; i < table.size(); i++)
{
if (table[i].flag != 1)
std::cout << i << " | flag: " << table[i].flag << std::endl;
else
std::cout << i << " | " << table[i].sorts.vendorcodes.vendorcode << " " << table[i].sorts.productnames.productname << " " << table[i].sorts.tins.tin << " " << table[i].sorts.dates.day << "." << table[i].sorts.dates.month << "." << table[i].sorts.dates.year << " | first hash: " << table[i].firstHash << " | flag: " << table[i].flag << std::endl;
}
}
void writeFile(std::string file)
{
std::ofstream out(file);
for (int i = 0; i < table.size(); i++)
{
if (table[i].flag != 1)
out << i << " | flag: " << table[i].flag << std::endl;
else
out << i << " | " << table[i].sorts.vendorcodes.vendorcode << " " << table[i].sorts.productnames.productname << " " << table[i].sorts.tins.tin << " " << table[i].sorts.dates.day << "." << table[i].sorts.dates.month << "." << table[i].sorts.dates.year << " | first hash: " << table[i].firstHash << " | flag: " << table[i].flag << std::endl;
}
}