数据结构和算法练习网站_视频和练习介绍了10种常见数据结构

数据结构和算法练习网站

“Bad programmers worry about the code. Good programmers worry about data structures and their relationships.” — Linus Torvalds, creator of Linux
“糟糕的程序员担心代码。 好的程序员担心数据结构及其关系。” — Linux的创建者Linus Torvalds

**Update** My video course about Algorithms is now live! Check out Algorithms in Motion from Manning Publications. Get 39% off my course by using code ‘39carnes’! Or you can get 50% off my Deep Learning in Motion course with code ‘vlcarnes2’.

**更新** 我有关算法的视频课程现已上线! 从Manning出版物中检验运动中的算法 使用代码“ 39carnes ”可获得39%的课程折扣 或者,您可以使用代码“ vlcarnes2 ”获得“ 深度学习运动”课程的 50%折扣。

Data structures are a critical part of software development, and one of the most common topics for developer job interview questions.

数据结构是软件开发的关键部分,也是开发人员求职面试问题的最常见主题之一。

The good news is that they’re basically just specialized formats for organizing and storing data.

好消息是,它们基本上只是组织和存储数据的专用格式。

I’m going to teach you 10 of the most common data structures — right here in this short article.

我将在这篇简短的文章中教您10种最常见的数据结构。

I’ve embedded videos that I created for each of these data structures. I’ve also linked to code examples for each of them, which show how to implement these in JavaScript.

我已经嵌入了为每个数据结构创建的视频。 我还链接了每个示例的代码示例,这些示例显示了如何在JavaScript中实现这些示例。

And to give you some practice, I’ve linked to challenges from the freeCodeCamp curriculum.

为了给您一些实践,我已经链接到freeCodeCamp课程的挑战。

Note that some of these data structures include time complexity in Big O notation. This isn’t included for all of them since the time complexity is sometimes based on how it’s implemented. If you want to learn more about Big O Notation, check out my article about it or this video by Briana Marie.

请注意,其中一些数据结构在Big O表示法中包括时间复杂度。 由于时间复杂度有时取决于实现方式,因此并非所有功能都包含此功能。 如果您想了解更多有关Big O Notation的信息,请查看我的相关文章Briana Marie的 视频

Also note that even though I show how to implement these data structures in JavaScript, for most of them you would never need to implement them yourself, unless you were using a low-level language like C.

还要注意,即使我展示了如何在JavaScript中实现这些数据结构,对于大多数数据结构,您都不需要自己实现它们,除非您使用的是C之类的低级语言。

JavaScript (like most high-level languages) has built-in implementations of many of these data structures.

JavaScript(像大多数高级语言一样)具有许多这些数据结构的内置实现。

Still, knowing how to implement these data structures will give you a huge edge in your developer job search, and may come in handy when you’re trying to write high-performance code.

不过,知道如何实现这些数据结构将为您在开发人员的工作搜索中提供巨大的优势,并且在您尝试编写高性能代码时可能会派上用场。

链表 (Linked Lists)

A linked list is one of the most basic data structures. It is often compared to an array since many other data structures can be implemented with either an array or a linked list. They each have advantages and disadvantages.

链表是最基本的数据结构之一。 通常将其与数组进行比较,因为可以使用数组或链表实现许多其他数据结构。 它们各有优缺点。

A linked list consists of a group of nodes which together represent a sequence. Each node contains two things: the actual data being stored (which can be basically any type of data) and a pointer (or link) to the next node in the sequence. There are also doubly linked lists where each node has a pointer to both the next item and the previous item in the list.

链表由一组节点组成,这些节点一起代表一个序列。 每个节点包含两件事:正在存储的实际数据(基本上可以是任何类型的数据)和指向序列中下一个节点的指针(或链接)。 还有双向链接的列表,其中每个节点都有一个指向列表中的下一项和上一项的指针。

The most basic operations in a linked list are adding an item to the list, deleting an item from the list, and searching the list for an item.

链接列表中最基本的操作是将项目添加到列表,从列表中删除项目以及在列表中搜索项目。

See the code for a linked list in JavaScript here.

