MCS-208 - Google Docs
MCS-208 - Google Docs
MCS-208 - Google Docs
B-trees Overview:
B-treeis a self-balancing tree data structure thatmaintains sorted data and allows for
A
searches, sequential access, insertions, and deletions in logarithmic time. It is particularly used
in databases and file systems where reading/writing from disk is costly and needs optimization.
B-trees minimize the number of disk reads required to access data by reducing the height of the
tree.
Characteristics of B-trees:
● B alanced: All leaf nodes are at the same level, meaningthe tree grows in a balanced
way.
● Multi-way nodes: Unlike binary trees (2 nodes), B-treescan have multiple child nodes.
mcan have at most
A B-tree of order m-1keys and
mchildren.
● E O(log n)time, where
fficiency: Searching, inserting, and deleting alltake nis the
number of nodes in the tree.
● Internal Nodes as Pointers: Internal nodes are usedas pointers to subtrees, allowing
faster access to a wider range of data.
m
Properties of a B-tree of order:
.
1 ach node can contain at most
E m-1keys and
mchildren.
2. All leaf nodes appear on the same level.
3. ceil(m/2)children(except the root).
Each internal node has at least
4. Keys in nodes are sorted.
Consider inserting the following keys into a B-tree of order 3: 10, 20, 5, 6, 12, 30, 7, 17.
1. S tart with an empty tree. Insert 10, then 20, andthen 5. Once we insert 6, the node
overflows (more than 2 keys in the node of order 3), and we split the node.
2. The process continues until all keys are inserted. Here's the resulting B-tree:
[10, 20]
/
| \
[5, 6, 7] [12] [30]
Here, each internal node has at most 2 keys, and the leaves contain the sorted data.
Advantages of B-trees:
● E fficient Disk Access: B-trees are optimized for systemsthat read/write large blocks of
data, making them ideal for databases.
● Logarithmic Time Complexity: Operations like search,insertion, and deletion take
O(log n)time.
2: Explain the process of converting a Tree into a Binary Tree with an
Q
example.
Ans 2.
generaltreecan have multiple children per node, whereas abinary treecan have at most
A
two children (left and right). The process of converting a general tree to a binary tree involves
adjusting the structure while preserving relationships.
A
/ \
B
C
/ \
\
D
E F
3.
○ Binary Tree(using left-child, right-sibling):
A
/
B
/ \
D
E
\
C
/
F
4.
his binary tree structure preserves the hierarchical relationships of the general tree while using
T
the binary format.
3: What is Heap Sort? What is Merge Sort? Write the factors on the basis
Q
of which Heap Sort or Merge Sort is selected.
Ans 3:
Heap Sort:
eap Sort is a comparison-based sorting algorithm that uses abinary heapdata structure to
H
sort elements. A binary heap can be either a max-heap or a min-heap, where:
.
1 uild a max heap from the input array.
B
2. Swap the root (maximum value) with the last element of the heap.
3. Reduce the heap size and heapify the root.
4. Repeat the process until all elements are sorted.
Example:
.
1 uild a max-heap: [10, 5, 3, 4, 1].
B
2. Swap 10 with 1: [1, 5, 3, 4, 10].
3. Heapify the root: [5, 4, 3, 1, 10].
4. Repeat until sorted: [1, 3, 4, 5, 10].
Merge Sort:
erge Sort is adivide-and-conqueralgorithm that splits the input array into two halves, sorts
M
each half recursively, and then merges the sorted halves.
. D
1 ivide the array into two halves.
2. Recursively sort each half.
3. Merge the sorted halves into a single sorted array.
Example:
. D
1 ivide: [4, 1, 3] and [9, 7].
2. Sort recursively: [1, 3, 4] and [7, 9].
3. Merge: [1, 3, 4, 7, 9].
Factors for Choosing Heap Sort vs. Merge Sort:
Adoubly linked listis a type of linked list in which each node contains three parts:
. D
1 ata: The actual data element.
2. Next pointer: A pointer to the next node in the sequence.
3. Previous pointer: A pointer to the previous node in the sequence.
In a doubly linked list, navigation can be performed in both directions (forward and backward)
because each node maintains a pointer to both its next and previous nodes.
Here, each node points to both its previous and next node, allowing traversal in both directions.
T
● helast node’s next pointer points back to the first node.
● Thefirst node’s previous pointer points back to thelast node.
Differences Between Doubly Linked List and Circular Doubly Linked List:
1. Termination:
○ In adoubly linked list, traversal ends when the nextpointer of the last node is
NULL
.
○ In acircular doubly linked list, the last node’s next pointer points to the first
node, creating a loop.
2. Traversal:
○ Adoubly linked listallows traversal from the head to the tail and vice versa, but
it ends atNULL .
○ Acircular doubly linked listallows continuous traversal in both directions
NULL
without hitting .
3. Use Case:
○ Doubly linked listsare used when there’s a need for bi-directional traversal with
defined end points.
○ Circular doubly linked listsare used in applications like buffer management
where continuous looping is required.