0% found this document useful (0 votes)
24 views

DSA Final

Uploaded by

Maha Jawd
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

DSA Final

Uploaded by

Maha Jawd
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 5

What is dfs and bfs?

DFS stands for Depth-First Search. It is graph traversal algorithms used


to explore or search through a graph. DFS starts at a given node and
explores as far as possible along each branch before backtracking. It
uses stack to keep track of the nodes to visit.
Working: In DFS, the algorithm starts at a given node and explores as
far as possible along each branch before backtracking. This means that
it goes deep into the graph, exploring all the possible paths before
moving on to the next branch. DFS is often used when searching for a
specific element in a graph, as it can efficiently traverse through the
graph and find the desired element.

BFS stands for Breadth-First Search. It is both graph traversal


algorithms used to explore or search through a graph. BFS, explores all
the neighboring nodes of the current node before moving on to the
next level. It uses a queue to keep track of the nodes to visit.
When it comes to searching using BFS explores all the neighboring
nodes of the current nodebefore moving on to the next level. This
means that it explores the graph level by level, starting from the initial
node and moving outward. BFS is commonly used when searching for
the shortest path or when you want to visit all the nodes in a graph in a
systematic manner.

What is linear search and binary search?

Linear search is a simple search algorithm that sequentially checks each


element ina list or array until it finds the target value. It starts
from the beginning and compares each element one by one until a
match is found or the end ofthe list is reached. Linear search is
straightforward but can be time-consuming for large lists.
Binary search, is a more efficient search algorithm but requires the list
to be sorted in ascending or descending order. It works by repeatedly
dividing the search interval in half and comparing the middle element
with the target value. Based on the comparison, the search interval is
further narrowed down until the target value is found or determined to
be absent.

In summary, linear search is a simple but potentially time consuming


search algorithm that checks each element sequentially, while binary
search is a more efficient algorithm that relies on the sorted order ofthe
list to quickly narrow down the search interval.

What are trees and its types?


A tree is a hierarchical structure composed of nodes connected by
edges. Each node can have zero or more child nodes, except for the
root node, which is the topmost node in the tree.

Types of tress:
Binary Tree: A binary tree is a tree in which each node has at most two
children, referred to as the left child and the right child. It's like
a family tree, where each person has at most
two children.
AVL Tree: An AVL tree is a self-balancing binary search tree. It
maintains a balance factor for each node, which represents the
difference in height between its left and right subtrees. AVL trees
automatically adjust their structure to ensure that the balance factor of
every node stays within a specific range, ensuring efficient operations.
Red-Black Tree: A red-black tree is another
type of self-balancing binary search tree. It maintains balance by
coloring the nodes either red or black and following specific rules to
ensure balance. Red-black trees are commonly used in many
applications, including map and set data structures.

What are operations on trees?


There are severalcommon operations that can be performed on trees in
data structures and algorithms.
1. Insertion: Adding a new node to the tree. The insertion operation
depends on the type of tree being used and follows specific rules to
maintain the tree's properties, such as maintaining the order in a binary
search tree or balancing the tree in a self-balancing tree.
2. Deletion: Removing a node from the tree. Similar to insertion, the
deletion operationvaries depending on the type of tree and
follows specific rules to maintain the tree'sproperties.
3. Searching: Finding a specific node in the tree. Searching can be done
using various algorithms, such as depth-first search (DFS) or breadth-
first search (BFS), depending on the specific requirements.
4. Traversals: Visiting and processing all the nodes in the tree in a
specific order. Common tree traversal algorithms include in-order, pre-
order, and post-order traversals. These traversals are used to perform
operations on each node, such as printing the node's value or
performing calculations.
5. Height/Depth calculation: Determining the height or depth of a tree,
which represents the number of edges from the root node to the
deepest leaf node.
6. Tree balancing: For self-balancing trees like AVL trees or red-black
trees, there are operations to ensure the tree remains
balanced after insertions or deletions. These operations involve
rotations and restructuring of the tree. These are some of the basic
operations on trees. Depending on the specific use case or problem,
there may be additional operations or variations of these operations.

What is collision and hashing?


Hashing is a technique used to map data to a fixed-size array called a
hash table. This mapping is done using a hash function, which takes the
input data and produces a unique hash value. The hash value serves as
the index where the data is stored in the hash table.

Now, collision occurs when two different inputs produce the same hash
value. It's like two different keys trying to occupy the same slot in the
hash table. This can happen due to the limited size ofthe hash table or
the nature of the hash function.

To handle collisions, there are different


strategies:
1. Separate Chaining: In this approach, each slot in the hash table
contains a linked list. When a collision occurs, the new data is simply
added to the linked list atthat slot.
2. Open Addressing: Here, whena collision occurs, the algorithm
searches for the next available slot in the hash table to store the data.
This can be done using different techniques like linear probing
(checking the next slot), quadratic probing (checking slots
with quadratic increments), or double hashing (using a second hash
function to find the next slot).
The goal is to minimize collisions and ensure efficient retrieval of data
from the hash table. A good hash function and an appropriate collision
resolution strategy are crucial for the performance of hashing.

What are collision resolution techniques?


Collision resolution techniques are used to handle collisions in data
structures and algorithms. When two different inputs produce the
same hash value, a collision occurs. There are a few common collision
resolution techniques:
1. Separate Chaining: In this technique, each slot in the hash table
contains a linked list. When a collision occurs, the new data is simply
added to the linked list at that slot.
2. Open Addressing: This technique involves finding the next available
slot in the hash table where collision occurs. There are different
methods for open addressing, such as linear probing, quadratic probing,
and double hashing. - Linear Probing: If a slot is already occupied,
the algorithm checks the next slot until an empty slot is found.
- Quadratic Probing: The algorithm checks slots with quadratic
increments until an empty slot is found.
- Double Hashing: A second hash function is used to calculate the
number of slots to skip when searching for an empty slot. These
techniques help ensure that data is stored and retrieved efficiently in
the hash table, minimizing collisions. The choice of collision resolution
technique depends on factors like the size of the hash table and the
expected number of collisions.

You might also like