├── bin ├── App.class ├── BST.class ├── LL.class ├── Node.class ├── Graph.class ├── Queue.class ├── Dequeue.class ├── DoublyLL.class ├── Employee.class ├── HeapSort.class ├── LLQueue.class ├── LLStack.class ├── Solution.class ├── TreeNode.class └── CircularLL.class ├── src ├── Employee.class ├── Node.java ├── TreeNode.java ├── Item.java ├── Employee.java ├── LLQueue.java ├── LLStack.java ├── Queue.java ├── Stack.java ├── DoublyLL.java ├── Solution.java ├── HeapSort.java ├── CircularLL.java ├── Knapsack.java ├── Queens.java ├── Dequeue.java ├── Archive │ └── Graph.java ├── Graph.java ├── App.java ├── LL.java ├── MatGraph.java └── BST.java ├── .vscode └── settings.json └── README.md /bin/App.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvabaccio/lectures/HEAD/bin/App.class -------------------------------------------------------------------------------- /bin/BST.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvabaccio/lectures/HEAD/bin/BST.class -------------------------------------------------------------------------------- /bin/LL.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvabaccio/lectures/HEAD/bin/LL.class -------------------------------------------------------------------------------- /bin/Node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvabaccio/lectures/HEAD/bin/Node.class -------------------------------------------------------------------------------- /bin/Graph.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvabaccio/lectures/HEAD/bin/Graph.class -------------------------------------------------------------------------------- /bin/Queue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvabaccio/lectures/HEAD/bin/Queue.class -------------------------------------------------------------------------------- /bin/Dequeue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvabaccio/lectures/HEAD/bin/Dequeue.class -------------------------------------------------------------------------------- /bin/DoublyLL.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvabaccio/lectures/HEAD/bin/DoublyLL.class -------------------------------------------------------------------------------- /bin/Employee.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvabaccio/lectures/HEAD/bin/Employee.class -------------------------------------------------------------------------------- /bin/HeapSort.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvabaccio/lectures/HEAD/bin/HeapSort.class -------------------------------------------------------------------------------- /bin/LLQueue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvabaccio/lectures/HEAD/bin/LLQueue.class -------------------------------------------------------------------------------- /bin/LLStack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvabaccio/lectures/HEAD/bin/LLStack.class -------------------------------------------------------------------------------- /bin/Solution.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvabaccio/lectures/HEAD/bin/Solution.class -------------------------------------------------------------------------------- /bin/TreeNode.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvabaccio/lectures/HEAD/bin/TreeNode.class -------------------------------------------------------------------------------- /src/Employee.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvabaccio/lectures/HEAD/src/Employee.class -------------------------------------------------------------------------------- /bin/CircularLL.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvabaccio/lectures/HEAD/bin/CircularLL.class -------------------------------------------------------------------------------- /src/Node.java: -------------------------------------------------------------------------------- 1 | public class Node { 2 | int value; 3 | Node next; 4 | Node prev; 5 | 6 | public Node(int value) { 7 | this.value = value; 8 | } 9 | } -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "java.project.sourcePaths": ["src"], 3 | "java.project.outputPath": "bin", 4 | "java.project.referencedLibraries": [ 5 | "lib/**/*.jar" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /src/TreeNode.java: -------------------------------------------------------------------------------- 1 | public class TreeNode { 2 | int value; 3 | TreeNode left; 4 | TreeNode right; 5 | 6 | public TreeNode(int x) { 7 | this.value = x; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/Item.java: -------------------------------------------------------------------------------- 1 | public class Item { 2 | Integer value; 3 | Integer weight; 4 | Integer vbyw; 5 | 6 | Item(Integer value, Integer weight) { 7 | this.value = value; 8 | this.weight = weight; 9 | vbyw = value / weight; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/Employee.java: -------------------------------------------------------------------------------- 1 | public class Employee { 2 | public String name; 3 | public int age; 4 | public int salary; 5 | 6 | public Employee(String name, int age, int salary) { 7 | this.name = name; 8 | this.age = age; 9 | this.salary = salary; 10 | } 11 | } -------------------------------------------------------------------------------- /src/LLQueue.java: -------------------------------------------------------------------------------- 1 | public class LLQueue { 2 | Node front, rear; 3 | 4 | void enqueue(int value) { 5 | Node newNode = new Node(value); 6 | 7 | if (rear == null) { 8 | front = newNode; 9 | rear = newNode; 10 | return; 11 | } 12 | 13 | rear.next = newNode; 14 | rear = rear.next; 15 | } 16 | 17 | int dequeue() { 18 | if (front == null) { 19 | System.out.println("Queue Underflow!"); 20 | return Integer.MIN_VALUE; 21 | } 22 | Node temp = front; 23 | front = front.next; 24 | return temp.value; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/LLStack.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList; 2 | 3 | public class LLStack { 4 | LL list = new LL(); 5 | 6 | boolean isEmpty() { 7 | return list.head == null; 8 | } 9 | 10 | void push(int value) { 11 | list.insertAtPos(value, 0); 12 | } 13 | 14 | int pop() { 15 | if (isEmpty()) { 16 | System.out.println("Stack Underflow!"); 17 | return Integer.MIN_VALUE; 18 | } 19 | return list.deleteHead(); 20 | } 21 | 22 | int peek() { 23 | if (isEmpty()) { 24 | System.out.println("Stack Underflow!"); 25 | return Integer.MIN_VALUE; 26 | } 27 | return list.head.value; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Getting Started 2 | 3 | Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. 4 | 5 | ## Folder Structure 6 | 7 | The workspace contains two folders by default, where: 8 | 9 | - `src`: the folder to maintain sources 10 | - `lib`: the folder to maintain dependencies 11 | 12 | Meanwhile, the compiled output files will be generated in the `bin` folder by default. 13 | 14 | > If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. 15 | 16 | ## Dependency Management 17 | 18 | The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). 19 | -------------------------------------------------------------------------------- /src/Queue.java: -------------------------------------------------------------------------------- 1 | public class Queue { 2 | int front = -1; 3 | int rear = -1; 4 | int[] arr; 5 | int count = 0; 6 | 7 | Queue(int length) { 8 | arr = new int[length]; 9 | } 10 | 11 | void enqueue(int value) { 12 | if (count == arr.length) { 13 | System.out.println("Queue Overflow!"); 14 | return; 15 | } 16 | if (front == -1) 17 | front++; 18 | rear = (rear + 1) % arr.length; 19 | arr[rear] = value; 20 | count++; 21 | } 22 | 23 | int dequeue() { 24 | if (count == 0) { 25 | System.out.println("Queue Underflow!"); 26 | return Integer.MIN_VALUE; 27 | } 28 | int x = arr[front]; 29 | front = (front + 1) % arr.length; 30 | count--; 31 | if (count == 0) { 32 | front = -1; 33 | rear = -1; 34 | } 35 | return x; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Stack.java: -------------------------------------------------------------------------------- 1 | // public class Stack { 2 | // private int top = -1; 3 | // private int[] arr; 4 | // private int length = 5; 5 | 6 | // Stack() { 7 | // arr = new int[length]; 8 | // } 9 | 10 | // boolean isEmpty() { 11 | // return top == -1; 12 | // } 13 | 14 | // boolean isFull() { 15 | // return top == (arr.length - 1); 16 | // } 17 | 18 | // void push(int value) { 19 | // if (isFull()) { 20 | // length = length * 2; 21 | // int[] newArr = new int[length]; 22 | // for (int i = 0; i < arr.length; i++) { 23 | // newArr[i] = arr[i]; 24 | // } 25 | // arr = newArr; 26 | // } 27 | // arr[++top] = value; 28 | // } 29 | 30 | // int pop() { 31 | // if (isEmpty()) { 32 | // System.out.println("Stack Underflow!"); 33 | // return Integer.MIN_VALUE; 34 | // } 35 | // return arr[top--]; 36 | // } 37 | 38 | // void peek() { 39 | // if (isEmpty()) { 40 | // System.out.println("Stack Underflow!"); 41 | // return; 42 | // } 43 | // System.out.println(arr[top]); 44 | // } 45 | // } 46 | -------------------------------------------------------------------------------- /src/DoublyLL.java: -------------------------------------------------------------------------------- 1 | public class DoublyLL { 2 | Node head; 3 | Node tail; 4 | 5 | public void insert(int value) { 6 | if (head == null) { 7 | head = new Node(value); 8 | tail = head; 9 | return; 10 | } 11 | 12 | Node temp = head; 13 | 14 | while (temp.next != null) 15 | temp = temp.next; 16 | 17 | Node newNode = new Node(value); 18 | temp.next = newNode; 19 | newNode.prev = temp; 20 | tail = newNode; 21 | } 22 | 23 | public void printList() { 24 | Node temp = head; 25 | 26 | while (temp != null) { 27 | System.out.print(temp.value + " "); 28 | temp = temp.next; 29 | } 30 | System.out.println(); 31 | } 32 | 33 | public void printListRev() { 34 | Node temp = tail; 35 | 36 | while (temp != null) { 37 | System.out.print(temp.value + " "); 38 | temp = temp.prev; 39 | } 40 | System.out.println(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Arrays; 3 | import java.util.List; 4 | 5 | class Solution { 6 | static List res = new ArrayList(); 7 | 8 | public static char[] swap(char[] arr, int x, int y) { 9 | char temp = arr[x]; 10 | arr[x] = arr[y]; 11 | arr[y] = temp; 12 | 13 | return arr; 14 | } 15 | 16 | public List find_permutation(String S) { 17 | char[] arr = S.toCharArray(); 18 | backtrack(arr, 0, arr.length - 1); 19 | return res; 20 | } 21 | 22 | public static void backtrack(char[] arr, int start, int end) { 23 | if (start == end) { 24 | // for (char i : arr) 25 | // System.out.print(i); 26 | // System.out.println(); 27 | res.add(Arrays.toString(arr)); 28 | return; 29 | } 30 | for (int i = start; i <= end; i++) { 31 | arr = swap(arr, start, i); 32 | backtrack(arr, start + 1, end); 33 | arr = swap(arr, start, i); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /src/HeapSort.java: -------------------------------------------------------------------------------- 1 | class App { 2 | 3 | public static void heapSort(int[] arr) { 4 | int n = arr.length; 5 | 6 | for (int i = (n / 2) - 1; i >= 0; i--) { 7 | heapify(arr, n, i); 8 | } 9 | 10 | for (int i = n - 1; i > 0; i--) { 11 | int temp = arr[0]; 12 | arr[0] = arr[i]; 13 | arr[i] = temp; 14 | 15 | heapify(arr, i, 0); 16 | } 17 | } 18 | 19 | public static void heapify(int[] arr, int n, int i) { 20 | int maximum = i; 21 | int left = 2 * i + 1; 22 | int right = 2 * i + 2; 23 | 24 | if (left < n && arr[left] > arr[maximum]) { 25 | maximum = left; 26 | } 27 | 28 | if (right < n && arr[right] > arr[maximum]) { 29 | maximum = right; 30 | } 31 | 32 | // swap 33 | 34 | if (maximum != i) { 35 | int temp = arr[i]; 36 | arr[i] = arr[maximum]; 37 | arr[maximum] = temp; 38 | 39 | heapify(arr, n, maximum); 40 | } 41 | } 42 | 43 | public static void printArray(int[] arr) { 44 | for (int i = 0; i < arr.length; i++) { 45 | System.out.print(arr[i] + " "); 46 | } 47 | System.out.println(""); 48 | } 49 | 50 | public static void main(String[] args) throws Exception { 51 | int[] arr = { 10, 20, 3, 1, 0, 5, 2 }; 52 | 53 | printArray(arr); 54 | 55 | heapSort(arr); 56 | 57 | printArray(arr); 58 | } 59 | } 60 | 61 | class HeapSort { 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/CircularLL.java: -------------------------------------------------------------------------------- 1 | public class CircularLL { 2 | public Node head; 3 | 4 | public void insert(int value) { 5 | if (head == null) { 6 | head = new Node(value); 7 | head.next = head; 8 | return; 9 | } 10 | 11 | Node temp = head.next; 12 | 13 | while (temp.next != head) { 14 | temp = temp.next; 15 | } 16 | 17 | temp.next = new Node(value); 18 | temp.next.next = head; 19 | } 20 | 21 | public void delete(int pos) { 22 | if (head == null) 23 | return; 24 | 25 | if (pos == 0) { 26 | if (head.next == head) { 27 | head = null; 28 | return; 29 | } 30 | 31 | Node temp = head; 32 | 33 | while (temp.next != head) { 34 | temp = temp.next; 35 | } 36 | 37 | temp.next = temp.next.next; 38 | head = temp.next; 39 | return; 40 | } 41 | 42 | Node temp = head; 43 | for (int i = 0; i < pos - 1; i++) { 44 | if (temp.next == head) { 45 | return; 46 | } 47 | temp = temp.next; 48 | } 49 | 50 | if (temp.next != null) 51 | temp.next = temp.next.next; 52 | } 53 | 54 | public void printList() { 55 | Node temp = head; 56 | 57 | System.out.print(temp.value + " "); 58 | temp = temp.next; 59 | while (temp != head) { 60 | System.out.print(temp.value + " "); 61 | temp = temp.next; 62 | } 63 | System.out.println(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/Knapsack.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Comparator; 3 | 4 | public class Knapsack { 5 | public static Double fractional(Item[] items, double W) { 6 | Arrays.sort(items, new Comparator() { 7 | @Override 8 | public int compare(Item x, Item y) { 9 | return y.vbyw.compareTo(x.vbyw); 10 | } 11 | }); 12 | 13 | Double profit = 0.0; 14 | int i = 0; 15 | while (W > 0 && i < items.length) { 16 | profit += items[i].vbyw * Math.min(W, items[i].weight); 17 | W -= items[i].weight; 18 | i++; 19 | } 20 | 21 | return profit; 22 | } 23 | 24 | public static Double zeroOne(Item[] items, int ksWeight) { 25 | double[][] dp = new double[items.length + 1][ksWeight + 1]; 26 | 27 | for (int i = 0; i <= items.length; i++) { 28 | for (int w = 0; w <= ksWeight; w++) { 29 | if (i == 0 || w == 0) 30 | continue; 31 | 32 | if (w - items[i - 1].weight < 0) 33 | dp[i][w] = dp[i - 1][w]; 34 | else 35 | dp[i][w] = Math.max(dp[i - 1][w], dp[i - 1][w - items[i - 1].weight] + items[i - 1].value); 36 | 37 | System.out.print(dp[i][w] + " "); 38 | } 39 | System.out.println(); 40 | } 41 | 42 | return dp[items.length][ksWeight]; 43 | } 44 | } 45 | 46 | class Item { 47 | Integer value; 48 | Integer weight; 49 | Integer vbyw; 50 | 51 | Item(Integer value, Integer weight) { 52 | this.value = value; 53 | this.weight = weight; 54 | vbyw = value / weight; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/Queens.java: -------------------------------------------------------------------------------- 1 | public class Queens { 2 | // abstraction 3 | public boolean isSafe(int[][] board, int i, int j) { 4 | for (int row = 0; row < board.length; row++) { 5 | if (board[row][j] == 1) 6 | return false; 7 | } 8 | for (int col = 0; col < board.length; col++) { 9 | if (board[i][col] == 1) 10 | return false; 11 | } 12 | 13 | int row = i; 14 | int col = j; 15 | 16 | while (row >= 0 && col >= 0) { 17 | if (board[row][col] == 1) 18 | return false; 19 | row--; 20 | col--; 21 | } 22 | 23 | row = i; 24 | col = j; 25 | 26 | while (row >= 0 && col < board.length) { 27 | if (board[row][col] == 1) 28 | return false; 29 | row--; 30 | col++; 31 | } 32 | 33 | return true; 34 | } 35 | 36 | public boolean solve(int[][] board, int row) { 37 | if (row == board.length) 38 | return true; 39 | 40 | for (int i = 0; i < board.length; i++) { 41 | if (isSafe(board, row, i)) { 42 | board[row][i] = 1; 43 | if (solve(board, row + 1)) { 44 | return true; 45 | } 46 | 47 | board[row][i] = 0; 48 | } 49 | } 50 | return false; 51 | } 52 | 53 | public void print(int[][] board) { 54 | for (int i = 0; i < board.length; i++) { 55 | for (int j = 0; j < board.length; j++) { 56 | System.out.print(board[i][j] + " "); 57 | } 58 | System.out.println(); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/Dequeue.java: -------------------------------------------------------------------------------- 1 | public class Dequeue { 2 | Node front; 3 | Node rear; 4 | 5 | int size; 6 | 7 | public Dequeue() { 8 | size = 0; 9 | front = null; 10 | rear = null; 11 | } 12 | 13 | public boolean isEmpty() { 14 | return front == null; 15 | } 16 | 17 | public void insertFront(int val) { 18 | Node newNode = new Node(val); 19 | size++; 20 | if (isEmpty()) { 21 | front = newNode; 22 | rear = front; 23 | return; 24 | } 25 | 26 | newNode.next = front; 27 | front = newNode; 28 | } 29 | 30 | public void insertRear(int val) { 31 | Node newNode = new Node(val); 32 | size++; 33 | if (this.isEmpty()) { 34 | front = newNode; 35 | rear = front; 36 | return; 37 | } 38 | 39 | rear.next = newNode; 40 | rear = newNode; 41 | } 42 | 43 | public void removeFront() { 44 | if (isEmpty()) { 45 | System.out.println("Queue Underflow"); 46 | return; 47 | } 48 | size--; 49 | front = front.next; 50 | 51 | if (front == null) 52 | rear = null; 53 | } 54 | 55 | public void removeRear() { 56 | if (isEmpty()) { 57 | System.out.println("Queue Underflow"); 58 | return; 59 | } 60 | 61 | if (size == 1) { 62 | front = null; 63 | rear = null; 64 | size--; 65 | return; 66 | } 67 | 68 | size--; 69 | Node temp = front; 70 | 71 | while (temp.next != null && temp.next.next != null) { 72 | temp = temp.next; 73 | } 74 | 75 | rear = temp; 76 | rear.next = null; 77 | } 78 | 79 | public void printList() { 80 | Node temp = front; 81 | 82 | while (temp != null) { 83 | System.out.print(temp.value + " "); 84 | temp = temp.next; 85 | } 86 | System.out.println(); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/Archive/Graph.java: -------------------------------------------------------------------------------- 1 | package Archive; 2 | 3 | import java.util.ArrayList; 4 | import java.util.LinkedList; 5 | import java.util.Queue; 6 | import java.util.Stack; 7 | 8 | public class Graph { 9 | public int vertices; 10 | public ArrayList[] adjList; 11 | 12 | Graph(int v) { 13 | this.vertices = v; 14 | adjList = new ArrayList[v]; 15 | 16 | for (int i = 0; i < v; i++) 17 | adjList[i] = new ArrayList(); 18 | } 19 | 20 | void addEdge(int v, int w) { 21 | adjList[v].add(w); 22 | adjList[w].add(v); 23 | } 24 | 25 | void BFS(int x) { 26 | boolean[] visited = new boolean[vertices]; 27 | 28 | Queue queue = new LinkedList(); 29 | 30 | queue.offer(x); 31 | visited[x] = true; 32 | 33 | while (!queue.isEmpty()) { 34 | x = queue.poll(); 35 | System.out.println(x); 36 | 37 | for (int i = 0; i < adjList[x].size(); i++) { 38 | int n = adjList[x].get(i); 39 | if (!visited[n]) { 40 | visited[n] = true; 41 | queue.offer(n); 42 | } 43 | } 44 | } 45 | } 46 | 47 | void DFSRecursive(int x, boolean[] visited) { 48 | visited[x] = true; 49 | System.out.println(x); 50 | 51 | for (int i = 0; i < adjList[x].size(); i++) { 52 | int n = adjList[x].get(i); 53 | if (!visited[n]) { 54 | DFSRecursive(n, visited); 55 | } 56 | } 57 | } 58 | 59 | void DFS(int x) { 60 | boolean[] visited = new boolean[vertices]; 61 | 62 | Stack stack = new Stack(); 63 | 64 | stack.push(x); 65 | 66 | while (!stack.isEmpty()) { 67 | x = stack.pop(); 68 | 69 | if (!visited[x]) { 70 | visited[x] = true; 71 | System.out.println(x); 72 | } 73 | 74 | for (int i = 0; i < adjList[x].size(); i++) { 75 | int n = adjList[x].get(i); 76 | if (!visited[n]) { 77 | stack.push(n); 78 | } 79 | } 80 | } 81 | } 82 | } -------------------------------------------------------------------------------- /src/Graph.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.HashMap; 3 | import java.util.LinkedList; 4 | import java.util.Set; 5 | import java.util.Stack; 6 | import java.util.Queue; 7 | 8 | public class Graph { 9 | HashMap> g; 10 | 11 | public Graph() { 12 | g = new HashMap<>(); 13 | } 14 | 15 | public void addEgde(char x, char y) { 16 | ArrayList arr = g.getOrDefault(x, new ArrayList<>()); 17 | arr.add(y); 18 | g.put(x, arr); 19 | 20 | arr = g.getOrDefault(y, new ArrayList<>()); 21 | arr.add(x); 22 | g.put(y, arr); 23 | } 24 | 25 | public void DFS(char x) { 26 | HashMap visited = new HashMap<>(); 27 | 28 | for (Character key : g.keySet()) { 29 | visited.put(key, false); 30 | } 31 | 32 | Stack stack = new Stack<>(); 33 | 34 | stack.push(x); 35 | visited.put(x, true); 36 | 37 | while (!stack.isEmpty()) { // while the stack is not empty 38 | char curr = stack.pop(); 39 | 40 | System.out.print(curr + " "); 41 | // visited.put(curr, true); 42 | 43 | for (int j = 0; j < g.get(curr).size(); j++) { 44 | char tmp = g.get(curr).get(j); 45 | if (!visited.get(tmp)) { 46 | stack.push(tmp); 47 | visited.put(tmp, true); 48 | } 49 | } 50 | } 51 | } 52 | 53 | public void BFS(char x) { 54 | HashMap visited = new HashMap<>(); 55 | 56 | for (Character key : g.keySet()) { 57 | visited.put(key, false); 58 | } 59 | 60 | Queue q = new LinkedList(); 61 | 62 | q.offer(x); 63 | visited.put(x, true); 64 | 65 | while (!q.isEmpty()) { 66 | char curr = q.poll(); 67 | 68 | System.out.print(curr + " "); 69 | // visited.put(curr, true); 70 | 71 | for (int j = 0; j < g.get(curr).size(); j++) { 72 | char tmp = g.get(curr).get(j); 73 | if (!visited.get(tmp)) { 74 | q.offer(tmp); 75 | visited.put(tmp, true); 76 | } 77 | } 78 | } 79 | } 80 | 81 | public void DFS(char x, HashMap visited) { 82 | if (visited == null) { 83 | visited = new HashMap<>(); 84 | 85 | for (Character key : g.keySet()) { 86 | visited.put(key, false); 87 | } 88 | } 89 | 90 | System.out.print(x + " "); 91 | visited.put(x, true); 92 | 93 | for (int j = 0; j < g.get(x).size(); j++) { 94 | char tmp = g.get(x).get(j); 95 | if (!visited.get(tmp)) { 96 | DFS(tmp, visited); // stack.push(tmp); 97 | } 98 | } 99 | } 100 | 101 | public void printGraph() { 102 | for (Character key : g.keySet()) { 103 | System.out.print(key + ": "); 104 | for (int j = 0; j < g.get(key).size(); j++) { 105 | System.out.print(g.get(key).get(j) + " "); 106 | } 107 | System.out.println(); 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /src/App.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class App { 4 | 5 | public static void removeCycle(Node head, Node fast) { 6 | Node s = fast.next; 7 | int count = 1; 8 | 9 | while (s != fast) { 10 | s = s.next; 11 | count++; 12 | } 13 | 14 | Node y = head; 15 | 16 | for (int i = 0; i < count; i++) 17 | y = y.next; 18 | 19 | Node x = head; 20 | 21 | while (y != x) { 22 | x = x.next; 23 | y = y.next; 24 | } 25 | 26 | for (int i = 0; i < count - 1; i++) 27 | y = y.next; 28 | 29 | y.next = null; 30 | } 31 | 32 | public static boolean detectAndRemove(Node head) { 33 | Node slow = head; 34 | Node fast = head; 35 | 36 | while (fast != null && fast.next != null) { 37 | slow = slow.next; 38 | fast = fast.next.next; 39 | 40 | if (slow == fast) { 41 | removeCycle(head, fast); 42 | return true; 43 | } 44 | } 45 | 46 | return false; 47 | } 48 | 49 | public static void printList(Node head) { 50 | Node temp = head; 51 | while (temp != null) { 52 | System.out.print(temp.value + " "); 53 | temp = temp.next; 54 | } 55 | } 56 | 57 | public static void callsItself() { 58 | System.out.println("Hello"); 59 | callsItself(); 60 | } 61 | 62 | public static int fact(int x) { 63 | if (x == 1) 64 | return 1; // base case 65 | return x * fact(x - 1); 66 | } 67 | 68 | public static int fib(int x) { 69 | if (x <= 1) { 70 | return x; 71 | } 72 | return fib(x - 2) + fib(x - 1); 73 | } 74 | 75 | public static int fibIterative(int x) { 76 | if (x <= 1) { 77 | return x; 78 | } 79 | int a = 0; 80 | int b = 1; 81 | for (int i = 2; i <= x; i++) { 82 | int temp = b; 83 | b = b + a; 84 | a = temp; 85 | } 86 | return b; 87 | } 88 | 89 | static int hcf(int a, int b) { 90 | if (b == 0) { 91 | return a; 92 | } 93 | return hcf(b, a % b); 94 | } 95 | 96 | public static int minSteps(int n) { 97 | if (n == 1) 98 | return 0; 99 | 100 | int div3 = Integer.MAX_VALUE; 101 | int div2 = Integer.MAX_VALUE; 102 | int sub1 = Integer.MAX_VALUE; 103 | 104 | if (n % 3 == 0) { 105 | div3 = 1 + minSteps(n / 3); 106 | } else if (n % 2 == 0) { 107 | div2 = 1 + minSteps(n / 2); 108 | } else { 109 | sub1 = 1 + minSteps(n - 1); 110 | } 111 | 112 | return Math.min(sub1, Math.min(div3, div2)); 113 | } 114 | 115 | public static void main(String[] args) { 116 | BST tree = new BST(); 117 | 118 | Scanner sc = new Scanner(System.in); 119 | 120 | while (sc.hasNext()) { 121 | switch (sc.nextInt()) { 122 | case 1: 123 | tree.insert(sc.nextInt()); 124 | break; 125 | case 2: 126 | tree.inOrder(tree.root); 127 | break; 128 | case 3: 129 | tree.postOrder(tree.root); 130 | break; 131 | case 4: 132 | tree.preOrder(tree.root); 133 | break; 134 | case 5: 135 | tree.root = tree.delete(tree.root, sc.nextInt()); 136 | break; 137 | } 138 | } 139 | } 140 | } -------------------------------------------------------------------------------- /src/LL.java: -------------------------------------------------------------------------------- 1 | import java.util.Stack; 2 | 3 | public class LL { 4 | Node head; 5 | 6 | public Node reverse(Node head) { 7 | if (head == null || head.next == null) 8 | return head; 9 | 10 | Node newNode = reverse(head.next); 11 | 12 | head.next.next = head; 13 | head.next = null; 14 | 15 | return newNode; 16 | } 17 | 18 | public void reverseStack() { 19 | Stack st = new Stack<>(); 20 | 21 | while (head != null) { 22 | Node x = head; 23 | head = head.next; 24 | x.next = null; 25 | st.push(x); 26 | } 27 | 28 | Node dummy = new Node(-1); 29 | Node temp = dummy; 30 | 31 | while (!st.isEmpty()) { 32 | temp.next = st.pop(); 33 | temp = temp.next; 34 | } 35 | 36 | head = dummy.next; 37 | } 38 | 39 | public void insert(int value) { 40 | if (head == null) { 41 | head = new Node(value); 42 | return; 43 | } 44 | 45 | Node temp = head; 46 | 47 | while (temp.next != null) { 48 | temp = temp.next; 49 | } 50 | 51 | temp.next = new Node(value); 52 | } 53 | 54 | public void insertAtPos(int value, int pos) { 55 | if (head == null) { 56 | head = new Node(value); 57 | return; 58 | } 59 | 60 | if (pos == 0) { 61 | Node newNode = new Node(value); 62 | newNode.next = head; 63 | head = newNode; 64 | return; 65 | } 66 | 67 | Node temp = head; 68 | 69 | for (int i = 0; i < pos - 1; i++) { 70 | if (temp.next == null) { 71 | break; 72 | } 73 | temp = temp.next; 74 | } 75 | 76 | Node newNode = new Node(value); 77 | 78 | newNode.next = temp.next; 79 | temp.next = newNode; 80 | } 81 | 82 | public void delete(int pos) { 83 | if (head == null) 84 | return; 85 | 86 | if (pos == 0) { 87 | head = head.next; 88 | return; 89 | } 90 | 91 | Node temp = head; 92 | 93 | for (int i = 0; i < pos - 1; i++) { 94 | if (temp.next == null) { 95 | return; 96 | } 97 | temp = temp.next; 98 | } 99 | 100 | if (temp.next != null) 101 | temp.next = temp.next.next; 102 | } 103 | 104 | public Node middle() { 105 | if (head == null) 106 | return head; 107 | 108 | Node slow = head; 109 | Node fast = head; 110 | 111 | while (fast.next != null && fast.next.next != null) { 112 | slow = slow.next; 113 | fast = fast.next.next; 114 | } 115 | 116 | return slow; 117 | } 118 | 119 | public void removeNthFromEnd(int n) { 120 | Node dummy = new Node(1); 121 | dummy.next = head; 122 | 123 | Node nth = head; 124 | 125 | for (int i = 0; i < n; i++) 126 | head = head.next; 127 | 128 | Node prev = dummy; 129 | 130 | while (head != null) { 131 | prev = nth; 132 | nth = nth.next; 133 | head = head.next; 134 | } 135 | prev.next = nth.next; 136 | head = dummy.next; 137 | } 138 | 139 | public int deleteHead() { 140 | int x = head.value; 141 | head = head.next; 142 | return x; 143 | } 144 | 145 | public void printList() { 146 | Node temp = head; 147 | 148 | while (temp != null) { 149 | System.out.print(temp.value + " -> "); 150 | temp = temp.next; 151 | } 152 | System.out.println(); 153 | } 154 | } 155 | -------------------------------------------------------------------------------- /src/MatGraph.java: -------------------------------------------------------------------------------- 1 | import java.util.Stack; 2 | import java.util.Queue; 3 | import java.util.Arrays; 4 | import java.util.LinkedList; 5 | 6 | public class MatGraph { 7 | int n; 8 | int[][] graph; 9 | 10 | MatGraph(int size) { 11 | n = size; 12 | graph = new int[n][n]; 13 | } 14 | 15 | void addEgde(int x, int y) { 16 | graph[x][y] = 1; 17 | graph[y][x] = 1; 18 | } 19 | 20 | void addEdgeDir(int x, int y) { 21 | graph[x][y] = 1; 22 | } 23 | 24 | void addEdge(int x, int y, int w) { 25 | graph[x][y] = w; 26 | graph[y][x] = w; 27 | } 28 | 29 | void addEdgeDir(int x, int y, int w) { 30 | graph[x][y] = w; 31 | } 32 | 33 | void DFS(int start) { 34 | boolean[] visited = new boolean[n]; 35 | Stack s = new Stack(); 36 | 37 | s.push(start); 38 | 39 | while (!s.isEmpty()) { 40 | int curr = s.pop(); 41 | 42 | if (!visited[curr]) { 43 | visited[curr] = true; 44 | System.out.print(curr + " "); 45 | } 46 | 47 | int[] adj = graph[curr]; 48 | 49 | for (int i = 0; i < adj.length; i++) { 50 | if (adj[i] == 1 && !visited[i]) { 51 | s.push(curr); 52 | s.push(i); 53 | break; 54 | } 55 | } 56 | } 57 | } 58 | 59 | void DFS(int start, boolean[] visited) { 60 | if (!visited[start]) { 61 | visited[start] = true; 62 | System.out.print(start + " "); 63 | } 64 | 65 | int[] adj = graph[start]; 66 | 67 | for (int i = 0; i < adj.length; i++) { 68 | if (adj[i] == 1 && !visited[i]) { 69 | DFS(i, visited); 70 | } 71 | } 72 | } 73 | 74 | void BFS(int start) { 75 | Queue q = new LinkedList(); 76 | boolean[] visited = new boolean[n]; 77 | 78 | q.offer(start); 79 | 80 | while (!q.isEmpty()) { 81 | int curr = q.poll(); 82 | 83 | if (!visited[curr]) { 84 | visited[curr] = true; 85 | System.out.print(curr + " "); 86 | } 87 | 88 | int[] adj = graph[curr]; 89 | 90 | for (int i = 0; i < adj.length; i++) { 91 | if (adj[i] == 1 && !visited[i]) { 92 | q.offer(i); 93 | } 94 | } 95 | } 96 | } 97 | 98 | void djikstra(int start) { 99 | int[] minDist = new int[n]; 100 | boolean[] fixed = new boolean[n]; 101 | 102 | for (int i = 0; i < n; i++) 103 | minDist[i] = Integer.MAX_VALUE; 104 | 105 | minDist[start] = 0; 106 | 107 | for (int i = 0; i < n - 1; i++) { 108 | // find min val 109 | int minVal = Integer.MAX_VALUE; 110 | int minIndex = -1; 111 | 112 | for (int j = 0; j < n; j++) { 113 | if (!fixed[j] && minDist[j] < minVal) { 114 | minVal = minDist[j]; 115 | minIndex = j; 116 | } 117 | } 118 | 119 | fixed[minIndex] = true; 120 | 121 | for (int j = 0; j < n; j++) { 122 | if (!fixed[j] && graph[minIndex][j] > 0 && minDist[minIndex] != Integer.MAX_VALUE 123 | && minDist[j] > minDist[minIndex] + graph[minIndex][j]) { 124 | minDist[j] = minDist[minIndex] + graph[minIndex][j]; 125 | } 126 | } 127 | } 128 | 129 | for (int x : minDist) 130 | System.out.print(x + " "); 131 | } 132 | 133 | void printGraph() { 134 | for (int[] x : graph) { 135 | for (int y : x) 136 | System.out.print(y + " "); 137 | System.out.println(); 138 | } 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /src/BST.java: -------------------------------------------------------------------------------- 1 | import java.util.Stack; 2 | import java.util.Queue; 3 | import java.util.ArrayList; 4 | import java.util.LinkedList; 5 | 6 | public class BST { 7 | TreeNode root; 8 | 9 | public void insert(int val) { 10 | if (root == null) { 11 | root = new TreeNode(val); 12 | return; 13 | } 14 | 15 | TreeNode temp = root; 16 | TreeNode prev = null; 17 | 18 | while (temp != null) { 19 | prev = temp; 20 | if (val < temp.value) { 21 | temp = temp.left; 22 | } else {// if (val > temp.value) { 23 | temp = temp.right; 24 | } 25 | } 26 | 27 | if (prev.value > val) { 28 | prev.left = new TreeNode(val); 29 | } else { 30 | prev.right = new TreeNode(val); 31 | } 32 | } 33 | 34 | public TreeNode delete(TreeNode root, int target) { 35 | if (root == null) 36 | return root; 37 | 38 | if (target > root.value) { 39 | root.right = delete(root.right, target); 40 | } else if (target < root.value) { 41 | root.left = delete(root.left, target); 42 | } else { 43 | if (root.left == null) { 44 | return root.right; 45 | } else if (root.right == null) { 46 | return root.left; 47 | } 48 | 49 | root.value = inOrderSuccessor(root.right); 50 | 51 | root.right = delete(root.right, root.value); 52 | } 53 | 54 | return root; 55 | } 56 | 57 | public void inOrder(TreeNode root) { 58 | if (root != null) { 59 | inOrder(root.left); 60 | System.out.println(root.value); 61 | inOrder(root.right); 62 | } 63 | } 64 | 65 | public void inOrder() { 66 | if (root == null) 67 | return; 68 | Stack s = new Stack(); 69 | 70 | TreeNode temp = root; 71 | 72 | while (temp != null || !s.isEmpty()) { 73 | while (temp != null) { 74 | s.push(temp); 75 | temp = temp.left; 76 | } 77 | 78 | temp = s.pop(); 79 | System.out.println(temp.value + " "); 80 | temp = temp.right; 81 | } 82 | } 83 | 84 | public void postOrder(TreeNode root) { 85 | if (root != null) { 86 | postOrder(root.left); 87 | postOrder(root.right); 88 | System.out.println(root.value); 89 | } 90 | } 91 | 92 | public void postOrder() { 93 | if (root == null) 94 | return; 95 | 96 | Stack s = new Stack(); 97 | Stack s2 = new Stack(); 98 | s.push(root); 99 | 100 | while (!s.isEmpty()) { 101 | TreeNode curr = s.pop(); 102 | s2.push(curr); 103 | 104 | if (curr.left != null) 105 | s.push(curr.left); 106 | if (curr.right != null) 107 | s.push(curr.right); 108 | } 109 | 110 | while (!s2.isEmpty()) 111 | System.out.println(s2.pop().value); 112 | } 113 | 114 | public void preOrder(TreeNode root) { 115 | if (root != null) { 116 | System.out.println(root.value); 117 | preOrder(root.left); 118 | preOrder(root.right); 119 | } 120 | } 121 | 122 | public void preOrder() { 123 | if (root == null) 124 | return; 125 | 126 | Stack s = new Stack(); 127 | s.push(root); 128 | 129 | while (!s.isEmpty()) { 130 | TreeNode curr = s.pop(); 131 | System.out.println(curr.value + " "); 132 | 133 | if (curr.right != null) 134 | s.push(curr.right); 135 | if (curr.left != null) 136 | s.push(curr.left); 137 | } 138 | } 139 | 140 | public void levelOrder() { 141 | Queue q = new LinkedList(); 142 | q.offer(root); 143 | 144 | while (!q.isEmpty()) { 145 | // double sum = 0; 146 | int size = q.size(); 147 | // for (int i = 0; i < size; i++) { 148 | TreeNode front = q.poll(); 149 | System.out.print(front.value + " "); 150 | if (front.left != null) 151 | q.offer(front.left); 152 | if (front.right != null) 153 | q.offer(front.right); 154 | 155 | // } 156 | // System.out.println(""); 157 | } 158 | } 159 | 160 | public int inOrderSuccessor(TreeNode node) { 161 | int succ = node.value; 162 | while (node.left != null) { 163 | succ = node.left.value; 164 | node = node.left; 165 | } 166 | return succ; 167 | } 168 | } 169 | --------------------------------------------------------------------------------