在此处查看JavaScript中的链表的代码。

链表时间复杂度 (Linked list time complexity)
AlgorithmAverageWorst Case
Space0(n)0(n)
Search0(n)0(n)
Insert0(1)0(1)
Delete0(1)0(1)
算法 平均 最糟糕的情况
空间 0(n) 0(n)
搜索 0(n) 0(n)
0(1) 0(1)
删除 0(1) 0(1)
freeCodeCamp的挑战 (freeCodeCamp challenges)

堆栈 (Stacks)

A stack is a basic data structure where you can only insert or delete items at the top of the stack. It is kind of similar to a stack of books. If you want to look at a book in the middle of the stack you must take all of the books above it off first.

堆栈是一种基本的数据结构,您只能在其中插入或删除堆栈顶部的项目。 这有点像一堆书。 如果要看书架中间的一本书,则必须先取走书架上方的所有书。

The stack is considered LIFO (Last In First Out) — meaning the last item you put in the stack is the first item that comes out of the stack

堆栈被认为是LIFO(后进先出)-意味着您放入堆栈中的最后一个项目是从堆栈中出来的第一个项目

There are three main operations that can be performed on stacks: inserting an item into a stack (called ‘push’), deleting an item from the stack (called ‘pop’), and displaying the contents of the stack (sometimes called ‘pip’).

可以在堆栈上执行三个主要操作:将项目插入堆栈(称为“推”),从堆栈中删除项目(称为“ pop”)以及显示堆栈的内容(有时称为“ pip”) ')。

See the code for a stack in JavaScript here.

在此处查看JavaScript中的堆栈代码。

堆栈时间复杂度 (Stack time complexity)
AlgorithmAverageWorst Case
Space0(n)0(n)
Search0(n)0(n)
Insert0(1)0(1)
Delete0(1)0(1)
算法 平均 最糟糕的情况
空间 0(n) 0(n)
搜索 0(n) 0(n)
0(1) 0(1)
删除 0(1) 0(1)
freeCodeCamp的挑战 (freeCodeCamp challenges)

Queue列 (Queues)

You can think of a queue as a line of people at a grocery store. The first one in the line is the first one to be served. Just like a queue.

您可以将队列视为杂货店中的一排人。 该行中的第一个是要投放的第一个。 就像一个队列。

A queue is considered FIFO (First In First Out) to demonstrate the way it accesses data. This means that once a new element is added, all elements that were added before have to be removed before the new element can be removed.

队列被视为FIFO(先进先出)以演示其访问数据的方式。 这意味着一旦添加了新元素,则必须先删除之前添加的所有元素,然后才能删除新元素。

A queue has just two main operations: enqueue and dequeue. Enqueue means to insert an item into the back of the queue and dequeue means removing the front item.

队列只有两个主要操作:入队和出队。 入队意味着将项目插入队列的后面,而出队则意味着除去前项。

See the code for a queue in JavaScript here.

在此处查看JavaScript中的队列代码。

队列时间复杂度 (Queue time complexity)
AlgorithmAverageWorst Case
Space0(n)0(n)
Search0(n)0(n)
Insert0(1)0(1)
Delete0(1)0(1)
算法 平均 最糟糕的情况
空间 0(n) 0(n)
搜索 0(n) 0(n)
0(1) 0(1)
删除 0(1) 0(1)
freeCodeCamp的挑战 (freeCodeCamp challenges)

套装 (Sets)

The set data structure stores values without any particular order and with no repeated values. Besides being able to add and remove elements to a set, there are a few other important set functions that work with two sets at once.

设置的数据结构存储的值没有任何特定的顺序,并且没有重复的值。 除了能够向集合中添加和删除元素外,还有一些其他重要的集合函数可以同时处理两个集合。

  • Union — This combines all the items from two different sets and returns this as a new set (with no duplicates).

    联合—合并来自两个不同集合的所有项目,并将其作为新集合返回(没有重复项)。
  • Intersection — Given two sets, this function returns another set that has all items that are part of both sets.

    交集-给定两个集合,此函数将返回另一个集合,该集合具有两个集合中的所有项。
  • Difference — This returns a list of items that are in one set but NOT in a different set.

    差异—这将返回一组中的项目列表,但不在另一组中。
  • Subset — This returns a boolean value that shows if all the elements in one set are included in a different set.

    子集-返回一个布尔值,该值显示一个集中的所有元素是否包含在另一个集中。

