In JavaScript programming, data can be stored in data structures like graphs and trees. Technically trees are graphs.
在JavaScript编程中,数据可以存储在图形和树之类的数据结构中。 从技术上讲,树是图。
图形数据结构 (Graph Data Structures)
Graphs evolved from the field of mathematics. They are primarily used to describe a model that shows the route from one location to another location.
图是从数学领域演变而来的。 它们主要用于描述显示从一个位置到另一位置的路线的模型。
A graph consists of a set of nodes and a set of edges. An edge is a pair of nodes that are connected. A path is the term used to describe traveling between nodes that share an edge. The image below shows a graph with 3 nodes and 3 edges.
图由一组节点和一组边组成。 边缘是一对连接的节点。 路径是用于描述共享一条边的节点之间传播的术语。 下图显示了具有3个节点和3个边的图形。
树数据结构 (Tree Data Structure)
A tree data structure, like a graph, is a collection of nodes. There is a root node. The node can then have children nodes. The children nodes can have their own children nodes called grandchildren nodes.
树数据结构(如图形)是节点的集合。 有一个根节点。 该节点然后可以具有子节点。 子节点可以有自己的子节点,称为子节点。
This repeats until all data is represented in the tree data structure. The image below shows a tree data structure.
重复此操作,直到所有数据都在树数据结构中表示出来为止。 下图显示了树数据结构。
A tree is a graph that has no cycles (a cycle being a path in the graph that starts and ends at the same vertex). A child node can only have one parent. For this reason trees are not a recursive data structure.
树是没有循环的图形(循环是图形中从同一顶点开始和结束的路径)。 子节点只能有一个父节点。 因此,树不是递归数据结构。
为什么将图和树用作数据结构? (Why Use Graphs and Trees as Data Structures?)
In computer programming, trees are used all the time to define data structures. They are also used as the basis for algorithms to solve problems.
在计算机编程中,一直使用树来定义数据结构。 它们还用作解决问题的算法的基础。
The most common implementations of a graph are finding a path between two nodes, finding the shortest path from one node to another and finding the shortest path that visits all nodes.
图的最常见实现是找到两个节点之间的路径,找到从一个节点到另一个节点的最短路径,并找到访问所有节点的最短路径。
The traveling salesman problem is a great example of using a tree algorithm to solve a problem.
旅行商问题是使用树算法解决问题的一个很好的例子。
搜索数据 (Searching Data)
Now that you understand the difference between the two data structures, I am going to show you how you can search through your data.
既然您了解了两种数据结构之间的区别,那么我将向您展示如何搜索数据。
The two most common methods of searching a graph or a tree are depth first search and breadth first search.
搜索图形或树的两种最常见的方法是深度优先搜索和宽度优先搜索。
Whether to use a depth first search or a breadth first search should be determined by the type of data that is contained in your tree or graph data structure.
是否使用深度优先搜索或宽度优先搜索应由树或图数据结构中包含的数据类型确定。
广度优先搜索 (Breadth First Search)
Here is an example of a tree that we want to search using a breadth first search.
这是我们要使用广度优先搜索来搜索树的示例。
In a breadth first search you will start at the root node. You will then search all their children nodes moving from left to right. Once all the children nodes have been searched, the process is repeated on the level below the root node.
在广度优先搜索中,您将从根节点开始。 然后,您将搜索从左到右移动的所有子节点。 搜索完所有子节点后,将在根节点下方的级别上重复此过程。
This process is repeated on each level until you reach the end of the tree or you reach the node that you were searching for initially. The image below shows you the order that you will search a tree in a breadth first search.
在每个级别上都重复此过程,直到到达树的末尾或到达最初要搜索的节点为止。 下图显示了在广度优先搜索中搜索树的顺序。
To implement a breadth first search you need some way of keeping track of what nodes you need to search next once you complete searching on the current level.
要实现广度优先搜索,您需要某种方式来跟踪在当前级别完成搜索之后接下来需要搜索哪些节点。
To keep track of the nodes that need to be searched next you will use a queue as an intermediary step in the search. A queue is a FIFO (first in first out) array.
为了跟踪接下来需要搜索的节点,您将使用队列作为搜索的中间步骤。 队列是一个FIFO(先进先出)阵列。
To demonstrate how this works let me walk you through doing the search of Level 1 and Level 2 in the image above.
为了演示其工作原理,让我引导您完成上图中的1级和2级搜索。
The first node to be searched is the root node or Node A. You would put Node A as the first element in your queue. You will then repeat these steps until your queue is empty.
要搜索的第一个节点是根节点或节点A。您应将节点A作为队列中的第一个元素。 然后,您将重复这些步骤,直到队列为空。
- Take the first node out of the queue and see if it matches your search item. 将第一个节点从队列中移出,看看它是否与您的搜索项匹配。
- Add all of the node’s children to the temporary queue. 将节点的所有子节点添加到临时队列中。
After step 2 of your search your queue queue will now hold all of the children of Node A.
搜索的第2步之后,您的队列队列现在将包含节点A的所有子节点。
We now compare Node B to see if it matches our search results. If it does not then it is removed from the queue leaving only node H. We then add in the children of Node B into the queue.
现在我们比较节点B,看它是否与我们的搜索结果匹配。 如果没有,则将其从队列中删除,仅留下节点H。然后,将节点B的子级添加到队列中。
This process continues until all nodes have been searched or your find the node that matches your search criteria.
该过程将一直持续到搜索完所有节点或找到符合搜索条件的节点为止。
更多文章 (More Articles)
Thanks for reading my article. If you like it, please click on clap icon below so that others will find the article. Here are some more of my articles that you might be interested in:
感谢您阅读我的文章。 如果喜欢,请单击下面的拍手图标,以便其他人可以找到该文章。 以下是您可能感兴趣的其他一些文章:
Instantiation Patterns in JavaScriptWhy Company Culture is Important to Your Career as a Software EngineerUsing Node.js & Express.js to save data to MongoDB Database
JavaScript中的实例化模式 为什么公司文化对您作为软件工程师的职业而言很重要 使用Node.js和Express.js将数据保存到MongoDB数据库
翻译自: https://www.freecodecamp.org/news/breadth-first-search-in-javascript-e655cd824fa4/