BIT201 · TU past paper
Data Structures and Algorithms 2079 question paper
The complete TU 2079 exam paper for Data Structures and Algorithms (BIT201), all 12 questions with solved model answers written to the mark scheme.
Tap a question to open its answer.
- 110 marksStack usage in recursionHideAnswer
How stack is used in recursion? Explain different stack operations. Explain algorithm to convert an infix expression to postfix using stack.[10]
Stack in Recursion, Stack Operations, and Infix to Postfix Conversion
1. How Stack is Used in Recursion (2 marks)
When a recursive function is called, the system uses an internal data structure called the call stack (or system stack) to manage function calls.
Mechanism:
- Every time a function calls itself recursively, the current state (local variables, parameters, and return address) is pushed onto the stack.
- When the base case is reached, the function returns and the saved state is popped from the stack, resuming execution from where it left off.
- This continues until the stack is empty and the original call completes.
Example: Factorial of n
factorial(3) → calls factorial(2) [state of factorial(3) pushed] → calls factorial(1) [state of factorial(2) pushed] → returns 1 [base case] ← factorial(2) = 2×1 = 2 [popped] ← factorial(3) = 3×2 = 6 [popped]Stack at deepest point:
Stack (top → bottom) factorial(1) factorial(2) factorial(3) Stack follows LIFO (Last In First Out) which perfectly models the "last called, first returned" nature of recursion.
2. Stack Operations (3 marks)
A stack is a linear data structure that follows the LIFO principle. The main operations are:
a) PUSH (Insert an element)
Adds an element to the top of the stack.
Algorithm:
PUSH(STACK, TOP, MAXSIZE, ITEM): 1. If TOP = MAXSIZE - 1, then: Print "Stack Overflow" and Return 2. Set TOP = TOP + 1 3. Set STACK[TOP] = ITEM 4. Returnb) POP (Delete an element)
Removes and returns the element from the top of the stack.
Algorithm:
POP(STACK, TOP): 1. If TOP = -1, then: Print "Stack Underflow" and Return 2. Set ITEM = STACK[TOP] 3. Set TOP = TOP - 1 4. Return ITEMc) PEEK / TOP
Returns the top element without removing it.
PEEK(STACK, TOP): 1. If TOP = -1, then: Print "Stack is Empty" and Return 2. Return STACK[TOP]d) isEmpty
Checks whether the stack is empty.
isEmpty(STACK, TOP): 1. If TOP = -1, Return TRUE 2. Else Return FALSEe) isFull
Checks whether the stack is full.
isFull(STACK, TOP, MAXSIZE): 1. If TOP = MAXSIZE - 1, Return TRUE 2. Else Return FALSE
3. Algorithm to Convert Infix to Postfix Using Stack (5 marks)
Key Concepts
Term Meaning Infix Operator between operands: A + BPostfix Operator after operands: A B +Operator Precedence Table
Operator Precedence ^(exponent)3 (highest) *,/2 +,-1 (lowest) (0 (when in stack)
Algorithm: Infix to Postfix
InfixToPostfix(expression): 1. Initialize an empty STACK and empty output string POSTFIX. 2. Scan the infix expression from LEFT to RIGHT, one token at a time. 3. For each token: a. If token is an OPERAND (letter or digit): Append it directly to POSTFIX. b. If token is '(' (left parenthesis): PUSH it onto STACK. c. If token is ')' (right parenthesis): While TOP of STACK ≠ '(': POP from STACK and append to POSTFIX. POP '(' from STACK (discard it). d. If token is an OPERATOR (+, -, *, /, ^): While STACK is not empty AND precedence(TOP of STACK) >= precedence(token): POP from STACK and append to POSTFIX. PUSH the current token onto STACK. 4. After all tokens are scanned: While STACK is not empty: POP from STACK and append to POSTFIX. 5. Return POSTFIX.
Worked Example
Convert:
A + B * C - DStep Token Stack (bottom→top) Postfix Output 1 A (empty) A 2 + + A 3 B + A B 4 * + * A B 5 C + * A B C 6 - pop *→ pop+, push-A B C * + 7 D - A B C * + D End - (empty) A B C * + D - Result:
A B C * + D -
Another Example with Parentheses
Convert:
(A + B) * CStep Token Stack Postfix 1 ( ( 2 A ( A 3 + ( + A 4 B ( + A B 5 ) (empty) A B + 6 * - 25 marksNumericalComplete binary tree definition with exampHideAnswer
Explain complete binary tree with example. Starting with an empty binary search tree, show the effect of successively adding the following elements: 47, 50, 25, 27, 17, 61, 5, and 26. Also, traverse the resulting tree in pre-order, in-order, and post-order.[5]
- Insertion sequence into an empty BST: 47, 50, 25, 27, 17, 61, 5, 26 - Required: definition + example of complete binary tree, BST construction, and pre-order, in-order, post-order traversals. All data present. --- A complete binary tre...
- 310 marksNumericalQuick sort algorithm and tracingHideAnswer
Explain quick sort algorithm. Use this algorithm to sort the numbers 35, 82, 18, 54, 13, 31, 20, 69, and 19.[10]
Quick Sort is a divide-and-conquer sorting algorithm. It selects a pivot element and partitions the array so that all elements less than or equal to the pivot are on its left, and all greater elements are on its right. The pivot is then ...
- 45 marksArray as an ADTHideAnswer
Define ADT. Explain array as an ADT. [5]
--- An Abstract Data Type (ADT) is a mathematical model for a data type where the data type is defined by its behavior (operations) from the user's point of view, rather than by its implementation details. An ADT specifies: - What data i...
- 55 marksTime complexity definition and measurementHideAnswer
What is time complexity? Explain big oh notation with example. [5]
Time complexity is a measure of the amount of time (or number of basic operations) an algorithm takes to complete as a function of the size of its input n. - It does not measure actual clock time, but rather the growth rate of operations...
- 65 marksPriority queue definition and implementatiHideAnswer
Explain priority queue with example. What is circular queue? [5]
A priority queue is a special type of queue in which each element is associated with a priority value, and elements are served (removed) based on their priority rather than their insertion order. - An element with higher priority is dequ...
- 75 marksRecursion definition and benefitsHideAnswer
What are the benefits of using recursion? Write a recursive function to find nth Fibonacci number. [5]
- Simplicity and Readability: Recursive solutions are often cleaner and easier to understand than their iterative counterparts, especially for problems that are naturally recursive (e.g., tree traversal, factorial). 2. Reduces Code Leng...
- 85 marksSingly linked list definition and traversaHideAnswer
Explain singly linked list with example. Compare singly linked list with doubly linked list. [5]
A singly linked list is a linear data structure in which elements (called nodes) are stored in non-contiguous memory locations. Each node contains two parts: 1. Data field - stores the actual data/value 2. Next pointer - stores the addre...
- 95 marksBinary tree definition and applicationsHideAnswer
Explain different applications of binary tree. [5]
Applications of Binary Tree
Applications of Binary Tree
A binary tree is a tree data structure in which each node has at most two children, referred to as the left child and the right child. Binary trees have numerous important applications in computer science.
1. Binary Search Tree (BST)
- A binary tree is used to implement BST, where the left subtree contains nodes with values less than the root and the right subtree contains nodes with values greater than the root.
- Used for efficient searching, insertion, and deletion of data in O(log n) time.
- Example: Dictionary implementations, database indexing.
2. Expression Tree
- Binary trees are used to represent arithmetic expressions.
- Leaf nodes store operands (numbers/variables) and internal nodes store operators (+, -, *, /).
- Used in compilers to evaluate and parse mathematical expressions.
- Example: Expression
(a + b) * ccan be represented as a binary tree.
3. Heap (Priority Queue)
- A binary heap is a complete binary tree used to implement priority queues.
- Used in heap sort algorithm and scheduling algorithms in operating systems.
- Supports efficient retrieval of the minimum or maximum element in O(log n) time.
4. Huffman Coding Tree
- Binary trees are used in data compression algorithms such as Huffman encoding.
- Frequently occurring characters are assigned shorter binary codes and less frequent characters are assigned longer codes.
- Used in file compression tools like ZIP, JPEG, and MP3 encoding.
5. Decision Tree
- Binary trees are used in Artificial Intelligence and Machine Learning to model decisions.
- Each internal node represents a condition/test, and each leaf node represents a decision/outcome.
- Used in game playing algorithms, medical diagnosis systems, and classification problems.
Summary Table
Application Purpose Binary Search Tree Searching and sorting data Expression Tree Evaluating arithmetic expressions Heap / Priority Queue Scheduling and heap sort Huffman Coding Tree Data compression Decision Tree AI decision making
Binary trees form the foundation of many efficient algorithms and data structures used in real-world software systems.
- 105 marksDouble hashing collision resolutionHideAnswer
Explain collision and collision resolution in hashing. What is double hashing? [5]
A collision occurs when two or more different keys are mapped to the same hash table index by the hash function. For example, if hash function is h(k) = k mod 10: - h(23) = 3 - h(33) = 3 → Collision! Collisions are unavoidable when the n...
- 115 marksGraph representation using adjacency matriHideAnswer
Explain adjacency matrix representation of graphs with example. [5]
An adjacency matrix is a 2D square matrix used to represent a graph. For a graph with n vertices, the adjacency matrix is an n × n matrix (usually denoted A), where each entry A[i][j] indicates whether there is an edge between vertex i a...
- 125 marksSequential search algorithmHideAnswer
Write short notes on: a) Linear Search Write short notes on: b) Minimum Spanning Tree [2.5+2.5]
Short Notes
a) Linear Search
Definition: Linear search (also called sequential search) is the simplest searching algorithm that checks each element of a list one by one, from the beginning to the end, until the desired element (key) is found or the list is exhausted.
Algorithm:
LinearSearch(A[], n, key): 1. for i = 0 to n-1 do 2. if A[i] == key then 3. return i // element found at index i 4. return -1 // element not foundWorking:
- Start from the first element.
- Compare each element with the search key.
- If a match is found, return the position.
- If the end of the array is reached without a match, return -1 (not found).
Example: Array:
[5, 3, 8, 1, 9], Key =8- Compare 5 != 8, Compare 3 != 8, Compare 8 == 8 → Found at index 2.
Complexity:
Case Time Complexity Best Case O(1) Worst Case O(n) Average Case O(n) Space Complexity: O(1)
Advantages:
- Simple to implement.
- Works on both sorted and unsorted arrays.
- No preprocessing required.
Disadvantage:
- Inefficient for large datasets compared to binary search.
b) Minimum Spanning Tree (MST)
Definition: A Minimum Spanning Tree of a connected, undirected, weighted graph G = (V, E) is a spanning tree that connects all vertices with the minimum possible total edge weight, containing exactly |V| - 1 edges and no cycles.
Key Properties:
- It spans all vertices (all n vertices are included).
- It is acyclic (no cycles).
- It has exactly n - 1 edges for n vertices.
- The sum of edge weights is minimum among all possible spanning trees.
Example:
Consider a graph with 4 vertices and edges:
A --1-- B | | 4 2 | | D --3-- CMST selects edges: A-B (1), B-C (2), C-D (3) → Total weight = 6
Algorithms to Find MST:
Algorithm Approach Time Complexity Kruskal's Sort edges by weight; add edge if no cycle (greedy) O(E log E) Prim's Grow tree from a starting vertex by adding minimum weight edge O(E log V) Applications:
- Network design (telephone, electrical, road networks).
- Cluster analysis.
- Approximation algorithms for NP-hard problems (e.g., Travelling Salesman Problem).
- Designing least-cost communication networks.
Significance: MST provides the most cost-effective way to connect all nodes in a network, making it fundamental in graph theory and network optimization.