DSA Saransh
DSA Saransh
class Node {
int data; // Data part of the node
Node next; // Pointer to the next node
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;
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
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