Explanation of BCA Programs
Explanation of BCA Programs
10-Binary Search Tree me left side chhote aur right side bade values hote hain. Yeh program
insertion aur searching dono karta hai.
11- Graph ko represent karne ka yeh matrix method hai jisme 1 edge dikhata hai aur 0 no
connection dikhata hai.
12- HashMap key-value pair ko store karta hai aur fast lookup provide karta hai. Yeh hashing ka
basic example hai.
13-Bubble Sort adjacent elements ko compare karta hai aur swap karta hai jab tak pura array
sorted na ho jaye.
14- Selection Sort me har baar minimum element ko select karke uski correct position pe swap
kiya jata hai.
Program1 –
main() is the program’s entry point — like a switch to start your code.
Here you're creating an integer array with 5 elements. Loop through each element from
index 0 to 4.
Arrays are like boxes arranged in a row, and each has a number inside.
�Change Suggestion:
arr[0] = 10;
�Real-World Use:
Arrays are good when you know the number of elements in advance.
Student 0: Alice
Student 1: Bob
Student 2: Charlie
� Real-world scenario: Class attendance, employee names, Storing marks, prices, sensor
data.etc.
Program2–
Real-World Scenario:
1�⃣ Grocery Store Barcode Search:
Why Important?
Foundation of search logic.
Easy to understand and apply.
Forms the base for more advanced searches like Binary Search or Hash-based Search.
Program3-
import java.util.Arrays;
� Real-World Scenario
1�⃣ Phone Book Lookup
Why Is It Important?
Faster than linear search for large datasets.
Used in databases, search engines, and operating systems.
Learning it helps you think like a problem-solver.
java
CopyEdit
if (top == stack.length - 1) {
Checks if the stack is already full.
No more room if top is at the last index.
java
CopyEdit
stack[++top] = data;
�void pop()
java
CopyEdit
if (top == -1) {
java
CopyEdit
System.out.println(stack[top] + " popped from stack");
top--;
�void display()
java
CopyEdit
s.push(10);
s.push(20);
s.push(30);
java
CopyEdit
s.display();
java
CopyEdit
s.pop();
java
CopyEdit
s.display();
� Real-World Scenario
1�⃣ Undo Feature in Word Processors:
Why Important?
Stack is used in:
o Expression evaluation (2 + 3 * 5).
o Function call history (Recursion).
o Undo-redo systems.
o Syntax parsing in compilers.
Program 5: Queue Implementation Using Array in Java
int[] queue = new int[5];
java
CopyEdit
if (rear == queue.length - 1) {
java
CopyEdit
if (front == -1) front = 0;
java
CopyEdit
queue[++rear] = data;
�void dequeue()
java
CopyEdit
System.out.println(queue[front] + " removed from the queue");
front++;
Prints and removes the first element by moving the front pointer ahead.
�void display()
� Real-World Scenario
1�⃣ Ticket Counters / Queues at Shops
Why Important?
Queues are used in:
o CPU process scheduling.
o Print services.
o Real-life task lists.
o Messaging systems like WhatsApp, Kafka, RabbitMQ.
java
CopyEdit
if ((rear + 1) % queue.length == front) {
java
CopyEdit
if (front == -1) front = 0;
If inserting the first element, set front = 0.
java
CopyEdit
rear = (rear + 1) % queue.length;
queue[rear] = data;
�void dequeue()
java
CopyEdit
if (front == -1) {
java
CopyEdit
if (front == rear) {
If front and rear are the same: only 1 item was present.
Reset both to -1 after deletion.
java
CopyEdit
front = (front + 1) % queue.length;
�void display()
Prints the elements from front to rear, even when rear < front (circular).
java
CopyEdit
int i = front;
while (true) {
System.out.print(queue[i] + " ");
if (i == rear) break;
i = (i + 1) % queue.length;
}
� Real-World Scenario
1�⃣ Car Parking System
Why Is It Important?
Prevents wasted space.
Used in OS-level memory and process management.
Perfect for queues where elements keep entering and exiting frequently.
java
CopyEdit
if (head == null) {
head = newNode;
}
java
CopyEdit
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
newNode.prev = temp;
If not empty, traverse to the last node and link the new node both ways:
o temp.next = newNode
o newNode.prev = temp
�void deleteByValue(int value)
java
CopyEdit
head = head.next;
if (head != null) {
head.prev = null;
}
java
CopyEdit
temp.prev.next = temp.next;
if (temp.next != null) {
temp.next.prev = temp.prev;
}
Reconnects the previous and next nodes — the node gets removed.
�void displayForward()
�void displayBackward()
Starts from the last node and goes backward using prev.
� Real-World Scenario
1�⃣ Music Player Playlist:
Skip Next or Previous track — both directions.
Uses next and prev links.
Node Class
java
CopyEdit
class Node {
java
CopyEdit
int data;
java
CopyEdit
Node left, right;
Each node has two links: one to the left child and one to the right child.
java
CopyEdit
public Node(int item) {
data = item;
left = right = null;
}
A constructor to initialize the node's value and set both child pointers to null (meaning no child
yet).
Creates the main class to manage the tree and its traversals.
java
CopyEdit
Node root;
Defines the top-most node of the tree. Initially, it’s null until you assign something to it.
Method to perform Inorder traversal — first Left, then Root, then Right.
java
CopyEdit
if (node == null)
return;
Base case for recursion: if the current node is null, do nothing and return.
java
CopyEdit
printInorder(node.left);
java
CopyEdit
System.out.print(node.data + " ");
Visit (print) the node’s data once the left side is done.
java
CopyEdit
printInorder(node.right);
Defines the Preorder traversal method — Root first, then Left, then Right.
java
CopyEdit
if (node == null)
return;
java
CopyEdit
System.out.print(node.data + " ");
java
CopyEdit
printPreorder(node.left);
java
CopyEdit
printPreorder(node.right);
java
CopyEdit
if (node == null)
return;
java
CopyEdit
printPostorder(node.left);
java
CopyEdit
printPostorder(node.right);
java
CopyEdit
System.out.print(node.data + " ");
java
CopyEdit
BinaryTree tree = new BinaryTree();
java
CopyEdit
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
markdown
CopyEdit
1
/ \
2 3
/ \
4 5
java
CopyEdit
System.out.println("\nPreorder traversal:");
tree.printPreorder(tree.root);
java
CopyEdit
System.out.println("\nPostorder traversal:");
tree.printPostorder(tree.root);
�Real-Life Analogy
java
CopyEdit
int key;
java
CopyEdit
Node left, right;
java
CopyEdit
public Node(int item) {
key = item;
left = right = null;
}
A constructor that assigns a value (item) to the key and initializes left and right children to null
— meaning no children when the node is created.
java
CopyEdit
Node root;
java
CopyEdit
BST() {
root = null;
}
Constructor: when you create a BST object, the root is set to null.
This is a public method that initiates the insertion process by calling insertRec.
Every new number is inserted starting from the root.
This method does the real work — it finds the correct place in the tree for the new node.
java
CopyEdit
if (root == null) {
root = new Node(key);
return root;
}
If the current node is null, this is the correct spot — create a new Node with the key.
java
CopyEdit
if (key < root.key)
root.left = insertRec(root.left, key);
If the key is smaller than the current node's key, insert it into the left subtree.
java
CopyEdit
else if (key > root.key)
root.right = insertRec(root.right, key);
Return the unchanged node pointer, so that the tree links stay correct.
java
CopyEdit
if (root == null)
return false;
If the current node is null, the key doesn't exist in the tree.
java
CopyEdit
if (root.key == key)
return true;
If the current node’s key matches the search key — success! Found it.
java
CopyEdit
return key < root.key ? searchRec(root.left, key) :
searchRec(root.right, key);
Ternary operator:
java
CopyEdit
BST tree = new BST();
markdown
CopyEdit
50
/ \
30 70
/ \ / \
20 40 60 80
�Searching
java
CopyEdit
System.out.println(tree.search(40) ? "Found" : "Not Found");
java
CopyEdit
System.out.println(tree.search(25) ? "Found" : "Not Found");
�Real-Life Analogy
This is exactly how a Binary Search Tree works — efficient, sorted, and easy to search.
Defines a class Graph that will hold the adjacency matrix and related functions.
3�⃣ Constructor
java
CopyEdit
public Graph(int numVertices) {
this.numVertices = numVertices;
adjMatrix = new int[numVertices][numVertices];
}
java
CopyEdit
for (int i = 0; i < numVertices; i++) {
for (int j = 0; j < numVertices; j++) {
System.out.print(adjMatrix[i][j] + " ");
}
System.out.println();
}
Nested loops:
java
CopyEdit
Graph g = new Graph(4);
�Adding Edges
java
CopyEdit
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 3);
0 connected to 1 and 2.
1 connected to 2.
2 connected to 3.
Expected Output:
Adjacency Matrix:
0 1 1 0
1 0 1 0
1 1 0 1
0 0 1 0
�Real-Life Analogy
Import Statement
java
CopyEdit
import java.util.HashMap;
This imports the HashMap class from Java’s built-in java.util package.
HashMap allows you to store data as key-value pairs.
java
CopyEdit
for (Integer key : map.keySet()) {
System.out.println("Key: " + key + ", Value: " + map.get(key));
}
java
CopyEdit
if (map.containsKey(searchKey)) {
System.out.println("Found! Value: " + map.get(searchKey));
} else {
System.out.println("Key not found!");
}
This is why hashing gives you lightning-fast lookup, insertion, and deletion for keys!
�Real-Life Analogy
Class Declaration
java
CopyEdit
class BubbleSort {
Defines a public class named BubbleSort where the sorting logic will live.
If the current element is larger than the next one, swap them.
java
CopyEdit
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
This is the "bubble" action — the largest unsorted element moves to its correct position.
6�⃣ Print Array Method
java
CopyEdit
void printArray(int arr[]) {
java
CopyEdit
int n = arr.length;
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
System.out.println();
�Real-Life Analogy
Imagine a lineup of students arranged by height but randomly shuffled. You start from the first
pair, compare them, and if the first one is taller, they swap places. You repeat this until the
tallest student walks to the end — just like a bubble rising to the top of water. That’s the
essence of Bubble Sort!
Class Declaration
java
CopyEdit
class SelectionSort {
Loop starts from i+1 (the unsorted part) and goes to the end.
j scans the unsorted section for the smallest element.
java
CopyEdit
int n = arr.length;
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
System.out.println();
�Main Method
java
CopyEdit
public static void main(String args[]) {
�Real-Life Analogy