Data Structure: block linked list

Source: Internet
Author: User
Transferred to: http://www.wypblog.com/archives/166
I. Overview

Sometimes we need to design a data structure that can quickly insert or delete a piece of data at a required location. Consider two simple data structures: arrays and linked lists. ArrayAdvantage:The location of the operation to be executed can be found in O (1) time, but itsDisadvantage:Whether it is to insert or delete all the data to be moved, the complexity is O (n. Linked ListAdvantage:You can insert or delete a piece of data within O (1),Disadvantage:When searching for the operation location, we need to traverse the entire linked list. The complexity is the same as that of O (n. The two data structures have their own advantages and disadvantages. We can combine the advantages of arrays and linked lists to form a new data structure:Block linked listCombined with the advantages and disadvantages of arrays and linked lists, the time complexity of various operations of a block linked list is O (SQRT (n )).

Ii. Operations on block linked lists

A block linked list combines the advantages and disadvantages of arrays and linked lists. A block linked list itself is a linked list. However, a linked list stores not general data, but an ordered table consisting of these data. Each node in a block linked list, that is, a sequence table, can be called a block. Block linked lists use variable sequence table lengths and special insertion and deletion methods to achieve complexity. Another feature of a block chain table is that it saves memory compared to a common chain table because it does not need to save pointers pointing to each data node.

1. Positioning

In fact, the positioning operation can be regarded as a search. Of course, we first need to locate the node where the element is located, and then find the node we want in the array of the node;

2. Split nodes

Splits a node into two nodes;

3. insert operations

First, locate the location to be inserted, split the node into two nodes, and put the data at the end of the first node. If you want to insert a large block of data, you must first cut the data into multiple blocks, each of which corresponds to a node in a block linked list, and link these blocks, then insert the two nodes. The inserted operation diagram is similar to the following:


4. delete operations

First, locate the location of the deleted element, and then delete the data according to the method of deleting the element in the array. To delete a large part of data, you must first locate the positions of the first and last elements of the data block, and then split them into two nodes, delete the node between the first element and the last element.


Iii. Key Technologies

In general, maintain a linked list. Each unit in the linked list contains an array and the number of data in the array. The data in each linked list is the whole data. Set the chain table length to a, and the array length in each unit to B. Whether it is insert or delete, the entire linked list must be traversed during addressing. The complexity is O (a). For insert operations, a new unit is directly added to the linked list, and the complexity is O (1 ); the delete operation involves multiple consecutive units. If all data in a unit is to be deleted, delete the Unit directly. The complexity is O (1). If only part of the data is deleted, to move the data in the array, the complexity is O (B ). The total complexity is O (a + B ). Because AB = n and a = B = √ n, the total complexity is O (√ n ).

The question is how to maintain that a and B are roughly equal to √ n?

After the insert operation, if the number of data in the current unit is greater than 2 √ n, the current Unit is divided into two new units, and the number of data in each unit is kept at 2 √ n. After the delete operation is performed, if the number of data in the current Unit and the next unit of the current Unit are equal to <√ n, the two units are merged into a new unit. To perform the above maintenance operations, we need to move the data in the array. The complexity is O (B). Division and merging of units are O (1), and the total complexity is O (B. In this way, maintenance operations do not increase the overall complexity. Finally, an O (√ n) data structure is obtained.

Iv. Application

1. Text Editor: http://download.noi.cn/T/noi/noi2003A.pdf

Another approach: http://www.byvoid.com/blog/noi-2003-editor/

2. Maintenance series: http://download.noi.cn/T/noi/noi2005A.pdf

Another approach: http://www.byvoid.com/blog/noi-2005-sequence/

Reprinted Please note: Reprinted from past memories (http://www.wypblog.com /)
Link: Data Structure: block linked list (http://www.wypblog.com/archives/166)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.