View the code to implement a set in JavaScript here.

在此处查看代码以在JavaScript中实现集合。

freeCodeCamp的挑战 (freeCodeCamp challenges)

地图 (Maps)

A map is a data structure that stores data in key / value pairs where every key is unique. A map is sometimes called an associative array or dictionary. It is often used for fast look-ups of data. Maps allow the following things:

映射是将数据存储在键/值对中的数据结构,其中每个键都是唯一的。 映射有时称为关联数组或字典。 它通常用于快速查找数据。 地图允许以下内容:

  • the addition of a pair to the collection

    在收藏中增加一对
  • the removal of a pair from the collection

    从集合中删除一对
  • the modification of an existing pair

    现有对的修改
  • the lookup of a value associated with a particular key

    与特定键关联的值的查找

View the code to implement a map in JavaScript here.

在此处查看代码以在JavaScript中实现地图。

freeCodeCamp的挑战 (freeCodeCamp challenges)

哈希表 (Hash Tables)

A hash table is a map data structure that contains key / value pairs. It uses a hash function to compute an index into an array of buckets or slots, from which the desired value can be found.

哈希表是一种包含键/值对的地图数据结构。 它使用哈希函数来计算存储桶或插槽数组的索引,从中可以找到所需的值。

The hash function usually takes a string as input and it outputs an numerical value. The hash function should always give the same output number for the same input. When two inputs hash to the same numerical output, this is called a collision. The goal is to have few collisions.

哈希函数通常将字符串作为输入,并输出一个数值。 散列函数应始终为相同的输入提供相同的输出编号。 当两个输入哈希到相同的数字输出时,这称为冲突。 目标是几乎没有碰撞。

So when you input a key / value pair into a hash table, the key is run through the hash function and turned into a number. This numerical value is then used as the actual key that the value is stored by. When you try to access the same key again, the hashing function will process the key and return the same numerical result. The number will then be used to look up the associated value. This provides very efficient O(1) lookup time on average.

因此,当您在哈希表中输入键/值对时,键将通过哈希函数运行并转换为数字。 然后将此数字值用作存储该值的实际键。 当您尝试再次访问相同的键时,哈希函数将处理该键并返回相同的数字结果。 然后,该数字将用于查找关联的值。 平均而言,这提供了非常有效的O(1)查找时间。

View the code for a hash table here.

在此处查看哈希表的代码。

哈希表时间复杂度 (Hash table time complexity)
AlgorithmAverageWorst Case
Space0(n)0(n)
Search0(1)0(n)
Insert0(1)0(n)
Delete0(1)0(n)
算法 平均 最糟糕的情况
空间 0(n) 0(n)
搜索 0(1) 0(n)
0(1) 0(n)
删除 0(1) 0(n)
freeCodeCamp的挑战 (freeCodeCamp challenges)

二进制搜索树 (Binary Search Tree)

A tree is a data structure composed of nodes It has the following characteristics:

树是由节点组成的数据结构,具有以下特征:

  1. Each tree has a root node (at the top).

    每棵树都有一个根节点(在顶部)。
  2. The root node has zero or more child nodes.

    根节点具有零个或多个子节点。
  3. Each child node has zero or more child nodes, and so on.

    每个子节点都有零个或多个子节点,依此类推。

A binary search tree adds these two characteristics:

二进制 搜索树添加了以下两个特征:

  1. Each node has up to two children.

    每个节点最多有两个孩子。
  2. For each node, its left descendents are less than the current node, which is less than the right descendents.

    对于每个节点,其左后代小于当前节点,而当前节点小于右后代。

Binary search trees allow fast lookup, addition and removal of items. The way that they are set up means that, on average, each comparison allows the operations to skip about half of the tree, so that each lookup, insertion or deletion takes time proportional to the logarithm of the number of items stored in the tree.

