Search Algorithms: Linear, Binary and Hash

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

In a linear/sequential search in an unordered linked list, what happens when a match is not found during insertion?

  • The item replaces the closest key in the linked list.
  • The search terminates without any changes to the list.
  • The item is added to the front of the linked list. (correct)
  • The item is added to the end of the linked list.

What is the time complexity of inserting an element into an unordered array using a linear search approach?

  • O(N log N)
  • O(log N)
  • O(1)
  • O(N) (correct)

In a binary search algorithm on a sorted array, what is the role of the 'rank' helper function?

  • To find the key that is closest in value to the target key.
  • To determine the number of keys less than the target key. (correct)
  • To sort the array before searching.
  • To randomly select an index to check for the key.

A binary search is used on a sorted array to find a specific element. After several iterations, the search range is narrowed down to a single element. What does this indicate?

<p>The target element is found or is confirmed to be absent. (C)</p>
Signup and view all the answers

What is a primary disadvantage of using binary search implementation on an ordered array, especially when inserting new elements?

<p>Shifting existing elements to make space for the new element. (B)</p>
Signup and view all the answers

What is the time complexity of Binary Search and Insert operations respectively, in an ordered array?

<p>O(log N) and O(N) (C)</p>
Signup and view all the answers

What is the primary goal of a hash function?

<p>To minimize collisions by evenly distributing keys across the hash table. (B)</p>
Signup and view all the answers

Under what condition can searching in a hash table be reduced from O(n) to O(1)?

<p>When a good hash function minimizes collisions. (B)</p>
Signup and view all the answers

In the context of direct addressing hash tables, what is a key requirement for the number of places, 'm,' in the hash table relative to the number of keys, 'k'?

<p>m must be at least as large as k. (A)</p>
Signup and view all the answers

What is a significant disadvantage of using direct addressing hash tables when dealing with a large universe of possible keys?

<p>They require a large amount of memory, potentially wasting space. (B)</p>
Signup and view all the answers

What is the key problem addressed by hash functions that incorporate collision resolution techniques?

<p>Handling situations where two distinct keys are hashed to the same index. (D)</p>
Signup and view all the answers

Which characteristic is most important when selecting a value for 'm' (the size of the hash table) in the division method of modular hashing?

<p>m should be a prime number. (A)</p>
Signup and view all the answers

What is the primary purpose of separate chaining in hash table implementations?

<p>To store multiple elements at a single hash table index using linked lists. (D)</p>
Signup and view all the answers

In separate chaining, what is the process for searching for a specific key?

<p>Compute the hash and search only the ith chain. (B)</p>
Signup and view all the answers

If a hash table using separate chaining has an array of size 'm' and stores 'k' keys, what is the condition that prompts the array to double in size?

<p>When k/m &gt;= 8 (D)</p>
Signup and view all the answers

Which of the following is a characteristic of linear probing?

<p>It finds the next available slot, and puts it there. (C)</p>
Signup and view all the answers

In quadratic probing, how is the next probe position calculated after a collision at index h(x)?

<p>By trying (i+1)%m, (i+4)%m, (i+9)%m etc. (B)</p>
Signup and view all the answers

What is the primary requirement for the secondary hash function, $h_2(k)$, in double hashing?

<p>It should never evaluate to 0. (B)</p>
Signup and view all the answers

Which of the following is true regarding hashing analysis?

<p>Linear probing typically wastes less space than separate chaining. (B)</p>
Signup and view all the answers

What is the definition of a 'tree' in the context of data structures?

<p>A connected, undirected graph with no cycle. (C)</p>
Signup and view all the answers

Which statement accurately describes the 'depth' of a node in a tree?

<p>The length of the path from the root to the node. (A)</p>
Signup and view all the answers

In a binary search tree (BST), what is the key property regarding the arrangement of nodes?

<p>Each node has a key greater than all keys in its left subtree and smaller than all keys in its right subtree. (A)</p>
Signup and view all the answers

In a Binary Search Tree (BST) what is the first step in searching for an element?

<p>Check the root of search tree. (C)</p>
Signup and view all the answers

In a BST, if the value to be inserted is less than the current node, which of the following operations is performed?

<p>The new node is inserted as the left child of the current node. (D)</p>
Signup and view all the answers

