How Algorithms Work
Chapter 4 Searching Algorithms

Chapter 4: Searching Algorithms

Searching is a fundamental operation in computer science that involves finding a particular item or set of items within a collection of data. Efficient search algorithms and data structures are crucial for many applications, from databases and file systems to information retrieval and computational geometry. In this chapter, we explore several important searching algorithms and data structures, including binary search trees, balanced search trees, and hash tables. We also discuss various applications of searching in real-world scenarios.

Symbol Tables and Data Structures

A symbol table is an abstract data type that associates keys with values, providing operations for inserting key-value pairs, searching for a value given its key, and deleting key-value pairs. Symbol tables are also known as dictionaries or associative arrays in different programming languages. They are fundamental data structures used in a wide range of applications, such as:

  • Compilers, where symbol tables are used to store information about variables, functions, and other identifiers.
  • Databases, where indices are built using symbol tables to enable fast searching and retrieval of records.
  • Network routers, which use symbol tables to store routing information for efficient packet forwarding.

There are several data structures that can be used to implement symbol tables, each with its own trade-offs in terms of search, insertion, and deletion performance. In this section, we focus on two important data structures: binary search trees and hash tables.

Binary Search Trees (BSTs)

A binary search tree (BST) is a hierarchical data structure that stores key-value pairs in a way that enables efficient search, insertion, and deletion operations. Each node in a BST contains a key, an associated value, and references to its left and right child nodes. The key in each node is greater than all keys in its left subtree and less than all keys in its right subtree. This property, known as the BST invariant, allows for efficient searching by making binary decisions at each node.

Here's an example of a simple BST:

    /   \
   2     6
  / \   / \
 1   3 5   7

Searching in a BST involves comparing the target key with the key at the current node and recursively searching in the left or right subtree based on the comparison. If the target key is found, the associated value is returned. If the target key is not found after reaching a null reference, the search terminates unsuccessfully.

Insertion in a BST follows a similar process to searching. We compare the key of the new node with the keys in the BST and traverse down the tree until we find a null reference, where we attach the new node as a leaf. Deletion in a BST is slightly more complex, as it requires handling three cases: deleting a leaf node, deleting a node with one child, and deleting a node with two children.

The average-case time complexity for search, insertion, and deletion in a BST is O(log n), where n is the number of nodes in the tree. However, in the worst case (e.g., when the BST degenerates into a linked list), the time complexity becomes O(n). To mitigate this issue, we can use self-balancing BSTs, such as AVL trees or red-black trees, which maintain a nearly balanced tree structure and guarantee O(log n) worst-case performance for all operations.

Hash Tables

A hash table is a data structure that provides fast average-case search, insertion, and deletion by using a hash function to map keys to indices in an array, called buckets. The hash function takes a key as input and returns an integer index, which is used to locate the corresponding bucket in the array. Ideally, the hash function should distribute keys uniformly across the buckets to minimize collisions (i.e., multiple keys mapping to the same bucket).

When a collision occurs, there are two main approaches to resolve it:

  1. Separate chaining: Each bucket is implemented as a linked list, and all key-value pairs that hash to the same bucket are stored in that bucket's linked list.

  2. Open addressing: When a collision occurs, the hash table probes other buckets in a predefined sequence until an empty bucket is found. Common probing techniques include linear probing, quadratic probing, and double hashing.

Here's an example of a hash table with separate chaining:

+---+    +-------+
| 0 |--->| (1,A) |
+---+    +-------+
| 1 |--->| (5,B) |---->| (9,C) |
+---+    +-------+     +-------+
| 2 |
| 3 |--->| (7,D) |
+---+    +-------+
| 4 |

In this example, keys 1, 5, and 9 all hash to bucket 1, so they are stored in a linked list attached to that bucket. Key 7 hashes to bucket 3, and it is the only key-value pair in that bucket.

The average-case time complexity for search, insertion, and deletion in a well-designed hash table is O(1), making it one of the fastest data structures for these operations. However, the worst-case time complexity can degrade to O(n) if the hash function is poorly chosen or if there are many collisions. To maintain good performance, it is essential to use a high-quality hash function and to resize the hash table when the load factor (i.e., the ratio of the number of key-value pairs to the number of buckets) exceeds a certain threshold, typically around 0.75.

Balanced Search Trees

While binary search trees provide efficient search, insertion, and deletion operations on average, their performance can degrade significantly in the worst case. Balanced search trees are a family of data structures that maintain a nearly balanced tree structure, ensuring good worst-case performance. In this section, we discuss two popular balanced search trees: AVL trees and red-black trees.

AVL Trees

An AVL tree, named after its inventors Adelson-Velsky and Landis, is a self-balancing binary search tree in which the heights of the left and right subtrees of any node differ by at most one. This height difference is called the balance factor. When an insertion or deletion operation violates the AVL property (i.e., the balance factor becomes greater than 1 or less than -1), the tree is rebalanced through one or more rotations.

There are four types of rotations used to rebalance AVL trees:

  1. Left rotation: Performed when the balance factor of a node is greater than 1 and its right child's balance factor is non-negative.

  2. Right rotation: Performed when the balance factor of a node is less than -1 and its left child's balance factor is non-positive.

  3. Left-right rotation: Performed when the balance factor of a node is greater than 1 and its right child's balance factor is negative.

  4. Right-left rotation: Performed when the balance factor of a node is less than -1 and its left child's balance factor is positive.

