Skip to content

added hash table search algorith #1381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/Avl-Tree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Hash-Table-Search.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
196 changes: 196 additions & 0 deletions dsa-solutions/Searching-Algorithms/05-AVL-Tree-Search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
---
id: AVL-Tree-Search
title: AVL Tree Search (Geeks for Geeks)
sidebar_label: AVL Tree Search
tags:
- Beginner
- Search Algorithms
- Geeks for Geeks
- CPP
- Python
- Java
- JavaScript
- DSA
description: "This is a solution to the AVL Tree Search problem on Geeks for Geeks."
---

## What is AVL Tree Search?

AVL Tree Search is a search operation performed on an AVL tree, a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by no more than one, ensuring O(log N) time complexity for search operations.

## Algorithm for AVL Tree Search

1. Start at the root node of the AVL tree.
2. Compare the target value with the value of the current node:
- If the target value equals the current node's value, return the node.
- If the target value is less than the current node's value, move to the left child.
- If the target value is greater than the current node's value, move to the right child.
3. Repeat step 2 until the target value is found or the current node becomes null.
4. If the target value is not found, return null.

## How does AVL Tree Search work?

- It begins by comparing the target value to the value of the root node.
- If the target value matches the root node's value, the search is complete.
- If the target value is less than the root node's value, the search continues in the left subtree.
- If the target value is greater than the root node's value, the search continues in the right subtree.
- This process continues until the target value is found or a leaf node is reached without finding the target value.

![Example for AVL Tree Search(GFG)](../../assets/Avl-Tree.png)

## Problem Description

Given an AVL tree and a target element, implement the AVL Tree Search algorithm to find the node containing the target value in the tree. If the element is not present, return null.

## Examples

**Example 1:**
```
Input:
AVL Tree:
9
/ \
5 12
/ \ \
2 7 15

Target: 7
Output: Node with value 7

```

## Your Task:

You don't need to read input or print anything. Complete the function avlTreeSearch() which takes the root of the AVL tree and a target value as input parameters and returns the node containing the target value. If the target value is not present in the tree, return null.

Expected Time Complexity: $O(LogN)$
Expected Auxiliary Space: $O(1)$

## Constraints

- $1 <= Number of nodes <= 10^5$
- $1 <= Node value <= 10^6$
- $1 <= Target value <= 10^6$

## Implementation

<Tabs>

<TabItem value="C++" label="C++">
<SolutionAuthor name="@ngmuraqrdd"/>
```cpp
#include <iostream>

struct AVLNode {
int value;
AVLNode* left;
AVLNode* right;
AVLNode(int val) : value(val), left(nullptr), right(nullptr) {}
};

AVLNode* avlTreeSearch(AVLNode* root, int target) {
AVLNode* current = root;
while (current) {
if (current->value == target) {
return current;
} else if (current->value < target) {
current = current->right;
} else {
current = current->left;
}
}
return nullptr;
}

int main() {
AVLNode* root = new AVLNode(9);
root->left = new AVLNode(5);
root->right = new AVLNode(12);
root->left->left = new AVLNode(2);
root->left->right = new AVLNode(7);
root->right->right = new AVLNode(15);

int target = 7;
AVLNode* result = avlTreeSearch(root, target);
if (result) {
std::cout << "Node with value " << result->value << " found." << std::endl;
} else {
std::cout << "Node not found." << std::endl;
}

return 0;
}

</TabItem>

<TabItem value="Java" label="Java">
<SolutionAuthor name="@ngmuraqrdd"/>
```java
class AVLNode {
int value;
AVLNode left, right;
AVLNode(int value) {
this.value = value;
left = right = null;
}
}

public class AVLTreeSearch {
public static AVLNode avlTreeSearch(AVLNode root, int target) {
AVLNode current = root;
while (current != null) {
if (current.value == target) {
return current;
} else if (current.value < target) {
current = current.right;
} else {
current = current.left;
}
}
return null;
}

public static void main(String[] args) {
AVLNode root = new AVLNode(9);
root.left = new AVLNode(5);
root.right = new AVLNode(12);
root.left.left = new AVLNode(2);
root.left.right = new AVLNode(7);
root.right.right = new AVLNode(15);

int target = 7;
AVLNode result = avlTreeSearch(root, target);
if (result != null) {
System.out.println("Node with value " + result.value + " found.");
} else {
System.out.println("Node not found.");
}
}
}

