C++ program that demonstrates the use of Binary Search Trees (BST) and Linked Lists in a scientific computing context. We'll implement a simple system for managing experimental data points, where we'll use a BST for efficient searching and a linked list for maintaining a sequence of observations.
algorithm, auto, command_line_arguments, exception, iostream, namespace, range-based, sstream, std::cin, std::cout, std::cerr, std::exception, std::ifstream, std::ofstream, stdexcept, throw, vector
#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;
}
This program demonstrates the following concepts in a scientific computing context:
BST
class implements a binary search tree for efficient searching of data points based on their x-coordinate.insert
and search
operations.The BST allows for quick lookup of experimental data points given an x-coordinate.
Singly Linked List:
LinkedList
class implements a singly linked list to maintain the sequence of observations.insert
and display
operations.The linked list preserves the order in which data points were added, which can be important in time-series data or when maintaining the sequence of experiments is crucial.
DataPoint Structure:
Represents an experimental data point with x and y coordinates.
Smart Pointers:
std::unique_ptr
is used for memory management in both the BST and linked list implementations, ensuring proper resource cleanup.
Scientific Computing Application:
This implementation provides a foundation for more complex data management systems in scientific computing applications. It demonstrates how different data structures can be used together to provide both efficient searching (BST) and sequence preservation (linked list) for experimental data.
Related - How can I optimize the search performance in a binary search tree - What are the advantages of using a doubly linked list over a singly linked list - How can I implement insertion and deletion operations in a binary search tree - What are the common use cases for binary trees in scientific computing - How can I visualize the structure of a binary search tree
Previous Page | Course Schedule | Course Content