Data Structures
Data Structures
What will be the BFS for the above graph? (Start from 0 and use
natural order of labels to select neighbors to visit)
a. 0, 1, 2, 3, 4, 5, 6, 7, 8
✅
b. 0, 1, 2, 3, 4, 7, 8, 6, 5
c. 0, 1, 3, 6, 5, 8, 7, 2, 4
d. None of the above
29. Consider the following matrix for a Weighted Directed Graph
Function isParenthesisMatching(input_string):
// Create an empty stack to store opening parentheses
Stack stack
a set of operations that can be performed on that data. It defines the behavior of
a data structure, including how data is stored and how operations are performed
on it, without specifying the implementation details. In other words, an ADT
abstracts the data and its associated operations, providing a clear interface for
interacting with the data while hiding the underlying implementation.
Stack: An abstract data type that represents a last-in, first-out (LIFO) data
structure with operations like push and pop.
Queue: An abstract data type that represents a first-in, first-out (FIFO) data
structure with operations like enqueue and dequeue.
List: An abstract data type that represents a linear collection of elements
with operations for adding, removing, and accessing elements.
Set: An abstract data type that represents a collection of unique elements
with operations for adding, removing, and checking for membership.
Dictionary or Map: An abstract data type that stores key-value pairs and
allows operations for inserting, retrieving, and deleting values associated
with keys.
Graph: An abstract data type for modeling graphs with vertices and edges,
often with various graph traversal and manipulation operations.
It's important to note that while ADTs define the behavior and interface of
a data structure, the actual implementation may vary.
Different programming languages and libraries may provide different
implementations of the same ADT, but as long as they conform to the
specified interface and behavior, they can be used interchangeably. This
separation of the abstract interface from the concrete implementation is a
fundamental concept in computer science and software engineering,
promoting modularity, code reuse, and maintainability.
same location (i.e., they produce the same hash value) in the hash table.
Collisions are common in practice, and addressing them is essential to ensure
that hash tables function correctly. There are several common collision
resolution techniques:
4. How do AVL trees work, and what are the principles and
balancing criteria that ensure that an AVL tree maintains
its balanced and self-balancing properties during
insertions and deletions?
Ans. AVL trees, named after their inventors Adelson-Velsky and Landis, are a type of
self-balancing binary search tree. They are designed to maintain a balanced
structure, which ensures efficient O(log n) time complexity for common
operations such as searching, insertion, and deletion.
The key principles and balancing criteria that ensure an AVL tree's balance
during insertions and deletions are as follows:
Balancing Condition: In an AVL tree, for every node, the heights of its left and
right subtrees (the balance factor) can differ by at most 1. This condition
ensures that the tree remains approximately balanced.
Balancing Operations:
When an element is inserted into an AVL tree, the tree may become
unbalanced due to the insertion. To restore balance, one or more rotations
are performed on affected nodes.
1. Left-rotation
2. Right-rotation
3. Left-right rotation
4. Right-left rotation.
Double Rotation: In some cases, a single rotation may not be sufficient to restore
balance. For example, after a left child of a node becomes taller than its right
child, a right rotation may be applied to the parent node. If this, in turn, makes the
left child of the parent node taller than its right child, a left rotation is applied to
the grandparent node. This is called a double rotation and is used to restore
balance.
Deletion Balancing: After deleting a node from an AVL tree, the same balancing
criteria are applied. Deletion may also lead to imbalance, and the same rotation
techniques are used to maintain balance.
The self-balancing property of AVL trees ensures that the height of the tree
remains logarithmic, which, in turn, guarantees efficient search, insertion, and
deletion operations. The balancing criteria are rigorously enforced during tree
modification to maintain this balance, making AVL trees a popular choice for
data structures where fast and reliable access is essential.
We want to insert the value 4 into this tree. Here's how the insertion process
works:
First, insert the element 12 into the AVL tree as you would in a
regular binary search tree, following the order of the tree:
2. Update Heights: After inserting the element, update the heights of the
nodes from the newly inserted node up to the root:
3. Check Balance and Perform Rotations: Check the balance factor of each
node along the path from the inserted node up to the root. In this case, we
need rebalancing because starting from Node 10 the balancing factors
are:
Node 10 needs a RIGHT ROTATION (new node is added to the left subtree
of left subtree of unbalanced node or node 10)
The insertion process in an AVL tree ensures that the tree maintains its balanced
structure, even after inserting new elements. Rotations are used to correct any
imbalance that may arise during the insertion, and the AVL property is
consistently enforced, ensuring efficient operations in the tree.