Hash table (three) - Why hash table and linked lists are often used together?

Foreword

Although the hash table support efficient data insert, delete, and search operations, but through which the data are erratic after the hash function disrupted. That is, it can not be quickly traversed in a certain order. If you want to order through the data in the hash table, it would need to copy data to the array, and then sort again traversal.

A hash table is a dynamic data structure, insert keep data, deleting, when we want to in order to traverse the hash table, we need to sort this would be very inefficient. To solve this problem, it will be the hash table and linked list (or skip lists) in combination.

Common usage scenarios:

  • LRU cache elimination algorithm can be implemented with linked lists and hash tables;
  • Redis ordered collection uses jump tables and hash tables;
  • Java is also used LinkedHashMap hash table and list.

1. LRU cache replacement algorithms

In fact, a cache (cache) system mainly includes the following several operations:

  • Adding to a data cache;
  • To delete a data from the cache;
  • Find a data cache.

The above three operations involving a search operation, if simply linked list, find the time complexity is O (n). If the hash table and the linked list, the time complexity becomes O (1).

Doubly linked list used to store data, each node in the linked list in addition to storing data (Data), the precursor pointer (PREV), subsequent addition pointer (Next), also added a special field hnext.

prev|data|next|hnext

Each node will be the two chains. A chain is a doubly linked list, hash table is another chain zipper. Predecessor and successor node pointers to the string doubly linked list, hnext pointer to the string node in the hash table fastener.

The whole process involved in the search operation can be accomplished by a hash table. Other operations, such as deleting the first node, the tail of the linked list data is inserted, it can be completed within a time complexity of O (1) is. Therefore, the time complexity of these three operations is O (1).

2. Redis ordered set

In fact, in the ordered set, each member object has two important attributes, key (key) and score (score). Not only to find data by score, but also to find data by key.

If you look at Redis refined ordered set of operations, and that is this:

  • Add a member object;
  • According to a member of the key to delete the object;
  • According to a member of the key to find the object;
  • Lookup value in accordance with section data, such as finding the integral [100, 356] between the members of the object;
  • According to the score from small to large member variables;

If only the score according to the member objects organized into a jump table structure, then in accordance with the key to delete, query member objects will be very slow. Then you can then follow the key to build a hash table, in accordance with the key to delete, find the time complexity of a member object becomes O (1).

3. Java LinkedHashMap

LinkedHashMap is achieved through a doubly linked list and hash table data structure combination of the two. LinkedHashMap the "Linked" actually refers to a doubly linked list, does not mean to solve the conflict with the hash chain method.

In accordance with the access time is itself a sort of LinkedHashMap support LRU cache strategy out of the cache system. In fact, two of them to achieve the principle is exactly the same.

Think

Several examples of the use of linked lists and hash tables combined with today's talk, are used in a doubly linked list. If doubly linked list into a single list, it also can work properly? why?
In fact, still able to work. However, insertion and deletion of time, need to find precursors pointer, the time complexity of O (n).

It is engaged in the development of the Android engineers for seven years, a lot of people ask me privately, 2019 Android how advanced the science, there is no method?

Yes, at the beginning I spent more than a month to sort out learning materials, hoping to help those who want to enhance the advanced Android development, but do not know how advanced learning friends. [ Including advanced UI, performance optimization, Architect courses, NDK, Kotlin, hybrid development (ReactNative + Weex), Flutter and other technical information architecture ], hoping to help review your pre-interview and find a good job, but also save in time they search for information online to learn.

Obtaining: Add Android architecture exchange QQ group chat: 513 088 520, into the group that is to receive the information! ! !

Click on the link to join a group chat Android mobile architecture [total population]: join a group chat

Sourcebook

Guess you like

Origin blog.csdn.net/weixin_43351655/article/details/90947299