MCS-208 - Google Docs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

‭MCS-208‬

‭Data Structures and Algorithms‬

‭Ques 1: What are B-trees? Explain with example.‬


‭Ans 1:‬

‭B-trees Overview:‬

‭ ‬‭B-tree‬‭is a self-balancing tree data structure that‬‭maintains 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, meaning‬‭the tree grows in a balanced‬
‭way.‬
‭●‬ ‭Multi-way nodes‬‭: Unlike binary trees (2 nodes), B-trees‬‭can have multiple child nodes.‬
m‬‭can have at most‬‭
‭A B-tree of order‬‭ m-1‬‭keys and‬‭
m‬‭children.‬
‭●‬ E O(log n)‬‭time, where‬‭
‭ fficiency‬‭: Searching, inserting, and deleting all‬‭take‬‭ n‬‭is the‬
‭number of nodes in the tree.‬
‭●‬ ‭Internal Nodes as Pointers‬‭: Internal nodes are used‬‭as 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-1‬‭keys and‬‭
m‬‭children.‬
‭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.‬

‭Example of a B-tree (Order 3):‬

‭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, and‬‭then 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 systems‬‭that 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.‬

‭Tree to Binary Tree Conversion:‬

‭ general‬‭tree‬‭can have multiple children per node, whereas a‬‭binary tree‬‭can 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.‬

‭Steps to Convert a General Tree to Binary Tree:‬

‭1.‬ L ‭ eft-Child, Right-Sibling Representation‬‭: This technique is commonly used to convert‬


‭a general tree to a binary tree.‬
‭○‬ ‭The‬‭left child‬‭of each node represents the first child of the general tree.‬
‭○‬ ‭The‬‭right child‬‭of each node represents the next sibling in the general tree.‬
‭2.‬ ‭Example‬‭:‬
‭○‬ ‭General Tree‬‭:‬

A‬

/ \‬

B
‭ C‬
/ \
‭ \‬
D
‭ E F‬

‭3.‬
‭○‬ ‭Binary Tree‬‭(using left-child, right-sibling):‬

A‬

/‬

B‬

/ \‬

D
‭ E‬
\‬

C‬

/‬

F‬

‭4.‬

‭In this representation:‬


‭‬ T
● ‭ he left child of a node points to its first child (e.g., A’s left child is B).‬
‭●‬ ‭The right child points to the next sibling (e.g., B’s right child is C, D’s right child is E).‬

‭ 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 a‬‭binary heap‬‭data structure to‬
H
‭sort elements. A binary heap can be either a max-heap or a min-heap, where:‬

‭ ‬ I‭n a‬‭max-heap‬‭, the parent node is greater than its children.‬



‭●‬ ‭In a‬‭min-heap‬‭, the parent node is smaller than its children.‬

‭Steps in Heap Sort:‬

‭ .‬
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:‬

‭Consider the array: [4, 10, 3, 5, 1].‬

‭ .‬
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 a‬‭divide-and-conquer‬‭algorithm that splits the input array into two halves, sorts‬
M
‭each half recursively, and then merges the sorted halves.‬

‭Steps in Merge Sort:‬

‭ .‬ D
1 ‭ ivide the array into two halves.‬
‭2.‬ ‭Recursively sort each half.‬
‭3.‬ ‭Merge the sorted halves into a single sorted array.‬

‭Example:‬

‭For the array [4, 1, 3, 9, 7]:‬

‭ .‬ 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:‬

‭1.‬ ‭Space Complexity‬‭:‬


O(1)‬‭auxiliary space (in-place sorting).‬
‭○‬ ‭Heap Sort‬‭:‬‭
‭ ‬ ‭Merge Sort‬‭:‬‭
○ O(n)‬‭auxiliary space (requires extra memory for merging).‬
‭2.‬ ‭Time Complexity‬‭:‬
‭○‬ ‭Both have a time complexity of‬‭ O(n log n)‬‭in the worst case.‬
‭ .‬ ‭Stability‬‭:‬
3
‭○‬ ‭Merge Sort‬‭is stable (it maintains the relative order of equal elements).‬
‭○‬ ‭Heap Sort‬‭is not stable.‬
‭4.‬ ‭Data Distribution‬‭:‬
‭○‬ ‭Heap Sort is typically used for sorting‬‭in-place‬‭with no additional memory‬
‭overhead.‬
‭○‬ ‭Merge Sort is better suited when stability is a priority or when working with linked‬
‭lists.‬
‭ 4: What is a Doubly Linked List? How does it differ from Circularly Doubly‬
Q
‭Linked List?‬
‭Ans 4:‬

‭Doubly Linked List:‬

‭A‬‭doubly linked list‬‭is 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.‬

I‭n 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.‬

‭Example of a Doubly Linked List:‬

NULL <- [1] <-> [2] <-> [3] -> NULL‬


‭Here, each node points to both its previous and next node, allowing traversal in both directions.‬

‭Circular Doubly Linked List:‬

‭A‬‭circular doubly linked list‬‭is a variation of a doubly linked list in which:‬

‭‬ T
● ‭ he‬‭last node’s next pointer points back to the first node‬‭.‬
‭●‬ ‭The‬‭first node’s previous pointer points back to the‬‭last node‬‭.‬

‭This forms a‬‭circular‬‭structure, meaning traversal can continue indefinitely.‬

‭Example of Circular Doubly Linked List:‬

[1] <-> [2] <-> [3]‬



^
‭ |‬
|------------------|‬

[3]‬‭points back to‬‭


‭In this example, node‬‭ [1]‬‭through its next pointer, and‬‭
[1]‬‭points back to‬
[3]‬‭through its previous pointer.‬

‭Differences Between Doubly Linked List and Circular Doubly Linked List:‬
‭1.‬ ‭Termination‬‭:‬
‭○‬ ‭In a‬‭doubly linked list‬‭, traversal ends when the next‬‭pointer of the last node is‬
NULL‬
‭ ‭.‬
‭○‬ ‭In a‬‭circular doubly linked list‬‭, the last node’s next pointer points to the first‬
‭node, creating a loop.‬
‭2.‬ ‭Traversal‬‭:‬
‭○‬ ‭A‬‭doubly linked list‬‭allows traversal from the head to the tail and vice versa, but‬
‭it ends at‬‭NULL‬ ‭.‬
‭○‬ ‭A‬‭circular doubly linked list‬‭allows continuous traversal in both directions‬
NULL‬
‭without hitting‬‭ ‭.‬
‭3.‬ ‭Use Case‬‭:‬
‭○‬ ‭Doubly linked lists‬‭are used when there’s a need for bi-directional traversal with‬
‭defined end points.‬
‭○‬ ‭Circular doubly linked lists‬‭are used in applications like buffer management‬
‭where continuous looping is required.‬

You might also like