Document 4
Document 4
Advanced Data
Structure and
Algorithms
R1UC503B
Name: Divyam
Adm. No.: 22SCSE1570012
Section: 1 CNCS
Submitted to: Dr. Vipul Narayan
Program 1
Find the Maximum and Minimum Elements in
an Array: Write a function to find the
maximum and minimum elements in an array.
=>Program:
public class MaxMin {
public static int[] findMaxMin(int[] arr) {
int max = arr[0], min = arr[0];
for (int num : arr) {
if (num > max) max = num;
if (num < min) min = num;
}
return new int[]{max, min};
}
=>Output:
Program 3
Find the kth largest/smallest Element in an
Array:
Write a function to find the kth smallest or
largest element in an Array.
=>Program:
import java.util.Arrays;
=>Output:
Program 4
Sort an array of 0s, 1s and 2s: Given an array
containing only 0s, 1s and 2s. Sort the array in
linear time.
=>Program:
public class Sort012 {
public static void sort(int[] arr) {
int low = 0, mid = 0, high = arr.length - 1;
while (mid <= high) {
if (arr[mid] == 0) {
swap(arr, low++, mid++);
} else if (arr[mid] == 1) {
mid++;
} else {
swap(arr, mid, high--);
}
}
}
=>Output:
Program 5
Move All Zeroes to End of Array: Write a
function to move all zeroes in an array to the
end while maintaining the relative order of
other elements.
=>Program:
public class MoveZeroes {
public static void moveZeroes(int[] arr) {
int count = 0;
for (int num : arr) {
if (num != 0) {
arr[count++] = num;
}
}
while (count < arr.length) {
arr[count++] = 0;
}
}
Program 6
Reverse a Linked List: Write a function to
reverse a singly linked list.
=>Program:
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
Program 7
Detect a Cycle in a Linked List: Write a
function to detect if a cycle exists in a linked
list.
=>Program:
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
Program 8
Find the Middle of a Linked List: Write a
function to find the middle element of a linked
list.
=>Program:
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
Porgram 9
Merge Two Sorted Linked Lists: Write a
function to merge two sorted linked lists into
one sorted linked list.
=>Program:
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
=>Output:
Program 10
Remove Nth Node from End of List: Write a
function to remove the Nth node from the
start/end of a linked list.
=>Program:
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
=>Output:
Program 11
Implement a Stack Using Arrays/Lists: Write a
function to implement a stack using an array
or list with basic operations: push, pop, peek,
and isEmpty.
=>Program:
import java.util.ArrayList;
import java.util.List;
public class Stack {
private List<Integer> stack;
public Stack() {
stack = new ArrayList<>();
}
Program 12
Implement a Stack Using Linked List: Write a
function to implement a stack using a linked
list with basic operations: push, pop, peek, and
isEmpty.
=>Program:
class ListNode {
int value;
ListNode next;
ListNode(int value) {
this.value = value;
}
}
public class Stack {
private ListNode top;
public Stack() {
top = null;
}
public void push(int value) {
ListNode newNode = new ListNode(value);
newNode.next = top;
top = newNode;
}
public int pop() {
if (isEmpty()) {
System.out.println("Stack is empty. Can't pop");
return -1;
} else {
int poppedValue = top.value;
top = top.next;
return poppedValue;
}
}
public int peek() {
if (isEmpty()) {
System.out.println("Stack is empty. Can't peek");
return -1;
} else {
return top.value;
}
}
public boolean isEmpty() {
return top == null;
}
public static void main(String[] args) {
Stack stack = new Stack();
stack.push(10);
stack.push(40);
stack.push(120);
System.out.println("Peek: " + stack.peek());
System.out.println("Pop: " + stack.pop());
System.out.println("IsEmpty: " + stack.isEmpty());
}
}
=>Output:
Program 13
Check for Balanced Parentheses: Write a
function to check if a string containing
parentheses is balanced.
=>Program:
import java.util.Stack;
Program 14
Evaluate Postfix Expression: Write a function
to evaluate a given postfix expression.
=>Program:
import java.util.Stack;
=>Output:
Program 15
Next Greater Element: Write a function to find
the next greater element for each elementin
an array.
=>Program:
import java.util.Stack;
import java.util.Arrays;
=>Output:
Program 16
Implement a Queue Using Arrays/Lists: Write a
function to implement a queue using an array
or list with basic operations: enqueue,
dequeue, front, and isEmpty.
=>Program:
import java.util.LinkedList;
public QueueUsingList() {
queue = new LinkedList<>();
}
public void enqueue(int data) {
queue.addLast(data);
}
public int dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty. Cannot dequeue.");
return -1;
}
return queue.removeFirst();
}
public int front() {
if (isEmpty()) {
System.out.println("Queue is empty.");
return -1;
}
return queue.getFirst();
}
=>Output:
Program 17
Implement a Queue Using Linked List: Write a
function to implement a queue using a linked
list with basic operations: enqueue, dequeue,
front, and isEmpty.
=>Program:
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
public class QueueUsingLinkedList {
private Node front;
private Node rear;
public QueueUsingLinkedList() {
front = null;
rear = null;
}
public void enqueue(int data) {
Node newNode = new Node(data);
if (rear == null) {
front = rear = newNode;
return;
}
rear.next = newNode;
rear = newNode;
}
public int dequeue() {
if (front == null) {
return -1;
}
int data = front.data;
front = front.next;
if (front == null) {
rear = null;
}
return data;
}
public int front() {
if (front == null) {
return -1;
}
return front.data;
}
public boolean isEmpty() {
return front == null;
}
public static void main(String[] args) {
QueueUsingLinkedList queue = new QueueUsingLinkedList();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
System.out.println("Front element: " + queue.front()); // 1
System.out.println("Dequeued element: " + queue.dequeue()); // 1
System.out.println("Is queue empty? " + queue.isEmpty()); // false
}
}
=>Output:
Program 18
Find the kth largest/smallest Element in an
Array:
Write a function to find the kth smallest or
largest element in an Array.
=>Program:
public class CircularQueue {
private int[] queue;
private int front, rear, size, capacity;
public CircularQueue(int capacity) {
this.capacity = capacity;
queue = new int[capacity];
front = rear = size = 0;
}
public void enqueue(int data) {
if (size == capacity) return;
queue[rear] = data;
rear = (rear + 1) % capacity;
size++;
}
public int dequeue() {
if (size == 0) return -1;
int data = queue[front];
front = (front + 1) % capacity;
size--;
return data;
}
public int front() {
return size == 0 ? -1 : queue[front];
}
public int rear() {
return size == 0 ? -1 : queue[(rear - 1 + capacity) % capacity];
}
public boolean isEmpty() {
return size == 0;
}
public static void main(String[] args) {
CircularQueue cq = new CircularQueue(5);
cq.enqueue(1);
cq.enqueue(2);
System.out.println(cq.front()); // 1
System.out.println(cq.dequeue()); // 1
System.out.println(cq.isEmpty()); // false
}
}
=>Output:
Program 19
Generate Binary Numbers from 1 to N: Write a
function to generate binary numbers from1 to
N using a queue.
=>Program:
import java.util.LinkedList;
import java.util.Queue;
Program 20
Implement a Queue Using Stacks: Write a
function to implement a queue using two
stacks.
=>Program:
import java.util.Stack;
public class QueueUsingStacks {
private Stack<Integer> stack1;
private Stack<Integer> stack2;
public QueueUsingStacks() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
public void enqueue(int data) {
stack1.push(data);
}
public int dequeue() {
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
public int front() {
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.peek();
}
public boolean isEmpty() {
return stack1.isEmpty() && stack2.isEmpty();
}
public static void main(String[] args) {
QueueUsingStacks queue = new QueueUsingStacks();
queue.enqueue(1);
queue.enqueue(2);
System.out.println("Front element: " + queue.front()); //
1
System.out.println("Dequeued element: " +
queue.dequeue()); // 1
System.out.println("Is queue empty? " + queue.isEmpty());
// false
}
}
=>Output:
Program 21
Implement a Binary Tree: Write a class to
implement a basic binary tree with insert,
delete, and traversal operations.
=>Program:
class Node {
int data; Node left, right;
Node(int item) { data = item; left = right = null; }
}
public class BinaryTree {
Node root;
public void insert(int data) { root = insertRec(root, data); }
private Node insertRec(Node root, int data) {
if (root == null) { root = new Node(data); return root; }
if (data < root.data) root.left = insertRec(root.left, data);
else if (data > root.data) root.right = insertRec(root.right, data);
return root;
}
public void delete(int data) { root = deleteRec(root, data); }
private Node deleteRec(Node root, int data) {
if (root == null) return root;
if (data < root.data) root.left = deleteRec(root.left, data);
else if (data > root.data) root.right = deleteRec(root.right, data);
else {
if (root.left == null) return root.right;
else if (root.right == null) return root.left;
root.data = minValue(root.right);
root.right = deleteRec(root.right, root.data);
}
return root;
}
private int minValue(Node root) {
int minv = root.data;
while (root.left != null) {
minv = root.left.data; root = root.left;
}
return minv;
}
public void inorder() { inorderRec(root); }
private void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.print(root.data + " ");
inorderRec(root.right);
}
}
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
tree.insert(50); tree.insert(30); tree.insert(20);
tree.insert(40); tree.insert(70); tree.insert(60);
tree.insert(80); tree.inorder();
tree.delete(20); tree.inorder();
}
}
=>Output:
Program 22
Inorder Traversal: Write a function to perform
inorder traversal of a binary tree.
=>Program:
class Node {
int data; Node left, right;
Node(int item) { data = item; left = right = null;
}
}
=>Output:
Program 23
Preorder Traversal: Write a function to perform
preorder traversal of a binary tree.
=>Program:
class Node {
int data; Node left, right;
Node(int item) { data = item; left = right = null; }
}
=>Output:
Program 24
Postorder Traversal: Write a function to
perform postorder traversal of a binary tree.
=>Program:
class Node {
int data; Node left, right;
Node(int item) { data = item; left = right = null; }
}
=>Output:
Program 25
Level Order Traversal: Write a function to
perform level order traversal of a binary tree.
=>Program:
import java.util.LinkedList;
import java.util.Queue;
class Node {
int data; Node left, right;
Node(int item) { data = item; left = right = null; }
}