What is the key disadvantage of using a binary search tree (BST) as a data structure?

<p>It can become unbalanced, leading to poor performance. (D)</p>
Signup and view all the answers

What determines the shape of a binary search tree (BST)?

<p>The order in which nodes are inserted. (B)</p>
Signup and view all the answers

What is the number of comparisons that may be performed for search/insert equal in a BST?

<p>equals 1 + depth of the node (C)</p>
Signup and view all the answers

To find the minimum value from the BST, which of the following step is appropriate?

<p>Go Left. (C)</p>
Signup and view all the answers

Which characteristic is used to implement size(), return the count at the root for BST?

<p>Node Count (D)</p>
Signup and view all the answers

Which of the following steps are performed while deleting the minimum key:?

<p>Update subtree counts. Then go left until find a node with a null left link (A)</p>
Signup and view all the answers

In the context of deleting a node from a BST, what action is taken in the 'Case 0' scenario?

<p>Setting parent link to null. (B)</p>
Signup and view all the answers

To delete a node with key $k$ in a BST and it has one child under which of the following cases is it found and what happens?

<p>Case 0 - The node $t$ is replaced by setting parent link to child. (B)</p>
Signup and view all the answers

In what scenario is the 'successor' of a node relevant during deletion in a Binary Search Tree (BST)?

<p>When the node to be deleted has two children. (C)</p>
Signup and view all the answers

Which performance metric improves when balancing the tree?

<p>Reduces height and average case. (C)</p>
Signup and view all the answers

For a 2-3 tree, how many keys are there per node?

<p>1 and 2. (C)</p>
Signup and view all the answers

During 2-3 tree insertion on a 2-node, what action should be performed?

<p>New key to 2-node to create a 3-node. (D)</p>
Signup and view all the answers

While performing 2-3 tree insertion to a 3-node then 4-node will be generated, under what condition such action occurs and it splits into three 2-nodes?

<p>If you reach the root. (A)</p>
Signup and view all the answers

Flashcards

Linear Search in a Linked List

Scan each key in a linked list until a match is found.

Sequential Search Complexity

The time complexity of searching an unordered array using sequential search.

Insert Complexity in Unordered Array

The time complexity of inserting into an unordered array.

Binary Search Data Structure

Requires maintaining an ordered array of key-value pairs.

Signup and view all the flashcards

Rank Helper Function

Determines the index if a key is present, or insertion point if not.

Signup and view all the flashcards

Binary Search Time Complexity

O(log N)

Signup and view all the flashcards

Insert Time Complexity for Ordered Array

O(N)

Signup and view all the flashcards

Hash Table Objective

A data structure for dynamic sets with simple operations.

Signup and view all the flashcards

Hash Table

A generalized field structure for quick data retrieval.

Signup and view all the flashcards

Direct Addressing

Maps each key directly to an index.

Signup and view all the flashcards

Collision

Hashing multiple keys to same location

Signup and view all the flashcards

Division Method Hash Function

h(k) = k mod m

Signup and view all the flashcards

Hashing Integer

Integer i between 0 to m-1

Signup and view all the flashcards

Separate Chaining

An array of linked lists.

Signup and view all the flashcards

Linear Probing

A method that finds the next empty slot upon collision.

Signup and view all the flashcards

Probing Hash Function

h(x) = x % m

Signup and view all the flashcards

Collision Resolution

A method where new key collides

Signup and view all the flashcards

Type of Probe Sequences

Quadratic probing

Signup and view all the flashcards

Quadratic Probing hash function

Hash function: Maps key to between 0 and m-1

Signup and view all the flashcards

Double Hashing Algorithm

Doubling helps minimize clustering

Signup and view all the flashcards

Best and Average Case

Divide the array into pieces

Signup and view all the flashcards

Separate chaining

Performance degrades completely

Signup and view all the flashcards

Definition of a Tree

Connected undirected graph.

Signup and view all the flashcards

Root

A node that opens into edges

Signup and view all the flashcards

Forest

n >=0 disjoint trees

Signup and view all the flashcards

Degree Deg (e)

Sub-trees

Signup and view all the flashcards

Leaf Node

Node with no descendants

Signup and view all the flashcards

Depth D (e)

Length of path from to Node

Signup and view all the flashcards

H (e)

Length of longest path

Signup and view all the flashcards

