Notes
Notes
Unit 4
A binary tree is a hierarchical data structure in which each node has at most two children, referred to
as the left child and the right child.
The height of a binary tree is the number of edges on the longest path from the root to a leaf node.
The height of an empty tree is -1.
Q. 4. What is the difference between binary tree and binary search tree (BST)?
• Binary Tree: A general tree structure where nodes have at most two children.
• Binary Search Tree: A binary tree where:
o The left child contains values less than the parent node.
o The right child contains values greater than the parent node.
Traversal methods:
scss
Copy code
void inorder(Node* root) {
if (root != NULL) {
inorder(root->left);
print(root->data);
inorder(root->right);
}
}
scss
Copy code
void preorder(Node* root) {
if (root != NULL) {
print(root->data);
preorder(root->left);
preorder(root->right);
}
}
scss
Copy code
void postorder(Node* root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
print(root->data);
}
}
What if the input to binary search tree comes in a sorted (ascending or descending) manner? It will
then look like this −
It is observed that BST's worst-case performance is closest to linear search algorithms, that is Ο(n).
In real-time data, we cannot predict data pattern and their frequencies. So, a need arises to balance
out the existing BST.
Named after their inventor Adelson, Velski & Landis, AVL trees are height balancing binary
search tree. AVL tree checks the height of the left and the right sub-trees and assures that the
difference is not more than 1. This difference is called the Balance Factor.
Here we see that the first tree is balanced and the next two trees are not balanced −
In the second tree, the left subtree of C has height 2 and the right subtree has height 0, so the
difference is 2. In the third tree, the right subtree of A has height 2 and the left is missing, so it is 0,
and the difference is 2 again. AVL tree permits difference (balance factor) to be only 1.
BalanceFactor = height(left-sutree) − height(right-sutree)
If the difference in the height of left and right sub-trees is more than 1, the tree is balanced using
some rotation techniques.
• Left rotation
• Right rotation
• Left-Right rotation
• Right-Left rotation
The first two rotations are single rotations and the next two rotations are double rotations. To have
an unbalanced tree, we at least need a tree of height 2. With this simple tree, let's understand them
one by one.
Left Rotation
If a tree becomes unbalanced, when a node is inserted into the right subtree of the right subtree, then
we perform a single left rotation −
In our example, node A has become unbalanced as a node is inserted in the right subtree of A's right
subtree. We perform the left rotation by making A the left-subtree of B.
Right Rotation
AVL tree may become unbalanced, if a node is inserted in the left subtree of the left subtree. The
tree then needs a right rotation.
As depicted, the unbalanced node becomes the right child of its left child by performing a right
rotation.
Left-Right Rotation
Double rotations are slightly complex version of already explained versions of rotations. To
understand them better, we should take note of each action performed while rotation. Let's first
check how to perform Left-Right rotation. A left-right rotation is a combination of left rotation
followed by right rotation.
State Action
A node has been inserted into the right subtree of the left
subtree. This makes C an unbalanced node. These scenarios
cause AVL tree to perform left-right rotation.
Right-Left Rotation
The second type of double rotation is Right-Left Rotation. It is a combination of right rotation
followed by left rotation.
State Action
A node has been inserted into the left subtree of the right subtree.
This makes A, an unbalanced node with balance factor 2.
Algorithm:
• All traversals (Inorder, Preorder, Postorder, Level-order) have a time complexity of O(n)
because each node is visited exactly once.
Q. What are some real-world applications of tree traversal?
Q. What is the difference between DFS and BFS in binary tree traversal?
Graphs can represent various relationships, such as roads connecting cities, social networks, or
dependencies in tasks.
1. Adjacency Matrix:
o A 2D matrix where matrix[i][j] represents the presence (1) or absence (0) of an
edge between vertices i and j.
o Space Complexity: O(V2), where V is the number of vertices.
2. Adjacency List:
o Each vertex has a list of adjacent vertices.
o Space Complexity: O(V+E), where EEE is the number of edges.
3. Edge List:
o A list of all edges, e.g., [(A, B), (B, C)].
Q. What is a weighted graph?
A weighted graph is a graph where edges have weights, representing the cost or distance between
vertices.
MST (Minimum Spanning Tree) algorithms find the spanning tree with the minimum total edge
weight:
1. Prim's Algorithm:
o Greedy algorithm; starts with a single vertex and adds edges with the smallest weight.
2. Kruskal's Algorithm:
o Greedy algorithm; sorts edges by weight and adds edges without forming a cycle.
python
Copy code
def bfs(graph, start):
visited = set()
queue = [start]
visited.add(start)
while queue:
vertex = queue.pop(0)
print(vertex, end=' ')
for neighbor in graph[vertex]:
if neighbor not in visited:
visited.add(neighbor)
queue.append(neighbor)
python
Copy code
def dfs(graph, node, visited):
if node not in visited:
print(node, end=' ')
visited.add(node)
for neighbor in graph[node]:
dfs(graph, neighbor, visited)
1. Initialization:
o Start with any vertex (let’s choose A as the starting vertex).
o Mark it as part of the MST.
o Add all its edges to a priority queue or sorted list based on weights.
2. Step-by-step Execution:
o Pick the smallest edge that connects a vertex in the MST to a vertex outside it.
o Add the corresponding edge and vertex to the MST.
o Repeat until all vertices are included in the MST.
Table of Progression
Step Current MST Edges Added Edge (Weight) Vertices in MST Edges Considered (Min)
1 - A → B (2) A, B A → B (2), A → D (6)
2 A→B B → C (3) A, B, C B → C (3), B → E (8)
3 A → B, B → C C → E (5) A, B, C, E C → E (5), E → D (7)
4 A → B, B → C, C → E A → D (6) A, B, C, E, D -
Result
• A → B (2)
• B → C (3)
• C → E (5)
• A → D (6)
Unit 5
Q. What is Hashing ?
Hashing is a technique or process of mapping keys, values into the hash table by using a hash
function. It is done for faster access to elements. The efficiency of mapping depends on the efficiency
of the hash function used. Let a hash function H(x) maps the value.
A hash function is a mathematical function that takes an input (or "key") and converts it into a fixed-
size string of characters, typically a sequence of numbers or letters, which represents the input in a
more compact and standardized form. The output of a hash function is called the hash value, hash
code, or simply hash.
1. Data Structures:
o Used in hash tables for efficient data retrieval, insertion, and deletion.
o Example: The key-value pairs in a dictionary or hashmap rely on hash functions to
quickly locate data.
2. Data Integrity:
o Hash functions verify the integrity of data by comparing the hash values before and
after transmission.
o Example: File checksum validation (e.g., MD5, SHA-256).
3. Cryptography:
o Used in secure password storage, digital signatures, and message authentication codes
(e.g., SHA-256, SHA-3).
o Properties like collision resistance make cryptographic hash functions secure.
4. Database Indexing:
o Hash functions are used for indexing data efficiently in large databases.
5. Load Balancing:
o Hash functions distribute tasks or requests across servers evenly in distributed
systems.
6. Distributed Systems:
o Used in consistent hashing for distributed data storage and retrieval (e.g., in systems
like Cassandra and DynamoDB).
7. Image and Video Processing:
o Hashing can identify duplicate files by comparing their hash values.
1. Collisions: When two inputs produce the same hash value, it’s called a collision. Collision-
handling techniques include chaining, open addressing, and double hashing.
2. Security: Cryptographic hash functions must resist vulnerabilities like pre-image attacks and
collision attacks.
3. Uniformity: A good hash function ensures that hash values are evenly distributed to avoid
clustering in hash tables.
Hash functions are foundational in computer science, used across a wide range of applications to
ensure efficient, secure, and reliable operations.
A collision occurs when more than one value to be hashed by a particular hash function hash to the
same slot in the table or data structure (hash table) being generated by the hash function.