简体   繁体   English

在java中跳过列表

[英]Skip list in java

I was going through data structures in java under the topic Skip list and I came across the following: 我在主题Skip list中浏览了java中的数据结构,我发现了以下内容:

In a skip list of n nodes , for each k and i such that 1 ≤ k ≤lg n and 1 ≤ i ≤ n/2k–1⎦ – 1 , the node in position 2k–1 · i points to the node in position 2k–1 · ( i + 1 ). n nodes的跳过列表中,对于每个ki ,使得1 ≤ k ≤lg n1 ≤ k ≤lg n 1 ≤ i ≤ n/2k–1⎦ – 1 ,位置2k–1 · i中的节点指向位置中的节点2k–1 ·( i + 1 )。 This means that every second node points to the node two positions ahead, every fourth node points to the node four positions ahead, and so on, as shown in Figure 3.17a. 这意味着每个第二个节点指向前面两个位置的节点,每四个节点指向前面四个位置的节点,依此类推,如图3.17a所示。 This is accomplished by having different numbers of reference fields in nodes on the list: Half of the nodes have just one reference field, one-fourth of the nodes have two reference fields, one-eighth of the nodes have three reference fields, and so on. 这是通过在列表中的节点中具有不同数量的参考字段来实现的:一半节点仅具有一个参考字段,四分之一的节点具有两个参考字段,八分之一的节点具有三个参考字段,因此上。 The number of reference fields indicates the level of each node, and the number of levels is maxLevel = ⎣lg n⎦ + 1 . 参考字段的数量表示每个节点的级别,级别的数量是maxLevel = ⎣lg n⎦ + 1

And the figure is : A skip list with (a) evenly and (b) unevenly spaced nodes of different levels; 并且该图是:跳过列表,其中(a)均匀地和(b)不同级别的不均匀节点; (c) the skip list with reference nodes clearly shown. (c)清楚显示参考节点的跳过列表。

在此输入图像描述

I don't understand the mathematical part and what exactly the sktip list is and even nodes? 我不明白数学部分和sktip列表究竟是什么甚至是节点?

Ok let me try to make you understand this. 好吧,让我试着让你明白这一点。

A skip list is a data-structure which definitely makes your searches faster in a list of given elements. 跳过列表是一种数据结构,它肯定会使您在给定元素列表中的搜索速度更快。

A better analogy would be a network of subway in any of the bigger cities. 更好的比喻是任何一个大城市的地铁网络。 Imagine there are 90 stations to cover and there are different lines (Green, Yellow and Blue). 想象一下,有90个站点可以覆盖,并且有不同的线路(绿色,黄色和蓝色)。

The Green line only connects the stations numbered 0, 30, 60 and 90 The Yellow line connects 0, 10, 20, 30, 40, 50, 60, 70, 80 and 90 The blue line connects all the station from 0 through 90. 绿线仅连接编号为0,30,60和90的站点。黄线连接0,10,20,30,40,50,60,70,80和90蓝线连接所有站点,从0到90。

If you want to board the train at station 0 and want to get down at 75. What is the best strategy? 如果你想在0号站登上火车,想要在75号站下来。最好的策略是什么?

Common sense would suggest to board a train on Green line from station 0 and get down at station 60. Board another train on Yellow line from station 60 and get down at station 70. Board another train on Blue line from station 70 and get down at 75. 常识会建议从0号车站搭乘绿线上的火车,然后在60号站下车。从60号车站乘坐黄线上的另一列火车,然后在70号站下车。从70号站乘坐另一列蓝线火车到达75。

Any other way would have been more time consuming. 任何其他方式都会耗费更多时间。

Now replace the stations with the nodes and lines with three individual lists (the set of these lists are called skip list). 现在用具有三个单独列表的节点和行替换站(这些列表的集合称为跳过列表)。

And just imaging that you wanted to search an element at a node containing the value 75. 只是想要在包含值75的节点上搜索元素的映像。

I hope this explains what Skip Lists are and how they are efficient. 我希望这能解释Skip Lists的内容以及它们的效率。

In the traditional approach of searching, you could have visited each node and got to 75 in 75 hops. 在传统的搜索方法中,您可以访问每个节点并在75跳中达到75。 In case of binary search you would have done it in logN In skip list you can do the same in 1 + 1 + 15 in our particular case. 在二进制搜索的情况下,你可以在logN中完成它。在跳过列表中你可以在我的特定情况下在1 + 1 + 15中做同样的事情。 You can do the math, seems to be simple though :) 你可以做数学,虽然似乎很简单:)

EDIT: Evenly spaced nodes & Unevenly spaced nodes As you can see my analogy, it has equal number of stations between each node on each line. 编辑:均匀间隔的节点和不均匀间隔的节点正如您可以看到我的类比,它在每条线上的每个节点之间具有相同数量的站。 This is evenly spaced nodes. 这是均匀间隔的节点。 It is an ideal situation. 这是一个理想的情况。

To understand it better we need to understand the creation of Skip Lists. 为了更好地理解它,我们需要了解Skip Lists的创建。

In the early stages of its construction there is only one list (the blue line) and each new node is first added to the list at an appropriate location. 在其构建的早期阶段,只有一个列表(蓝线),并且每个新节点首先在适当的位置添加到列表中。 When the number of nodes in the blue line increases then there comes a need to create another list (yellow line) and promote one of the nodes to list 2. (PS: The first and the last element of list 1 is always promoted to the newly added list in the skip lists set). 当蓝线中的节点数量增加时,需要创建另一个列表(黄线)并将其中一个节点提升为列表2.(PS:列表1的第一个和最后一个元素始终提升为跳过列表集中新添加的列表)。 Hence, the moment a new list is added it will have three nodes. 因此,添加新列表的那一刻将有三个节点。

Promotion Strategy : How to find out which node to promote from the bottom most list(blue line) to the upper lists (yellow line and green line). 促销策略 :如何从最下面的列表(蓝线)到上面的列表(黄线和绿线)找出要推广的节点。

The best way to decide is randomly :) So lets say upon addition of a new node, we flip a coin to see if it can be promoted to the second list. 决定的最好方法是随机:)所以我们假设添加一个新节点后,我们翻转硬币以查看是否可以将其提升到第二个列表。 if yes, then we add it to the second list and then flip a coin again to check if it has to be added in the third list or not. 如果是,那么我们将它添加到第二个列表然后再次翻转硬币以检查是否必须在第三个列表中添加它。

So you see, if you use this random mechanism, there might arrive situations where the nodes are unevenly spaced. 所以你看,如果使用这种随机机制,可能会出现节点间距不均匀的情况。 :) :)

Hope this helps. 希望这可以帮助。

蓝色,黄色,绿色火车图和蓝色火车从0到60,黄色火车从60到70,最后蓝色火车从70到71,72,73,74和75的路线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM