0% found this document useful (0 votes)
15 views8 pages

DSA Saransh

yujtyut

Uploaded by

YOGENDRA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views8 pages

DSA Saransh

yujtyut

Uploaded by

YOGENDRA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Q. Write a program to implement Linked List in java.

class Node {
int data; // Data part of the node
Node next; // Pointer to the next node

// Constructor to create a new node


Node(int data) {
this.data = data;
this.next = null;
}
}
// LinkedList class
public class LinkedList {
private Node head; // Head of the list

// Method to insert a new node at the beginning of the list


public void insertFirst(int data) {
Node newNode = new Node(data);
newNode.next = head; // Point new node to the current head
head = newNode; // Update head to the new node
}

// Method to delete the first node of the list


public void deleteFirst() {
if (head != null) {
head = head.next; // Move head to the next node
} else {
System.out.println("List is empty. Nothing to delete.");
}
}

// Method to insert a new node at the end of the list


public void insertLast(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode; // If the list is empty, make the new node the head
} else {
Node current = head;
while (current.next != null) {
current = current.next; // Traverse to the end of the list
}
current.next = newNode; // Link the new node at the end
}
}

// Method to delete the last node of the list


public void deleteLast() {
if (head == null) {
System.out.println("List is empty. Nothing to delete.");
return;
}
if (head.next == null) {
head = null; // If only one node is present, set head to null
return;
}
Node current = head;
while (current.next.next != null) {
current = current.next; // Traverse to the second last node
}
current.next = null; // Remove the last node
}

// Method to print the linked list


public void print() {
Node current = head;
while (current != null) {
System.out.print(current.data + " -> "); // Print the current node's
data
current = current.next; // Move to the next node
}
System.out.println("null"); // End of the list
}

// Main method to test the LinkedList implementation


public static void main(String[] args) {
LinkedList list = new LinkedList();

list.insertFirst(20);
list.insertFirst(10);
list.insertLast(30);
list.insertLast(40);

System.out.println("Linked List:");
list.print();

list.deleteFirst();
System.out.println("After deleting first element:");
list.print();

list.deleteLast();
System.out.println("After deleting last element:");
list.print();
}
}

Output:
Linked List:
10 -> 20 -> 30 -> 40 -> null
After deleting first element:
20 -> 30 -> 40 -> null
After deleting last element:
20 -> 30 -> null
Q. Write a program to implement Merge Sort in java.

import java.util.Arrays;

public class MergeSort{


public static void main(String[] args) {
int [] arr = {5, 4, 3, 2, 1};
mergerSort(arr, 0, arr.length); // Sort the array
System.out.println(Arrays.toString(arr)); // Print the sorted array
}

static void mergerSort(int [] arr, int s, int e) {


if (e - s < 2) return; // Base case
int mid = (e + s) / 2; // Find middle index
mergerSort(arr, s, mid); // Sort left half
mergerSort(arr, mid, e); // Sort right half
merge(arr, s, mid, e); // Merge the sorted halves
}

// Merge two sorted sub-arrays into one


static void merge(int [] arr, int s, int m, int e) {
int [] mix = new int[e - s]; // Temporary array for merging
int i = s, j = m, k = 0;

// Merge elements from both halves


while (i < m && j < e) {
if (arr[i] < arr[j]) mix[k++] = arr[i++];
else mix[k++] = arr[j++];
}

// Copy remaining elements from left half, if any


while (i < m) mix[k++] = arr[i++];

// Copy remaining elements from right half, if any


while (j < e) mix[k++] = arr[j++];

// Copy merged array back into original array


for (int l = 0; l < mix.length; l++) arr[s + l] = mix[l];
}
}

Output:
[1, 2, 3, 4, 5]
Q. Write a program to implement Binary Search Tree in
java.
class BST {
public class Node {
private int value; // Value of the node
private Node left; // Left child node
private Node right; // Right child node
private int height; // Height of the tree

public Node(int value) {


this.value = value;
}
public int getValue() {
return value;
}
}
private Node root; // Root node of the tree
// Method to insert a new value into the BST
public void insert(int value) {
root = insert(value, root);
}
// Private recursive method to insert a new value at the correct position
private Node insert(int value, Node node) {
if (node == null) {
node = new Node(value);
return node;
}
// If value is smaller than current node, go to the left subtree
if (value < node.value) {
node.left = insert(value, node.left);
}
// If value is greater than current node, go to the right subtree
if (value > node.value) {
node.right = insert(value, node.right);
}
// Update the height of the node
node.height = Math.max(height(node.left), height(node.right)) + 1;
return node;
}
// Method to populate the tree with an array of values
public void populate(int[] nums) {
for (int i = 0; i < nums.length; i++) {
this.insert(nums[i]);
}
}
// Method to display the tree starting from the root
public void display() {
display(this.root, "Root Node: ");
}
// Recursive method to display the tree in a structured way
private void display(Node node, String details) {
if (node == null) {
return;
}
System.out.println(details + node.value);
// Recursively display left and right children
display(node.left, "Left child of " + node.value + " : ");
display(node.right, "Right child of " + node.value + " : ");
}
// Method to check if the tree is empty (root is null)
public boolean isEmpty() {
return root == null;
}
// Method to get the height of a given node
public int height(Node node) {
if (node == null) {
return -1;
}
return node.height;
}
}

public class BinarySearchTree {


public static void main(String[] args) {
BST tree = new BST();
int[] nums = {5, 2, 7, 1, 4, 6, 9, 8, 3, 10};
tree.populate(nums);
tree.display();
}
}

Output:
Root Node: 5
Left child of 5 : 2
Left child of 2 : 1
Right child of 2 : 4
Left child of 4 : 3
Right child of 4 : null
Right child of 5 : 7
Left child of 7 : 6
Right child of 7 : 9
Left child of 9 : 8
Right child of 9 : 10

You might also like