Binary Search Trees

Maintaing key and value

Signup and view all the flashcards

Search

Search if less, greater or equal

Signup and view all the flashcards

Binary Search Tree

Go left, if less etc

Signup and view all the flashcards

BST Search –Implem

If N is 1 then depth

Signup and view all the flashcards

Put Associate

To find key, consider two cases.

Signup and view all the flashcards

Shapes depend

Must correspond insert depth nodes

Signup and view all the flashcards

BST Table.

Min

Signup and view all the flashcards

Nodes data

Root is in charge.

Signup and view all the flashcards

Double hash.

Signup and view all the flashcards

Rotation

Used to maintain black tree.

Signup and view all the flashcards

A BST SUCH

Colored.

Signup and view all the flashcards

Study Notes

  • Covers search algorithms, including linear search, binary search, and hash tables
  • Touches on 2-3 trees and Left-Leaning Red-Black Trees (LLRBT) as balanced search tree options

Search Applications

  • Various applications use specific search types
  • Dictionaries use search to find definitions by word
  • Book indexes use search to identify relevant pages by term
  • File sharing uses search to locate songs by name
  • Financial accounts systems use search to process transactions by account number
  • Web search engines use search to find web pages by keywords
  • Involves maintaining an unordered linked list of key-value pairs
  • Requires scanning through all keys until a match is located
  • For insertion, if a match is found keys will be scanned, otherwise the new node is added to the front
  • The complexity of sequential search in an unordered array is O(N)
  • The complexity of inserting into an unordered array is O(N)
  • This search algorithm maintains an ordered array of key-value pairs
  • Utilizes a 'rank' helper function to determine the number of keys less than k
  • Determines the index position of a key if it exists in the array, and if the key is present in the array or the appropriate insertion point if it doesn't
  • Binary search has a search complexity of O(log N) in an ordered array
  • Insertion complexity in an ordered array is O(N), considering the need to shift elements
  • Inserting a key requires shifting all greater keys over

Hash Tables

  • An efficient data structure for dynamic sets, supporting simple operations like insert/put, search/get, and delete
  • Generalized field structure suitable for applications like Google search and caching
  • In the worst-case scenario with linked lists, searching can be O(n)

Direct Addressing Hash Table

  • Best used with a relatively small set of keys
  • Given a hash table H[0,..., m-1] with m places, m >= k
  • Hash Function: h(k) → k maps key k to index k
  • Advantage: offers simple implementation of insert, search, and delete
  • Disadvantage: impractical for a large number of keys, as the actual number of keys observed k, may be very small compared to the table size m, wasting space
  • Solutions of this include using a hash function with collision resolution, separate chaining, and probing

Hash Function with Collision

  • Restricts the size of the hash table by mapping a set of possible keys to a smaller set m
  • Problem with Collisions: Two distinct keys being hashed onto the same hash code
  • E.g., the Mod7 function makes any integer key to the range [0 … 6], Mod7(15)=1, Mod7(22)=1

Design of Good Hash Functions

  • Objective is to achieve a good distribution of the search key k to the index range of the hash table H, without underlying assumptions about a probability distribution

Division Method / Modular Hashing

  • The hash function h is defined as h(k) = k mod m, where m is the size of the hash table H
  • For instance, if m = 12 and k = 100, then h(100) = 4
  • Good candidates for m are prime numbers

Separate Chaining

  • Also known as open hashing or closed addressing
  • Uses an array of size m < k linked lists
  • Hash: Maps a key to an integer i between 0 to m-1
  • Insert: Puts the new node at the front of the ith chain if it's not already there
  • Search: Needs to search only the ith chain

Separate Chaining - Resizing

  • The goal is to keep the average length of the list k/m constant
  • Double the size of array M when k/m >= 8
  • Halve the size of array M when k/m <= 2
  • Need to rehash all keys when resizing

Further Topics in Search Algorithm

  • Terminologies of tree structures such as 'forests', 'degree of node', 'leaf node', 'depth of node', 'height of node'
  • BST
  • Balanced search tree
  • 2-3 tree and related transformations and properties
  • Red-black trees and "glue" nodes

Note that there is no material related to deletion of hash table entries or separate chaining entries in this study guide. Probing, quadratic and double hashing, and complexity analysis are also missing.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser