-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
90 lines (73 loc) · 2.45 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
#include <vector>
#include <fstream>
#include "TwoThreeTree.h"
void buildTreeFromFile(const std::string& filename, TwoThreeTree& tree);
int main() {
try {
TwoThreeTree tree;
buildTreeFromFile("test.txt", tree);
std::cout << std::endl << "Tree:" << std::endl;
std::vector<std::string> wordList;
tree.print(wordList);
std::cout << std::endl;
// Test insert
TwoThreeTree test;
std::cout << "Testing insert operation:" << std::endl;
std::cout << " insert banana" << std::endl;
test.insert("banana", 20);
std::cout << std::endl;
// Test search
std::cout << "Testing search operation:" << std::endl;
Node* result = test.search("banana");
if (result) {
std::cout << "Word found: " << result->keys[0] << std::endl;
}
else {
std::cout << "Word not found." << std::endl;
}
std::cout << std::endl;
// Test remove
std::cout << "Testing remove operation:" << std::endl;
test.remove("banana");
result = test.search("banana");
if (result) {
std::cout << "Word found: " << result->keys[0] << std::endl;
}
else {
std::cout << "Word not found." << std::endl;
}
}
catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
return 0;
}
void buildTreeFromFile(const std::string& filename, TwoThreeTree& tree) {
std::ifstream inputFile(filename);
if (!inputFile) {
throw std::runtime_error("Failed to open the input file.");
}
std::string line;
int lineNumber = 1;
while (std::getline(inputFile, line)) {
std::istringstream iss(line);
std::string word;
std::cout << lineNumber << " ";
while (iss >> word) {
std::cout << word << " ";
word.erase(std::remove_if(word.begin(), word.end(), [](char c) {
return std::ispunct(static_cast<unsigned char>(c)) or std::isdigit(static_cast<unsigned char>(c));
}), word.end());
if (!word.empty()) {
tree.insert(word, lineNumber);
}
}
lineNumber++;
std::cout << std::endl;
}
inputFile.close();
}