</TabItem>
```

</Tabs>

## Complexity Analysis

- **Time Complexity**:$O(log n)$, where $n$ is the number of nodes in the AVL tree. The height of the tree is kept balanced, leading to logarithmic time complexity.
- **Space Complexity**: $O(1)$, as no extra space is required apart from the input tree.

## Advantages and Disadvantages

**Advantages:**
- Ensures balanced tree structure for efficient search, insert, and delete operations.
- Fast search time due to logarithmic time complexity.

**Disadvantages:**
- Requires additional rotations to maintain balance during insert and delete operations.
- More complex to implement compared to simple binary search trees.


## References

- **GFG Problem:** [GFG Problem](https://www.geeksforgeeks.org/practice-questions-height-balancedavl-tree//)
- **Author's Geeks for Geeks Profile:** [MuraliDharan](https://www.geeksforgeeks.org/user/ngmuraqrdd/)

159 changes: 159 additions & 0 deletions dsa-solutions/Searching-Algorithms/06-Hash-Table-Search
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
---
id: Hash-Table-Search
title: Hash Table Search (Geeks for Geeks)
sidebar_label: Hash Table Search
tags:
- Beginner
- Search Algorithms
- Geeks for Geeks
- CPP
- Python
- Java
- JavaScript
- DSA
description: "This is a solution to the Hash Table Search problem on Geeks for Geeks."
---

## What is Hash Table Search?

Hash Table Search is a search operation performed on a hash table, a data structure that stores key-value pairs. It allows for efficient insertion, deletion, and search operations in average O(1) time complexity.

## Algorithm for Hash Table Search

1. Compute the hash code of the target key using the hash function.
2.Use the hash code to find the index of the target key in the hash table.
3. If the key is found at the computed index, return the associated value.
4. If the key is not found at the computed index, return null.

## How does Hash Table Search work?

- It begins by computing the hash code of the target key.
- The hash code is then used to determine the index in the hash table where the key-value pair should be stored or searched.
- If the key exists at the computed index, the corresponding value is returned.
- If the key does not exist at the computed index, the search returns null.



![Example for AVL Tree Search(GFG)](../../assets/Hash-Table-Search.png)

## Problem Description

Given a hash table and a target key, implement the Hash Table Search algorithm to find the value associated with the target key in the table. If the key is not present, return null.

## Examples

**Example 1:**
```
Input:
Hash Table: {1: 'one', 2: 'two', 3: 'three'}
Target Key: 2
Output: 'two'

```

**Example 2:**
```
Input:
Hash Table: {1: 'one', 2: 'two', 3: 'three'}
Target Key: 4
Output: null
```

## Your Task:

You don't need to read input or print anything. Complete the function hashTableSearch() which takes a hash table and a target key as input parameters and returns the value associated with the target key. If the target key is not present, return null.

Expected Time Complexity: $O(1)$
Expected Auxiliary Space: $O(1)$

## Constraints

- $1 <= Number of key-value pairs <= 10^5$
- Keys are integers and unique.
- Values are strings.

## Implementation

<Tabs>

<TabItem value="Java label="Java">
<SolutionAuthor name="@ngmuraqrdd"/>
```java

import java.util.HashMap;

public class HashTableSearch {
public static String hashTableSearch(HashMap<Integer, String> hashTable, int target) {
return hashTable.getOrDefault(target, null);
}

public static void main(String[] args) {
HashMap<Integer, String> hashTable = new HashMap<>();
hashTable.put(1, "one");
hashTable.put(2, "two");
hashTable.put(3, "three");

int target = 2;
System.out.println(hashTableSearch(hashTable, target)); // Output: 'two'

target = 4;
System.out.println(hashTableSearch(hashTable, target)); // Output: null
}
}

</TabItem>

<TabItem value="C++" label="C++">
<SolutionAuthor name="@ngmuraqrdd"/>
```cpp
#include <iostream>
#include <unordered_map>

std::string hashTableSearch(const std::unordered_map<int, std::string>& hashTable, int target) {
auto it = hashTable.find(target);
if (it != hashTable.end()) {
return it->second;
}
return "null";
}

int main() {
std::unordered_map<int, std::string> hashTable = {{1, "one"}, {2, "two"}, {3, "three"}};

int target = 2;
std::cout << hashTableSearch(hashTable, target) << std::endl; // Output: 'two'

target = 4;
std::cout << hashTableSearch(hashTable, target) << std::endl; // Output: null

return 0;
}


</TabItem>
```

</Tabs>

## Complexity Analysis

- **Time Complexity**:$O(1)$, as hash table operations (insertion, deletion, and search) typically have constant time complexity.

- **Space Complexity**:$O(1)$, as no extra space is required apart from the input hash table.

## Advantages and Disadvantages

**Advantages:**
- Provides efficient search, insertion, and deletion operations.
- Useful for applications requiring fast lookups and quick data retrieval.

**Disadvantages:**
- May require more memory compared to other data structures due to hashing and potential collisions.
- Performance can degrade if the hash function leads to many collisions.
References

## References

- **GFG Problem:** [GFG Problem](https://www.geeksforgeeks.org/hash-table-data-structure/)
- **Author's Geeks for Geeks Profile:** [MuraliDharan](https://www.geeksforgeeks.org/user/ngmuraqrdd/)

Loading