The beauty of data structures and algorithms (skip table)

1. What is a skip table

Create a multi-level index for a value-ordered linked list, such as extracting a node for every two nodes to the previous level, and we call the extracted level an index or an index layer. As shown in the figure below, where down represents the down pointer, which points to the next level node. By analogy, for a linked list with n nodes, a log2n-1 level index can be established. A data structure like this that builds a multi-level index for a linked list is called a skip list.
insert image description here

Second, the time complexity of the skip table

1. Calculate the height of the jump table

Let's first find the index height of the skip table. As shown in the figure below, assuming that every two nodes will extract a node as the node of the upper-level index, the original linked list has n elements, then the first- level index has n/2 elements , and the second -level index has n/2 elements. 4 elements , k-level index has n/2k elements . The highest-level index generally has 2 elements , namely: the highest-level index h satisfies 2 = n/2h, that is, h = log2n - 1, the highest-level index h is the height of the index layer plus the original data layer, and the total height of the skip table h = log2n .
insert image description here

2. Calculate the time complexity of the skip table

Assuming that when we query a certain data in the skip table, if each layer traverses m nodes, then the time complexity of querying a data in the skip table is O(m*logn) . How much is this m? As shown in the figure below, assuming that the data we are looking for is x, in the k-th index, after we traverse to the y node, we find that x is greater than y and less than the following node z, so we pass the down pointer of y, from the k-th node level down to the k-1th level index. In the k-1 level index, there are only 3 nodes (including y and z) between y and z, so we only need to traverse at most 3 nodes in the k-1 level index, and so on, each level Indexes only need to traverse at most 3 nodes. So m=3. Therefore, the time complexity of querying a certain data in the skip table is O(logn) .

insert image description here

Third, the space complexity of the skip table

The skip table improves the efficiency of finding elements by establishing an index, which is a typical idea of ​​" space for time ", so some sacrifices are made in space.

1. Calculate the total number of nodes for the index

If the linked list has n nodes, and every 2 nodes extracts a node as the node of the previous index, the number of nodes in each index is: n/2, n/4, n/8, ..., 8, 4 , 2, and the proportional sequence sum n-1, so the space complexity of the skip table is O(n) .

2. How to optimize the time complexity

As shown in the following figure: If every three nodes are drawn as an index, the total number of indexes is n/3 + n/9 + n/27 + … + 9 + 3 + 1= n/2 , which is reduced by half . Therefore, we can reduce the space complexity by reducing the number of indexes , but the corresponding search efficiency will definitely decrease. We can control this threshold according to our application scenario, depending on whether we pay more attention to time or space.

insert image description here

Fourth, efficient dynamic insertion of data, delete data

1) Insert data

The skip list is essentially a linked list, so the time complexity of inserting, inserting and deleting operations is O(1). The operation is time-consuming, but the time complexity of this lookup operation in the skip table is O(logn), so the time complexity of the insertion and deletion operations of the skip table is also O(logn) .
insert image description here
insert image description here

2) Delete data

When deleting data from the skip table, the corresponding node in the index should also be deleted. As shown in the figure below, if you want to delete element 9, you need to delete both the 9 in the original linked list and the 9 in the first-level index.
insert image description here

The process of deleting an element is similar to the process of finding an element, except that if the element x to be deleted is found on the searched path, the delete operation is performed. In the skip list, each level of index is actually an ordered singly linked list. The time complexity of deleting elements in the singly linked list is O(1). The number of index levels is logn, which means that at most logn elements need to be deleted, so the total number of deleted elements is The time includes the time to find an element plus the time to delete logn elements is O(logn) + O(logn) = 2 O(logn), ignoring the constant part, the time complexity of deleting an element is O(logn) .

5. Dynamic update of skip table index?

When inserting data into the skip table, you can choose to insert this data into part of the index layer at the same time, so how to choose this index layer?
insert image description here

The random function can be used to determine which level of indexes to insert this node into. For example, if the random function generates a value K, then this node can be added to the first to Kth level indexes.

Guess you like

Origin blog.csdn.net/qq_54729417/article/details/122884793