Skip to content

Adding Some DSA questions in JAVA #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions ArrayLists.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.util.ArrayList;
import java.util.Collections;
//also the two above libraries can be imported by import java.util.*
class ArrayLists {
public static void main(String args[]) {
ArrayList <Integer> list = new ArrayList<Integer>();
//add elements
list.add(1);
list.add(2);
list.add(3);
System.out.println(list);
//get elements
int element = list.get(0);
System.out.println(element);
//add element in between
list.add(1, 1);
System.out.println(list);
//set element
list.set(0, 5);
System.out.println(list);
//delete element
list.remove(3);
System.out.println(list);
//size
int size = list.size();
System.out.println(size);
//loops
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i));
}
System.out.println();
//sorting
Collections.sort(list);
System.out.println(list);
}
}
146 changes: 146 additions & 0 deletions BST.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
class Node {
int data;
Node left, right;

public Node(int item) {
data = item;
left = right = null;
}
}

public class BST {
private Node root;

public BST() {
root = null;
}

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;
}

public int minValue() {
return minValue(root);
}

private int minValue(Node root) {
int minv = root.data;
while (root.left != null) {
minv = root.left.data;
root = root.left;
}
return minv;
}

public int maxValue() {
return maxValue(root);
}

private int maxValue(Node root) {
int maxv = root.data;
while (root.right != null) {
maxv = root.right.data;
root = root.right;
}
return maxv;
}

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 void preorder() {
preorderRec(root);
}

private void preorderRec(Node root) {
if (root != null) {
System.out.print(root.data + " ");
preorderRec(root.left);
preorderRec(root.right);
}
}

public void postorder() {
postorderRec(root);
}

private void postorderRec(Node root) {
if (root != null) {
postorderRec(root.left);
postorderRec(root.right);
System.out.print(root.data + " ");
}
}

public static void main(String[] args) {
BST bst = new BST();
bst.insert(50);
bst.insert(30);
bst.insert(20);
bst.insert(40);
bst.insert(70);
bst.insert(60);
bst.insert(80);

System.out.println("Inorder traversal:");
bst.inorder();
System.out.println("\nPreorder traversal:");
bst.preorder();
System.out.println("\nPostorder traversal:");
bst.postorder();

System.out.println("\nMin value: " + bst.minValue());
System.out.println("Max value: " + bst.maxValue());

bst.delete(20);
System.out.println("\nInorder traversal after deleting 20:");
bst.inorder();
}
}
18 changes: 18 additions & 0 deletions BackTracking.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class BackTracking {
public static void printPermutation(String str, String perm, int idx) {
if (str.length() == 0) {
System.out.println(perm);
return;
}
for (int i = 0; i < str.length(); i++) {
char currChar = str.charAt(i);
String newStr = str.substring(0, i) + str.substring(i + 1);
printPermutation(newStr, perm + currChar, idx + 1);
}
}
public static void main(String args[]) {
String str = "ABC";
printPermutation(str, "", 0);
}
}
//time complexity = n * n!
44 changes: 44 additions & 0 deletions BinarySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
public class BinarySearch {

// Function to perform binary search
public static int binarySearch(int[] arr, int target) {
// Initialize left and right pointers
int left = 0;
int right = arr.length - 1;

// Iterate until left pointer is less than or equal to right pointer
while (left <= right) {
// Calculate the middle index
int mid = left + (right - left) / 2;

// If the target is found at middle index, return the index
if (arr[mid] == target) {
return mid;
}

// If target is greater, ignore left half
if (arr[mid] < target) {
left = mid + 1;
}
// If target is smaller, ignore right half
else {
right = mid - 1;
}
}

// If target is not found, return -1
return -1;
}

// Main method to test binary search
public static void main(String[] args) {
int[] arr = { 2, 5, 8, 12, 16, 23, 38, 56, 72, 91 };
int target = 23;
int result = binarySearch(arr, target);
if (result != -1) {
System.out.println("Element found at index: " + result);
} else {
System.out.println("Element not found in the array.");
}
}
}
22 changes: 22 additions & 0 deletions BubbleSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class BubbleSort {
public static void printArray(int arr[]) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();

}
public static void main(String args[]) {
int arr[] = {7,8,3,2,1};
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printArray(arr);
}
}
16 changes: 16 additions & 0 deletions CheckSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class CheckSortedArray {
public static boolean isSorted(int arr[], int idx) {
if (idx == arr.length - 1) {
return true;
}
if (arr[idx] < arr[idx+1]) {
return isSorted(arr,idx + 1);
} else {
return false;
}
}
public static void main(String args[]) {
int arr[] = {1, 3, 5, 4};
System.out.println(isSorted(arr, 0));
}
}
97 changes: 97 additions & 0 deletions CircularLinkedListInsertionDeletion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//class Node {
// int data;
// Node next;
//
// public Node(int data) {
// this.data = data;
// this.next = null;
// }
//}
//
//class CircularLinkedList {
// private Node head;
//
// public CircularLinkedList() {
// this.head = null;
// }
//
// // Insertion into the circular list
// public void insert(int data) {
// Node newNode = new Node(data);
// if (head == null) {
// head = newNode;
// head.next = head;
// } else {
// Node current = head;
// while (current.next != head) {
// current = current.next;
// }
// current.next = newNode;
// newNode.next = head;
// }
// }
//
// // Deletion by value from the circular list
// public void delete(int value) {
// if (head == null) {
// System.out.println("List is empty");
// return;
// }
//
// Node current = head;
// Node prev = null;
//
// // Traverse the list to find the node with the given value
// do {
// if (current.data == value) {
// if (prev != null) {
// prev.next = current.next;
// }
// // If the head is being deleted, update the head
// if (current == head) {
// head = head.next;
// }
// return;
// }
// prev = current;
// current = current.next;
// } while (current != head);
//
// System.out.println("Value not found in the list");
// }
//
// // Display the circular list
// public void display() {
// if (head == null) {
// System.out.println("List is empty");
// return;
// }
//
// Node current = head;
// do {
// System.out.print(current.data + " ");
// current = current.next;
// } while (current != head);
// System.out.println();
// }
//}
//
//public class CircularLinkedListInsertionDeletion {
// public static void main(String[] args) {
// CircularLinkedList list = new CircularLinkedList();
//
// // Insert elements into the circular list
// list.insert(10);
// list.insert(20);
// list.insert(30);
//
// // Display the circular list
// System.out.print("Circular List: ");
// list.display();
//
// // Delete an element from the circular list
// list.delete(20);
// System.out.print("List after deletion: ");
// list.display();
// }
//}
Loading