二进制搜索树允许快速查找,添加和删除项目。 设置它们的方式意味着,平均而言,每个比较都允许操作跳过树的大约一半,因此每次查找,插入或删除所花的时间与树中存储的项目数的对数成正比。

View the code for a binary search tree in JavaScript here.

在此处查看JavaScript中的二进制搜索树的代码

二进制搜索时间复杂度 (Binary search time complexity)
AlgorithmAverageWorst Case
Space0(n)0(n)
Search0(log n)0(n)
Insert0(log n)0(n)
Delete0(log n)0(n)
算法 平均 最糟糕的情况
空间 0(n) 0(n)
搜索 0(log n) 0(n)
0(log n) 0(n)
删除 0(log n) 0(n)
freeCodeCamp的挑战 (freeCodeCamp challenges)

特里 (Trie)

The trie (pronounced ‘try’), or prefix tree, is a kind of search tree. A trie stores data in steps where each step is a node in the trie. Tries are often used to store words for quick lookup, such as a word auto-complete feature.

trie(读作“ try”)或前缀树是一种搜索树。 特里树按步骤存储数据,其中每个步骤都是特里树中的一个节点。 尝试通常用于存储单词以进行快速查找,例如单词自动完成功能。

Each node in a language trie contains one letter of a word. You follow the branches of a trie to spell a word, one letter at a time. The steps begin to branch off when the order of the letters diverge from the other words in the trie, or when a word ends. Each node contains a letter (data) and a boolean that indicates whether the node is the last node in a word.

语言特里里的每个节点都包含一个单词的一个字母。 您按照特里的分支来拼写一个单词,一次拼一个字母。 当字母的顺序与特里中的其他单词不同或单词结束时,步骤开始分支。 每个节点包含一个字母(数据)和一个布尔值,指示该节点是否是单词中的最后一个节点。

Look at the image and you can form words. Always start at the root node at the top and work down. The trie shown here contains the word ball, bat, doll, do, dork, dorm, send, sense.

查看图像,您可以形成单词。 始终从顶部的根节点开始,然后向下进行。 此处显示的特里包含单词ball,bat,doll,do,dork,dorm,send,sense。

View the code for a trie in JavaScript here.

在此处查看JavaScript的代码。

freeCodeCamp的挑战 (freeCodeCamp challenges)

二进制堆 (Binary Heap)

A binary heap is another type of tree data structure. Every node has at most two children. Also, it is a complete tree. This means that all levels are completely filled until the last level and the last level is filled from left to right.

二进制堆是树数据结构的另一种类型。 每个节点最多有两个孩子。 而且,它是一棵完整的树。 这意味着所有级别都被完全填充,直到最后一个级别,并且最后一个级别从左到右被填充。

A binary heap can be either a min heap or a max heap. In a max heap, the keys of parent nodes are always greater than or equal to those of the children. In a min heap, the keys of parent nodes are less than or equal to those of the children.

二进制堆可以是最小堆,也可以是最大堆。 在最大堆中,父节点的键始终大于或等于子节点的键。 在最小堆中,父节点的密钥小于或等于子节点的密钥。

The order between levels is important but the order of nodes on the same level is not important. In the image, you can see that the third level of the min heap has values 10, 6, and 12. Those numbers are not in order.

级别之间的顺序很重要,但是同一级别上的节点的顺序并不重要。 在该图像中,您可以看到最小堆的第三级具有值10、6和12。这些数字没有顺序。

View the code for a heap in JavaScript here.

在此处查看JavaScript中的堆代码。

二进制堆时间复杂度 (Binary heap time complexity)
AlgorithmAverageWorst Case
Space0(n)0(n)
Search0(1)0(log n)
Insert0(log n)0(log n)
Delete0(1)0(1)
算法 平均 最糟糕的情况
空间 0(n) 0(n)
搜索 0(1) 0(log n)
0(log n) 0(log n)
删除 0(1) 0(1)
freeCodeCamp的挑战 (freeCodeCamp challenges)

