Major 2 (Programming Methodology & Data Structures)
Major 2 (Programming Methodology & Data Structures)
Session 2023 24
Class –BCA First Year
Subject –Major 2 ( Programming Methodology & Data Structures)
CCE – II Submitted to Dr. Teerath Prasad patel
Note – Long term answer type questions
Q.1 what is Array? Explain type with Suitable example.
Q.2 Explain Stack with operation and Algorithms.
Q.3 Explain Linked List with its Types.
Q.4 Explain tree with its types.
Q.5 What is sorting? explain
1) Bubble sort 2) selection sort 3) Quick Sort
Question: 1.
Answer: 1.
There are two main types of arrays: one dimensional arrays and
multidimensional arrays.
Example in Python:
python
# Creating a one dimensional array (list in Python)
numbers = [1, 2, 3, 4, 5]
Answer: 2.
A stack is a fundamental data structure that follows the Last In, First Out (LIFO)
principle. This means that the last element added to the stack is the first one to
be removed. Think of it as a collection of elements with two main operations:
push (to add an element) and pop (to remove the last added element).
1. Push: This operation is used to add an element to the top of the stack.
2. Pop: This operation is used to remove the element from the top of the
stack.
3. Peek (or Top): This operation returns the element at the top of the stack
without removing it.
1. Balanced Parentheses:
Using a stack, you can check if a given expression has balanced
parentheses. The algorithm involves iterating through the expression and
pushing opening parentheses onto the stack and popping when a closing
parenthesis is encountered. At the end, the stack should be empty for the
expression to be balanced.
2. Expression Evaluation:
Stacks can be used to evaluate expressions in postfix or prefix notation. For
example, in postfix notation, you iterate through the expression and push
operands onto the stack. When an operator is encountered, you pop the
required number of operands, perform the operation, and push the result
back onto the stack.
def isEmpty(self):
return len(self.items) == 0
def pop(self):
if not self.isEmpty():
return self.items.pop()
else:
raise IndexError("pop from an empty stack")
def peek(self):
if not self.isEmpty():
return self.items[ 1]
else:
raise IndexError("peek from an empty stack")
def size(self):
return len(self.items)
# Example Usage:
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
stack.pop()
print("After pop, new top:", stack.peek()) # Output: 2
```
Answer: 3.
A linked list is a linear data structure used for organizing and storing data. Unlike
arrays, elements in a linked list are not stored in contiguous memory locations;
instead, each element (node) points to the next one. This structure provides
flexibility in terms of size and allows for efficient insertions and deletions, though
accessing elements by index can be slower compared to arrays.
5. Skip List:
A more advanced type of linked list that incorporates multiple layers of
linked lists to allow for faster search operations.
Skip lists are typically used in scenarios where a balance between simplicity
and search efficiency is needed.
Linked lists have various applications, and the choice of which type to use
depends on the specific requirements of a given problem. Each type has its own
advantages and trade offs in terms of memory usage, ease of implementation,
and performance for different operations.
Question: 4.
Answer: 4.
1. Node: A fundamental building block of a tree that contains data and may have
a reference to one or more child nodes.
2. Root: The topmost node in a tree; it has no parent.
3. Parent: A node that has one or more child nodes.
4. Child: A node that has a parent.
5. Leaf: A node that has no children, i.e., it is a node without any descendants.
6. Subtree: A tree formed by a node and all its descendants.
7. Depth: The level or distance of a node from the root. The root is considered to
be at depth 0, and its children are at depth 1, and so on.
8. Height: The length of the longest path from a node to a leaf. The height of the
tree is the height of the root.
#Types of Trees:
1. Binary Tree:
Each node has at most two children, referred to as the left child and the
right child.
Useful for representing hierarchical relationships, such as in expression
trees or binary search trees.
3. AVL Tree:
A self balancing binary search tree where the height difference between
the left and right subtrees of any node (called the balance factor) is limited
to { 1, 0, 1}.
Maintains balance to ensure efficient operations.
4. B Tree:
A balanced tree structure designed for efficient searching and retrieval in
large datasets.
Widely used in databases and file systems.
These are just a few examples, and there are many other types of trees used in
various applications based on specific requirements and use cases.
Question: 5.
Answer: 5.
There are various sorting algorithms, each with its own advantages and
disadvantages in terms of time complexity, space complexity, and stability. Here
are some commonly used sorting algorithms:
1. Bubble Sort:
A simple sorting algorithm that repeatedly steps through the list,
compares adjacent elements, and swaps them if they are in the wrong
order.
It has a time complexity of O(n^2) in the worst and average cases.
2. Selection Sort:
An in place comparison sorting algorithm that divides the input list into a
sorted and an unsorted region.
It has a time complexity of O(n^2) in all cases.
3. Insertion Sort:
Builds the final sorted array one item at a time by repeatedly taking
elements from the unsorted part and inserting them into their correct
position in the sorted part.
It has a time complexity of O(n^2) in the worst and average cases.
4. Merge Sort:
A divide and conquer algorithm that divides the input into smaller parts,
recursively sorts them, and then merges them to produce a sorted output.
It has a time complexity of O(n log n) in all cases, making it more efficient
than the previous algorithms for large datasets.
5. Quick Sort:
Another divide and conquer algorithm that works by selecting a 'pivot'
element and partitioning the other elements into two sub arrays according
to whether they are less than or greater than the pivot.
It has a time complexity of O(n log n) on average, but its worst case time
complexity is O(n^2).
6. Heap Sort:
A comparison based sorting algorithm that uses a binary heap data
structure.
It has a time complexity of O(n log n) in all cases, and it is often used where
a stable sort is not required.
7. Radix Sort:
A non comparative integer sorting algorithm that distributes elements into
buckets according to their individual digits.
It has linear time complexity O(nk) for integers with k digits.
Choosing the right sorting algorithm depends on the specific requirements of the
task at hand, such as the size of the dataset, the desired order, and the
characteristics of the data.