commit 7368807c68e88f790a6aa6510e8200e20866da89 Author: grtsinry43 Date: Thu May 8 12:01:17 2025 +0800 Initial Commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..314b094 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +cmake-build-debug/ \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..5db2a96 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +data_structure_homework \ No newline at end of file diff --git a/.idea/data-structure-homework.iml b/.idea/data-structure-homework.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/.idea/data-structure-homework.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 0000000..30bab2a --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/.idea/editor.xml b/.idea/editor.xml new file mode 100644 index 0000000..25c6c37 --- /dev/null +++ b/.idea/editor.xml @@ -0,0 +1,344 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0b76fe5 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..3314090 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..eaa3cf7 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.31) +project(data_structure_homework) + +set(CMAKE_CXX_STANDARD 20) + +add_executable(data_structure_homework main.cpp + problem1.cpp + problem1.h + problem2.cpp + problem2.h) diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..460608d --- /dev/null +++ b/main.cpp @@ -0,0 +1,7 @@ +#include "problem1.h" +#include "problem2.h" + +int main() { + problem_1(); + problem_2(); +} diff --git a/problem1.cpp b/problem1.cpp new file mode 100644 index 0000000..0fccae8 --- /dev/null +++ b/problem1.cpp @@ -0,0 +1,76 @@ +// +// Created by grtsinry43 on 5/8/25. +// + +#include +#include +#include +#include +#include +using namespace std; + +struct Edge { + int to, weight; +}; + +void dijkstra(int source, const vector > &graph) { + int n = graph.size(); + vector dist(n, INT_MAX); // 存储最短路径长度 + vector prev(n, -1); // 存储路径 + dist[source] = 0; + + // 优先队列,按距离从小到大排序 + priority_queue, vector >, greater<> > pq; + pq.push({0, source}); + + while (!pq.empty()) { + auto [d, u] = pq.top(); + pq.pop(); + + if (d > dist[u]) continue; + + for (const auto &edge: graph[u]) { + int v = edge.to, weight = edge.weight; + if (dist[u] + weight < dist[v]) { + dist[v] = dist[u] + weight; + prev[v] = u; + pq.push({dist[v], v}); + } + } + } + + // 输出结果 + for (int i = 0; i < n; ++i) { + if (dist[i] == INT_MAX) { + cout << "Node " << i << " is unreachable from source " << source << ".\n"; + } else { + cout << "Shortest distance to node " << i << " is " << dist[i] << ". Path: "; + vector path; + for (int at = i; at != -1; at = prev[at]) { + path.push_back(at); + } + ranges::reverse(path.begin(), path.end()); + for (int j = 0; j < path.size(); ++j) { + cout << path[j] << (j + 1 < path.size() ? " -> " : "\n"); + } + } + } +} + +int problem_1() { + int n = 5; // 节点数 + vector > graph(n); + + // 添加边 (示例) + graph[0].push_back({1, 2}); + graph[0].push_back({2, 4}); + graph[1].push_back({2, 1}); + graph[1].push_back({3, 7}); + graph[2].push_back({4, 3}); + graph[3].push_back({4, 1}); + + int source = 0; // 源点 + dijkstra(source, graph); + + return 0; +} diff --git a/problem1.h b/problem1.h new file mode 100644 index 0000000..30079cc --- /dev/null +++ b/problem1.h @@ -0,0 +1,10 @@ +// +// Created by grtsinry43 on 5/8/25. +// + +#ifndef PROBLEM1_H +#define PROBLEM1_H + +int problem_1(); + +#endif //PROBLEM1_H diff --git a/problem2.cpp b/problem2.cpp new file mode 100644 index 0000000..2055441 --- /dev/null +++ b/problem2.cpp @@ -0,0 +1,127 @@ +// +// Created by grtsinry43 on 5/8/25. +// + +#include "problem2.h" + +#include +#include +using namespace std; + +struct Student { + int id; // 学号 + int score; // 成绩 + Student(int id, int score) : id(id), score(score) {} +}; + +struct TreeNode { + Student data; + TreeNode* left; + TreeNode* right; + explicit TreeNode(Student data) : data(data), left(nullptr), right(nullptr) {} +}; + +class BST { +private: + TreeNode* root; + + TreeNode* insert(TreeNode* node, Student data) { + if (!node) return new TreeNode(data); + if (data.id < node->data.id) + node->left = insert(node->left, data); + else if (data.id > node->data.id) + node->right = insert(node->right, data); + return node; + } + + TreeNode* findMin(TreeNode* node) { + while (node && node->left) node = node->left; + return node; + } + + TreeNode* remove(TreeNode* node, int id) { + if (!node) return nullptr; + if (id < node->data.id) + node->left = remove(node->left, id); + else if (id > node->data.id) + node->right = remove(node->right, id); + else { + if (!node->left) { + TreeNode* temp = node->right; + delete node; + return temp; + } else if (!node->right) { + TreeNode* temp = node->left; + delete node; + return temp; + } + TreeNode* temp = findMin(node->right); + node->data = temp->data; + node->right = remove(node->right, temp->data.id); + } + return node; + } + + TreeNode* search(TreeNode* node, int id) { + if (!node || node->data.id == id) return node; + if (id < node->data.id) + return search(node->left, id); + return search(node->right, id); + } + + void inorder(TreeNode* node) { + if (!node) return; + inorder(node->left); + cout << "ID: " << node->data.id << ", Score: " << node->data.score << endl; + inorder(node->right); + } + +public: + BST() : root(nullptr) {} + + void insert(Student data) { + root = insert(root, data); + } + + void remove(int id) { + root = remove(root, id); + } + + void search(int id) { + TreeNode* result = search(root, id); + if (result) + cout << "Found - ID: " << result->data.id << ", Score: " << result->data.score << endl; + else + cout << "Student with ID " << id << " not found." << endl; + } + + void display() { + inorder(root); + } +}; + +int problem_2() { + BST bst; + + // 插入学生成绩 + bst.insert(Student(101, 85)); + bst.insert(Student(102, 90)); + bst.insert(Student(103, 78)); + bst.insert(Student(104, 88)); + + cout << "All students:\n"; + bst.display(); + + // 查找学生成绩 + cout << "\nSearching for student with ID 102:\n"; + bst.search(102); + + // 删除学生信息 + cout << "\nRemoving student with ID 103:\n"; + bst.remove(103); + + cout << "\nAll students after removal:\n"; + bst.display(); + + return 0; +} \ No newline at end of file diff --git a/problem2.h b/problem2.h new file mode 100644 index 0000000..97765b9 --- /dev/null +++ b/problem2.h @@ -0,0 +1,11 @@ +// +// Created by grtsinry43 on 5/8/25. +// + +#ifndef PROBLEM2_H +#define PROBLEM2_H + +int problem_2(); + + +#endif //PROBLEM2_H