-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.php
48 lines (38 loc) · 1.55 KB
/
main.php
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
<?php
require_once 'BinarySearchTree.php';
require_once 'Node.php';
$jsonData = file_get_contents('documents.json');
$documents = json_decode($jsonData, true, 512, JSON_THROW_ON_ERROR);
$indexTree = new BinarySearchTree();
foreach ($documents as $document) {
if (isset($document['name'])) {
$indexTree->insert($document['name']);
}
}
$indexData = json_encode($indexTree, JSON_PRETTY_PRINT);
file_put_contents('index.json', $indexData);
$searchTerm = 'Aachen'; // Пример искомого значения
// Поиск с использованием индекса
$searchResult = $indexTree->search($searchTerm);
if ($searchResult !== null) {
echo 'Найденные документы: ' . $searchResult . PHP_EOL;
} else {
echo 'Документы не найдены.' . PHP_EOL;
}
// Поиск без использования индекса (последовательный перебор)
$sequentialSearchResult = [];
$comparisonCount = 0;
foreach ($documents as $document) {
if (isset($document['name'])) {
$comparisonCount++;
if ($document['name'] === $searchTerm) {
$sequentialSearchResult[] = $document;
}
}
}
echo 'Найденные документы (последовательный поиск):' . PHP_EOL;
foreach ($sequentialSearchResult as $document) {
echo 'Название: ' . $document['name'] . PHP_EOL;
// Вывод остальных полей документа...
}
echo 'Количество операций сравнения: ' . $comparisonCount . PHP_EOL;