返回
JavaScript的数据结构与算法,学起来并没那么难!
前端
2023-11-12 16:25:08
当今时代,JavaScript已经成为一门十分重要的语言,在网络开发中占有重要的地位。而数据结构与算法是计算机科学的基础,是学习JavaScript的必备知识。
JavaScript的数据结构有数组、链表、栈、队列、哈希表、二叉树、图等。这些数据结构各有其特点,适合不同的应用场景。
JavaScript的算法有排序算法、搜索算法、动态规划算法等。这些算法可以帮助我们高效地解决各种问题。
掌握了JavaScript的数据结构与算法,我们可以编写出更加高效、健壮的程序。
数组
数组是一种线性数据结构,它存储一系列同一种数据类型的值。数组中的元素可以通过索引来访问。
const numbers = [1, 2, 3, 4, 5];
console.log(numbers[2]); // 输出:3
链表
链表是一种线性数据结构,它由一系列节点组成。每个节点存储一个值和一个指向下一个节点的指针。
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
this.tail = null;
}
add(value) {
const node = new Node(value);
if (!this.head) {
this.head = node;
this.tail = node;
} else {
this.tail.next = node;
this.tail = node;
}
}
remove(value) {
let current = this.head;
let previous = null;
while (current) {
if (current.value === value) {
if (previous) {
previous.next = current.next;
} else {
this.head = current.next;
}
if (current === this.tail) {
this.tail = previous;
}
return;
}
previous = current;
current = current.next;
}
}
search(value) {
let current = this.head;
while (current) {
if (current.value === value) {
return true;
}
current = current.next;
}
return false;
}
}
栈
栈是一种后进先出(LIFO)的数据结构。这意味着后添加的元素将首先被删除。
class Stack {
constructor() {
this.items = [];
}
push(item) {
this.items.push(item);
}
pop() {
return this.items.pop();
}
peek() {
return this.items[this.items.length - 1];
}
isEmpty() {
return this.items.length === 0;
}
}
队列
队列是一种先进先出(FIFO)的数据结构。这意味着先添加的元素将首先被删除。
class Queue {
constructor() {
this.items = [];
}
enqueue(item) {
this.items.push(item);
}
dequeue() {
return this.items.shift();
}
peek() {
return this.items[0];
}
isEmpty() {
return this.items.length === 0;
}
}
哈希表
哈希表是一种非线性数据结构,它使用哈希函数将键映射到值。哈希表可以快速地查找、插入和删除数据。
class HashTable {
constructor() {
this.table = new Array(100);
}
hash(key) {
return key % this.table.length;
}
set(key, value) {
const index = this.hash(key);
this.table[index] = value;
}
get(key) {
const index = this.hash(key);
return this.table[index];
}
remove(key) {
const index = this.hash(key);
delete this.table[index];
}
isEmpty() {
return this.table.every(item => item === undefined);
}
}
二叉树
二叉树是一种树形数据结构,每个节点最多有两个子节点。二叉树可以用于存储数据、查找数据和排序数据。
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
class BinaryTree {
constructor() {
this.root = null;
}
insert(value) {
const node = new Node(value);
if (!this.root) {
this.root = node;
} else {
this._insertNode(node, this.root);
}
}
_insertNode(node, current) {
if (node.value < current.value) {
if (!current.left) {
current.left = node;
} else {
this._insertNode(node, current.left);
}
} else {
if (!current.right) {
current.right = node;
} else {
this._insertNode(node, current.right);
}
}
}
search(value) {
return this._searchNode(value, this.root);
}
_searchNode(value, current) {
if (!current) {
return false;
}
if (current.value === value) {
return true;
}
if (value < current.value) {
return this._searchNode(value, current.left);
} else {
return this._searchNode(value, current.right);
}
}
remove(value) {
this._removeNode(value, this.root);
}
_removeNode(value, current) {
if (!current) {
return;
}
if (value === current.value) {
if (!current.left && !current.right) {
current = null;
} else if (!current.left) {
current = current.right;
} else if (!current.right) {
current = current.left;
} else {
const successor = this._findSuccessor(current.right);
current.value = successor.value;
this._removeNode(successor.value, current.right);
}
} else if (value < current.value) {
this._removeNode(value, current.left);
} else {
this._removeNode(value, current.right);
}
}
_findSuccessor(node) {
if (!node.left) {
return node;
}
return this._findSuccessor(node.left);
}
isEmpty() {
return this.root === null;
}
}
图
图是一种非线性数据结构,它由一系列顶点和边组成。图可以用于表示各种关系,如社交网络、道路网络和计算机网络。
class Graph {
constructor() {
this.vertices = [];
this.edges = [];
}
addVertex(vertex) {
this.vertices.push(vertex);
}
addEdge(vertex1, vertex2) {
this.edges.push({
vertex1,
vertex2
});
}
getNeighbors(vertex) {
return this.edges.filter(edge => edge.vertex1 === vertex || edge.vertex2 === vertex);
}
isEmpty() {
return this.vertices.length === 0 && this.edges.length === 0;
}
}
算法
除了数据结构之外,JavaScript还提供了许多内置的算法,我们可以直接使用这些算法来解决各种问题。
// 排序算法
const numbers = [1, 5,