当前位置: 首页 > news >正文

数据结构第一弹-数据结构在不同领域的应用

大家好,今天和大家一起总结一下数据结构在不同领域和场景的应用~

不同的数据结构适用于解决不同类型的问题,从简单的数组到复杂的图结构,每种数据结构都有其独特的应用场景。

1. 数组与链表

1.1 概念

  • 数组:一种线性数据结构,其中元素按照顺序排列,每个元素可以通过索引快速访问。
  • 链表:另一种线性数据结构,由一系列节点组成,每个节点包含数据部分和指向下一个节点的引用(指针)。

1.2 应用领域

  • 数据库系统:使用数组来存储固定大小的记录集,利用链表来管理动态增长的数据集合。
  • 操作系统:进程调度中的队列通常采用链表形式,以支持高效的插入和删除操作。

1.3 代码

链表实现

class ListNode {int val;ListNode next;ListNode(int x) {val = x;next = null;}
}public class LinkedList {private ListNode head;public void add(int data) {if (head == null) {head = new ListNode(data);} else {ListNode current = head;while (current.next != null) {current = current.next;}current.next = new ListNode(data);}}public void printList() {ListNode temp = head;while (temp != null) {System.out.print(temp.val + " ");temp = temp.next;}System.out.println();}
}

2. 栈与队列

2.1 概念

  • :遵循后进先出(LIFO)原则的数据结构。
  • 队列:遵循先进先出(FIFO)原则的数据结构。

2.2 应用领域

  • 编译器设计:表达式求值、函数调用等常用栈来实现。
  • 操作系统:任务调度、消息传递等场景中广泛使用队列。

2.3 代码

栈实现

import java.util.Stack;public class StackExample {public static void main(String[] args) {Stack<Integer> stack = new Stack<>();stack.push(10);stack.push(20);stack.push(30);System.out.println("Stack: " + stack);System.out.println("Popped: " + stack.pop());System.out.println("After popping: " + stack);}
}

队列实现

import java.util.LinkedList;
import java.util.Queue;public class QueueExample {public static void main(String[] args) {Queue<Integer> queue = new LinkedList<>();queue.add(10);queue.add(20);queue.add(30);System.out.println("Queue: " + queue);System.out.println("Removed: " + queue.remove());System.out.println("After removing: " + queue);}
}

3. 堆

3.1 概念

堆是一种特殊的完全二叉树,分为最大堆和最小堆两种类型。最大堆要求父节点的值大于或等于其子节点的值;最小堆则相反。

3.2 应用领域

  • 优先级队列:用于任务调度、事件处理等需要按优先级排序的应用。
  • 堆排序:基于堆的高效排序算法。

3.3 代码

最小堆实现

