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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 | #include <iostream>
#include <cmath>
#include <memory>
// Data structure to represent an experimental data point
struct DataPoint {
double x;
double y;
DataPoint(double x, double y) : x(x), y(y) {}
};
// Binary Search Tree Node
struct BSTNode {
DataPoint data;
std::unique_ptr<BSTNode> left;
std::unique_ptr<BSTNode> right;
BSTNode(DataPoint data) : data(data), left(nullptr), right(nullptr) {}
};
// Binary Search Tree for efficient searching of data points
class BST {
private:
std::unique_ptr<BSTNode> root;
void insert(std::unique_ptr<BSTNode>& node, const DataPoint& point) {
if (!node) {
node = std::make_unique<BSTNode>(point);
} else if (point.x < node->data.x) {
insert(node->left, point);
} else if (point.x > node->data.x) {
insert(node->right, point);
}
}
DataPoint* search(BSTNode* node, double x) const {
if (!node) return nullptr;
if (std::abs(node->data.x - x) < 1e-6) return &node->data;
if (x < node->data.x) return search(node->left.get(), x);
return search(node->right.get(), x);
}
public:
void insert(const DataPoint& point) {
insert(root, point);
}
DataPoint* search(double x) const {
return search(root.get(), x);
}
};
// Singly Linked List Node
struct ListNode {
DataPoint data;
std::unique_ptr<ListNode> next;
ListNode(DataPoint data) : data(data), next(nullptr) {}
};
// Singly Linked List for maintaining sequence of observations
class LinkedList {
private:
std::unique_ptr<ListNode> head;
public:
void insert(const DataPoint& point) {
auto newNode = std::make_unique<ListNode>(point);
newNode->next = std::move(head);
head = std::move(newNode);
}
void display() const {
ListNode* current = head.get();
while (current) {
std::cout << "(" << current->data.x << ", " << current->data.y << ") ";
current = current->next.get();
}
std::cout << std::endl;
}
};
int main() {
BST bst;
LinkedList list;
// Insert some experimental data points
DataPoint points[] = {{1.0, 2.0}, {3.0, 4.0}, {2.0, 3.0}, {4.0, 5.0}, {0.5, 1.5}};
for (const auto& point : points) {
bst.insert(point);
list.insert(point);
}
std::cout << "Experimental data points (in insertion order):" << std::endl;
list.display();
// Search for a specific data point in the BST
double searchX = 3.0;
DataPoint* result = bst.search(searchX);
if (result) {
std::cout << "Found point with x = " << searchX << ": (" << result->x << ", " << result->y << ")" << std::endl;
} else {
std::cout << "Point with x = " << searchX << " not found." << std::endl;
}
return 0;
}
|