By maintaining the AVL property, AVL trees guarantee a worst-case time complexity of O(log n) for search, insertion, and deletion operations. However, the constant factors involved in AVL tree operations are slightly higher compared to ordinary BSTs due to the overhead of maintaining balance factors and performing rotations.

Red-Black Trees

A red-black tree is another self-balancing binary search tree that maintains a nearly balanced structure. Each node in a red-black tree is colored either red or black, and the tree satisfies the following properties:

  1. The root node is always black.
  2. All leaf nodes (NIL) are black.
  3. If a node is red, both its children are black.
  4. Every path from a node to any of its descendant leaf nodes contains the same number of black nodes.

These properties ensure that the longest path from the root to any leaf is no more than twice the length of the shortest path, guaranteeing a worst-case time complexity of O(log n) for search, insertion, and deletion operations.

When an insertion or deletion operation violates any of the red-black tree properties, the tree is rebalanced through a series of color flips and rotations. The rebalancing process in red-black trees is generally more efficient than in AVL trees, as it requires fewer rotations on average. This makes red-black trees a popular choice for implementing balanced search trees in practice, such as in the C++ Standard Template Library (STL) and the Java Collections Framework.

Applications of Searching

Searching algorithms and data structures have numerous applications across various domains. In this section, we discuss a few examples to illustrate the importance and versatility of searching in real-world scenarios.

Databases and Information Retrieval

Databases and information retrieval systems heavily rely on efficient searching techniques to provide fast access to data. In relational databases, indices are built using data structures like B-trees or hash tables to enable quick lookups of records based on specific attributes. These indices allow databases to efficiently execute queries with conditions on indexed attributes, greatly reducing the search space and improving query performance.

In information retrieval systems, such as web search engines, inverted indices are used to map terms to the documents containing them. An inverted index is essentially a symbol table where the keys are terms, and the values are lists of document identifiers. When a user submits a query, the search engine looks up the query terms in the inverted index and retrieves the corresponding document lists, which are then combined and ranked to produce the final search results.

Compiler Design

Compilers use symbol tables extensively to keep track of identifiers (e.g., variable names, function names) and their attributes (e.g., data type, scope) during the compilation process. When the compiler encounters an identifier in the source code, it searches the symbol table to determine its meaning and properties. Efficient searching is crucial for compiler performance, as a typical compiler may need to process millions of identifiers in a large codebase.

Bioinformatics and Computational Biology

In bioinformatics and computational biology, searching algorithms play a vital role in analyzing and understanding biological data. Some examples include:

  • Sequence alignment: Algorithms like BLAST (Basic Local Alignment Search Tool) and Smith-Waterman are used to search for similarities between DNA, RNA, or protein sequences. These algorithms employ various searching techniques to efficiently find the best matches between sequences, helping researchers identify evolutionary relationships, functional similarities, and potential mutations.

  • Genome assembly: Searching algorithms are used to locate overlaps between short DNA fragments (reads) generated by sequencing machines, allowing the reconstruction of the original genome sequence. Efficient searching is essential for handling the massive amounts of data generated in modern sequencing projects.

  • Gene and motif finding: Researchers use searching algorithms to locate specific patterns or motifs within DNA or protein sequences, such as transcription factor binding sites, splice sites, or conserved domains. These patterns can provide insights into gene regulation, protein function, and evolutionary conservation.

Cybersecurity and Cryptography

Searching algorithms are employed in various aspects of cybersecurity and cryptography, including:

  • Intrusion detection: Network intrusion detection systems often use searching algorithms like Aho-Corasick or Boyer-Moore to efficiently match network traffic patterns against a database of known attack signatures. This allows for real-time detection and prevention of security threats.

  • Password cracking: Attackers may use searching algorithms to efficiently search through large password dictionaries or generate potential password combinations when attempting to crack password hashes. Techniques like rainbow tables and time-memory trade-offs rely on efficient searching to speed up the password recovery process.

  • Cryptanalysis: In cryptography, searching algorithms are used to analyze and potentially exploit weaknesses in cryptographic systems. For example, algorithms like baby-step giant-step or Pollard's rho are used to solve the discrete logarithm problem, which underlies the security of certain cryptographic schemes.


Searching algorithms and data structures are fundamental tools in computer science, with applications spanning a wide range of domains. From databases and information retrieval to scientific computing, bioinformatics, and cybersecurity, the ability to efficiently search for and locate data is crucial for solving complex problems and extracting valuable insights.

By understanding the principles and techniques behind searching algorithms, such as binary search trees, balanced search trees, and hash tables, developers can make informed decisions when designing and implementing systems that rely on efficient searching capabilities. The choice of the appropriate searching algorithm and data structure depends on factors such as the size and nature of the data, the frequency of search operations, and the specific requirements of the application.

As the amount of data generated and processed continues to grow exponentially, the importance of efficient searching algorithms will only increase. Researchers and practitioners in various fields will continue to rely on these fundamental tools to tackle the challenges posed by big data and to unlock new opportunities for discovery and innovation.