import java.util.PriorityQueue;public class MinHeapExample {public static void main(String[] args) {PriorityQueue<Integer> minHeap = new PriorityQueue<>();// 添加元素minHeap.add(10);minHeap.add(20);minHeap.add(15);minHeap.add(5);// 打印堆System.out.println("Min Heap: " + minHeap);// 删除最小元素System.out.println("Removed: " + minHeap.poll());System.out.println("After removing: " + minHeap);}
}

4. 图

4.1 概念

图是由顶点(节点)和边组成的非线性数据结构。边可以是有向的也可以是无向的,还可以有权重。

4.2 应用领域

  • 社交网络分析:用户之间的关系可以用图来表示。
  • 路由算法:如Dijkstra算法和A*算法,用于寻找最短路径。
  • 推荐系统:基于用户行为构建图模型,进行个性化推荐。

4.3 代码

无向图的邻接矩阵表示

public class Graph {private final int V; // 顶点数量private int[][] adjMatrix; // 邻接矩阵public Graph(int v) {V = v;adjMatrix = new int[V][V];}public void addEdge(int v, int w) {adjMatrix[v][w] = 1;adjMatrix[w][v] = 1; // 无向图}public void printGraph() {for (int i = 0; i < V; i++) {for (int j = 0; j < V; j++) {System.out.print(adjMatrix[i][j] + " ");}System.out.println();}}
}

Dijkstra算法

import java.util.Arrays;import java.util.PriorityQueue;public class DijkstraAlgorithm {private static final int INF = Integer.MAX_VALUE;public static int[] dijkstra(int[][] graph, int src) {int V = graph.length;int[] dist = new int[V];Arrays.fill(dist, INF);dist[src] = 0;PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[1] - b[1]);pq.offer(new int[]{src, 0});while (!pq.isEmpty()) {int[] u = pq.poll();int uNode = u[0], uDist = u[1];if (uDist > dist[uNode]) continue;for (int v = 0; v < V; v++) {if (graph[uNode][v] != 0) { // 有边int newDist = uDist + graph[uNode][v];if (newDist < dist[v]) {dist[v] = newDist;pq.offer(new int[]{v, newDist});}}}}return dist;}public static void main(String[] args) {int[][] graph = {{0, 4, 0, 0, 0, 0, 0, 8, 0},{4, 0, 8, 0, 0, 0, 0, 11, 0},{0, 8, 0, 7, 0, 4, 0, 0, 2},{0, 0, 7, 0, 9, 14, 0, 0, 0},{0, 0, 0, 9, 0, 10, 0, 0, 0},{0, 0, 4, 14, 10, 0, 2, 0, 0},{0, 0, 0, 0, 0, 2, 0, 1, 6},{8, 11, 0, 0, 0, 0, 1, 0, 7},{0, 0, 2, 0, 0, 0, 6, 7, 0}};int[] distances = dijkstra(graph, 0);System.out.println("Shortest distances from source 0: " + Arrays.toString(distances));}
}

5. Trie树

5.1 概念

Trie树,也称为前缀树或字典树,是一种用于存储字符串集合的数据结构。它特别适用于快速查找和插入操作,尤其是在处理大量字符串时表现优异。

5.2 应用领域

  • 搜索引擎:利用Trie树进行关键词索引,加速搜索过程。
  • 词频统计:在文本处理中快速统计单词出现频率。
  • 自动补全:输入法中的候选词推荐功能。

5.3 代码

Trie树实现

class TrieNode {private final int ALPHABET_SIZE = 26;TrieNode[] children = new TrieNode[ALPHABET_SIZE];boolean isEndOfWord;public TrieNode() {isEndOfWord = false;for (int i = 0; i < ALPHABET_SIZE; i++) {children[i] = null;}}
}public class Trie {private TrieNode root;public Trie() {root = new TrieNode();}// 插入单词public void insert(String key) {TrieNode pCrawl = root;for (int level = 0; level < key.length(); level++) {int index = key.charAt(level) - 'a';if (pCrawl.children[index] == null)pCrawl.children[index] = new TrieNode();pCrawl = pCrawl.children[index];}pCrawl.isEndOfWord = true;}// 搜索单词public boolean search(String key) {TrieNode pCrawl = root;for (int level = 0; level < key.length(); level++) {int index = key.charAt(level) - 'a';if (pCrawl.children[index] == null)return false;pCrawl = pCrawl.children[index];}return (pCrawl != null && pCrawl.isEndOfWord);}// 删除单词public void delete(TrieNode root, String key, int depth) {if (root == null)return;if (depth == key.length()) {if (root.isEndOfWord)root.isEndOfWord = false;if (isEmpty(root))root = null;return;}int index = key.charAt(depth) - 'a';delete(root.children[index], key, depth + 1);if (isEmpty(root) && !root.isEndOfWord) {root = null;}}private boolean isEmpty(TrieNode node) {for (int i = 0; i < 26; i++) {if (node.children[i] != null)return false;}return true;}
}

6. 并查集

6.1 概念

并查集是一种用来管理不相交集合的数据结构,支持合并两个集合以及查询两个元素是否属于同一个集合的操作。

6.2 应用领域

  • 社交网络:确定用户之间的关系网。
  • 图像分割:基于像素相似性对图像进行区域划分。
  • 最小生成树算法:如Kruskal算法,在构建过程中使用并查集避免形成环。

6.3 代码

并查集实现

public class UnionFind {private int[] parent;private int[] rank;public UnionFind(int size) {parent = new int[size];rank = new int[size];for (int i = 0; i < size; i++) {parent[i] = i;rank[i] = 0;}}// 查找public int find(int x) {if (parent[x] != x) {parent[x] = find(parent[x]);  // 路径压缩}return parent[x];}// 合并public void union(int x, int y) {int rootX = find(x);int rootY = find(y);if (rootX != rootY) {if (rank[rootX] > rank[rootY]) {parent[rootY] = rootX;} else if (rank[rootX] < rank[rootY]) {parent[rootX] = rootY;} else {parent[rootY] = rootX;rank[rootX]++;}}}// 判断是否属于同一集合public boolean isConnected(int x, int y) {return find(x) == find(y);}
}

数组与链表为基本的数据存储提供了基础支持,而栈与队列为特定的操作模式提供了高效的解决方案。堆则在优先级队列和排序算法中发挥着重要作用,图结构则广泛应用于复杂的关系建模。Trie树和并查集作为高级数据结构,在处理特定问题时提供了更优的性能。

http://www.lryc.cn/news/500612.html

相关文章:

  • 如何创建基于udp的客户端和服务端
  • ThinkPHP框架审计--基础
  • Java8 CompletableFuture异步编程
  • Java的Mvc整合Swagger的knife4框架
  • 分阶段构建在复杂系统中的应用:以推荐系统为例
  • 2024年12月9日历史上的今天大事件早读
  • 快捷构建AI大模型,源码自取可直接运行
  • 怎么为开源项目做贡献提PR?
  • 如何在 JavaScript 中设置定时器?
  • 【学习路线】Java
  • [GYCTF2020]Easyphp
  • JavaScript 数组的高级用法与最佳实践
  • 通信协议 http、tcp、udp
  • Scala的隐式对象和隐式类
  • 【AIGC】2016-ACCV-即时追捕:自然环境下的自动唇音同步
  • 启智畅想集装箱箱号识别算法,2台相机即可实现较高识别率
  • 让IIS支持PUT请求解决IIS里不支持PUT请求的问题405 Method Not Allowed
  • 入门级捡垃圾工作站记录
  • 2024.12.9——攻防世界ics-06
  • 微信小程序介绍-以及写项目流程(重要)
  • 国内国际标准!羊毛衫检测项目、检测要求及标准
  • MySQL知识大总结(进阶)
  • 【C语言】库函数常见的陷阱与缺陷(2):字符串转化函数
  • 渗透测试基础
  • 传奇996_53——后端ui窗口局部刷新
  • C++ constexpr vs const
  • 【达梦数据库】存储过程调用实践案例-select
  • 041_Compare_Matrix_Squre_Sum_in_MATLAB中矩阵平方和的比较
  • TimeXplusplus——提高时间序列数据的可解释性,避免琐解和分布偏移问题的深度学习可解释性的框架
  • 批处理读取文本第n行并赋值给变量?--遍历所有行并赋值给变量数组