图形 (Graph)

Graphs are collections of nodes (also called vertices) and the connections (called edges) between them. Graphs are also known as networks.

图是节点(也称为顶点)及其之间的连接(称为边)的集合。 图也称为网络。

One example of graphs is a social network. The nodes are people and the edges are friendship.

图的一个示例是社交网络。 节点是人,边缘是友谊。

There are two major types of graphs: directed and undirected. Undirected graphs are graphs without any direction on the edges between nodes. Directed graphs, in contrast, are graphs with a direction in its edges.

图有两种主要类型:有向图和无向图。 无向图是节点之间的边缘上没有任何方向的图。 相反,有向图是在其边缘具有方向的图。

Two common ways to represent a graph are an adjacency list and an adjacency matrix.

表示图形的两种常见方法是邻接表和邻接矩阵。

An adjacency list can be represented as a list where the left side is the node and the right side lists all the other nodes it’s connected to.

邻接表可以表示为一个列表,其中左侧为节点,右侧列出其连接到的所有其他节点。

An adjacency matrix is a grid of numbers, where each row or column represents a different node in the graph. At the intersection of a row and a column is a number that indicates the relationship. Zeros mean there is no edge or relationship. Ones mean there is a relationship. Numbers higher than one can be used to show different weights.

邻接矩阵是一个数字网格,其中每一行或每一列代表图中的一个不同节点。 在行和列的交点处是一个数字,指示关系。 零表示不存在边或关系。 有人表示有关系。 大于1的数字可用于显示不同的权重。

Traversal algorithms are algorithms to traverse or visit nodes in a graph. The main types of traversal algorithms are breadth-first search and depth-first search. One of the uses is to determine how close nodes are to a root node. See how to implement breadth-first search in JavaScript in the video below.

遍历算法是遍历或访问图中节点的算法。 遍历算法的主要类型是广度优先搜索和深度优先搜索。 用途之一是确定节点与根节点的距离。 在下面的视频中,了解如何在JavaScript中实现广度优先搜索。

See the code for breadth-first search on an adjacency matrix graph in JavaScript.

有关在JavaScript中对邻接矩阵图进行广度优先搜索的代码,请参见。

二进制搜索时间复杂度 (Binary search time complexity)
AlgorithmTime
StorageO(|V|+|E|)
Add VertexO(1)
Add EdgeO(1)
Remove VertexO(|V|+|E|)
Remove EdgeO(|E|)
QueryO(|V|)
算法 时间
存储 O(| V | + | E |)
添加顶点 O(1)
添加边缘 O(1)
删除顶点 O(| V | + | E |)
移除边缘 O(| E |)
询问 O(| V |)
freeCodeCamp的挑战 (freeCodeCamp challenges)

更多 (More)

The book Grokking Algorithms is the best book on the topic if you are new to data structures/algorithms and don’t have a computer science background. It uses easy-to-understand explanations and fun, hand-drawn illustrations (by the author who is a lead developer at Etsy) to explain some of the data structures featured in this article.

如果您是数据结构/算法的新手并且没有计算机科学背景,那本书《 Grokking Algorithms》是有关该主题的最佳书籍。 它使用易于理解的解释和有趣的手绘插图(作者是Etsy的主要开发人员)来解释本文中介绍的某些数据结构。

Grokking Algorithms: An illustrated guide for programmers and other curious peopleSummary Grokking Algorithms is a fully illustrated, friendly guide that teaches you how to apply common algorithms to…www.amazon.com

Grokking算法:面向程序员和其他好奇者的插图指南 摘要Grokking算法是一本全面插图的友好指南,教您如何将通用算法应用于……

Or you can check out my video course based on that book: Algorithms in Motion from Manning Publications. Get 39% off my course by using code ‘39carnes’!

或者,您可以根据该书查看我的视频课程: Manning Publications的《运动中的算法》 。 使用代码“ 39carnes ”可获得39%的课程折扣

翻译自: https://www.freecodecamp.org/news/10-common-data-structures-explained-with-videos-exercises-aaff6c06fb2b/

数据结构和算法练习网站

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值