# Search data structure

In computer science, a **search data structure** is any data structure that allows the efficient retrieval of specific items from a set of items, such as a specific record from a database.

The simplest, most general, and least efficient search structure is merely an unordered sequential list of all the items. Locating the desired item in such a list, by the linear search method, inevitably requires a number of operations proportional to the number *n* of items, in the worst case as well as in the average case. Useful search data structures allow faster retrieval; however, they are limited to queries of some specific kind. Moreover, since the cost of building such structures is at least proportional to *n*, they only pay off if several queries are to be performed on the same database (or on a database that changes little between queries).

**Static** search structures are designed for answering many queries on a fixed database; **dynamic** structures also allow insertion, deletion, or modification of items between successive queries. In the dynamic case, one must also consider the cost of fixing the search structure to account for the changes in the database.

## Classification

The simplest kind of query is to locate a record that has a specific field (the *key*) equal to a specified value *v*. Other common kinds of query are "find the item with smallest (or largest) key value", "find the item with largest key value not exceeding *v*", "find all items with key values between specified bounds *v*_{min} and *v*_{max}".

In certain databases the key values may be points in some multi-dimensional space. For example, the key may be a geographic position (latitude and longitude) on the Earth. In that case, common kinds of queries are *find the record with a key closest to a given point *v*", or "find all items whose key lies at a given distance from *v*", or "find all items within a specified region *R* of the space".*

A common special case of the latter are simultaneous range queries on two or more simple keys, such as "find all employee records with salary between 50,000 and 100,000 and hired between 1995 and 2007".

### Single ordered keys

- Array if the key values span a moderately compact interval.
- Priority-sorted list; see linear search
- Key-sorted array; see binary search
- Self-balancing binary search tree
- Hash table

### Finding the smallest element

## Asymptotic amortized worst-case analysis

In this table, the asymptotic notation *O*(*f*(*n*)) means "not exceeding some fixed multiple of *f*(*n*) in the worst case."

Data Structure | Insert | Delete | Balance | Get at index | Search | Find minimum | Find maximum | Space usage |
---|---|---|---|---|---|---|---|---|

Unsorted array | O(1)^{(see note)} |
O(1)^{(see note)} |
N/A | O(1) |
O(n) |
O(n) |
O(n) |
O(n) |

Sorted array | O(n) |
O(n) |
N/A | O(1) |
O(log n) |
O(1) |
O(1) |
O(n) |

Stack | ||||||||

Queue | ||||||||

Unsorted linked list | O(1) |
O(1)[1] |
N/A | O(n) |
O(n) |
O(n) |
O(n) |
O(n) |

Sorted linked list | O(n) |
O(1)[1] |
N/A | O(n) |
O(n) |
O(1) |
O(1) |
O(n) |

Skip list | ||||||||

Self-balancing binary search tree | O(log n) |
O(log n) |
O(log n) |
N/A | O(log n) |
O(log n) |
O(log n) |
O(n) |

Heap | O(log n) |
O(log n) |
O(log n) |
N/A | O(n) |
O(1) for a min-heapO(n) for a max-heap[2] |
O(1) for a max-heapO(n) for a min-heap[2] |
O(n) |

Hash table | O(1) |
O(1) |
O(n) |
N/A | O(1) |
O(n) |
O(n) |
O(n) |

Trie (k = average length of key) |
O(k) |
O(k) |
N/A | O(k) |
O(k) |
O(k) |
O(k) |
O(k n) |

Cartesian tree | ||||||||

B-tree | O(log n) |
O(log n) |
O(log n) |
N/A | O(log n) |
O(log n) |
O(log n) |
O(n) |

Red-black tree | ||||||||

Splay tree | ||||||||

AVL tree | O(log n) |
|||||||

k-d tree |

*Note*: Insert on an unsorted array is sometimes quoted as being *O*(*n*) due to the assumption that the element to be inserted must be inserted at one particular location of the array, which would require shifting all the subsequent elements by one position. However, in a classic array, the array is used to store arbitrary unsorted elements, and hence the exact position of any given element is of no consequence, and insert is carried out by increasing the array size by 1 and storing the element at the end of the array, which is a *O*(1) operation.[3][4] Likewise, the deletion operation is sometimes quoted as being *O*(*n*) due to the assumption that subsequent elements must be shifted, but in a classic unsorted array the order is unimportant (though elements are implicitly ordered by insert-time), so deletion can be carried out by swapping the element to be deleted with the last element in the array and then decrementing the array size by 1, which is a *O*(1) operation.[5]

This table is only an approximate summary; for each data structure there are special situations and variants that may lead to different costs. Also two or more data structures can be combined to obtain lower costs.

## Footnotes

- Cormen, Leiserson, Rivest.
*Introduction to Algorithms*. The College of Information Sciences and Technology at Penn State. ISBN 9780262530910.LIST-DELETE runs in

CS1 maint: uses authors parameter (link)*O*(1) time, but if to delete an element with a given key, Θ(n) time is required in the worst case because we must first call LIST-SEARCH. - Cormen, Leiserson, Rivest.
*Introduction to Algorithms*. The College of Information Sciences and Technology at Penn State. ISBN 9780262530910.There are two kinds of binary heaps: max-heaps and min-heaps. In both kinds, the values in the nodes satisfy a

CS1 maint: uses authors parameter (link)**heap property**... the largest element in a max-heap is stored at the root... The smallest element in a min-heap is at the root... The operation HEAP-MAXIMUM returns the maximum heap element in Θ(1) time by simply returning the value*A*[1] in the heap. - Allen Sherrod (2007).
*Data Structures and Algorithms for Game Developers*. Cengage Learning. ISBN 9781584506638.The insertion of an item into an unordered array does not depend on anything other than placing the new item at the end of the list. This gives the insertion into an unordered array of

*O*(1). - Cormen, Leiserson, Rivest.
*Introduction to Algorithms*. The College of Information Sciences and Technology at Penn State. ISBN 9780262530910.CS1 maint: uses authors parameter (link) - "Algorithm - the time complexity of deletion in a unsorted array".
Finding the element with a given value is linear. Since the array isn't sorted anyway, you can do the deletion itself in constant time. First swap the element you want to delete to the end of the array, then reduce the array size by one element.