├── Linked List ├── Set ├── Get ├── Remove first ├── prepend ├── Reverse ├── Remove ├── Insert ├── print list ├── Linked-List.java ├── Aappend.java ├── remove last.java └── all Linked List.java ├── Stacks&Queues ├── pop ├── push ├── enqueue ├── dequeue ├── constructer └── Queue constructer ├── Doubly Linked Lists ├── set ├── Append ├── prepend ├── Remove first ├── Remove last ├── remove ├── get ├── insert └── constructer ├── Hash Table ├── Get ├── Keys ├── Hash Table intervew q ├── Set ├── constructer └── Hash Method ├── Recursion ├── Factorial └── call stack ├── Graph ├── Add Edge ├── Remove Vertex ├── Remove Edge └── Add Vertex ├── Time Complexity └── O(n) ├── Binary Search Tree ├── constructer └── Insert ├── cookie ├── POINTERS └── POINTERS.java ├── Dynamic Programming ├── fibonacci seq ├── Bottom Up └── Memoization ├── Basic Sorts ├── Insertion Sort ├── Bubble sort ├── Selection sort ├── QUICK sort │ ├── pivot │ └── quick sort code └── Merge Sort └── DFS ├── code ├── DFS post order └── Inorder /Linked List/Set: -------------------------------------------------------------------------------- 1 | public boolean set(int index, int value) { 2 | Node temp = get(index); 3 | if (temp != null) { 4 | temp.value = value; 5 | return true; 6 | } 7 | return false; 8 | } 9 | -------------------------------------------------------------------------------- /Stacks&Queues/pop: -------------------------------------------------------------------------------- 1 | public Node pop() { 2 | if(height == 0) return null; 3 | 4 | Node temp = top; 5 | top = top.next; 6 | temp.next = null; 7 | 8 | height--; 9 | return temp; 10 | } 11 | -------------------------------------------------------------------------------- /Linked List/Get: -------------------------------------------------------------------------------- 1 | public Node get(int index) { 2 | if (index < 0 || index >= length) return null; 3 | Node temp = head; 4 | for(int i = 0; i < index; i++) { 5 | temp = temp.next; 6 | } 7 | return temp; 8 | } 9 | -------------------------------------------------------------------------------- /Doubly Linked Lists/set: -------------------------------------------------------------------------------- 1 | 2 | public boolean set(int index, int value) { 3 | Node temp = get(index); 4 | if(temp != null) { 5 | temp.value = value; 6 | return true; 7 | } 8 | return false; 9 | } 10 | -------------------------------------------------------------------------------- /Hash Table/Get: -------------------------------------------------------------------------------- 1 | 2 | public int get(String key) { 3 | int index = hash(key); 4 | Node temp = dataMap[index]; 5 | while (temp != null) { 6 | if (temp.key == key) return temp.value; 7 | temp = temp.next; 8 | } 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /Stacks&Queues/push: -------------------------------------------------------------------------------- 1 | public void push(int value) { 2 | Node newNode = new Node(value); 3 | if(height == 0) { 4 | top = newNode; 5 | } else { 6 | newNode.next = top; 7 | top = newNode; 8 | } 9 | height++; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /Linked List/Remove first: -------------------------------------------------------------------------------- 1 | public Node removeFirst() { 2 | if (length == 0) return null; 3 | Node temp = head; 4 | head = head.next; 5 | temp.next = null; 6 | length--; 7 | if (length == 0) { 8 | tail = null; 9 | } 10 | return temp; 11 | } 12 | -------------------------------------------------------------------------------- /Recursion/Factorial: -------------------------------------------------------------------------------- 1 | public class Factorial { 2 | 3 | public static int factorial(int n) { 4 | if (n == 1) return 1; 5 | return n * factorial(n - 1); 6 | } 7 | 8 | 9 | public static void main(String[] args) { 10 | 11 | System.out.println( factorial(4) ); 12 | 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Stacks&Queues/enqueue: -------------------------------------------------------------------------------- 1 | public void enqueue(int value) { 2 | Node newNode = new Node(value); 3 | if (length == 0) { 4 | first = newNode; 5 | last = newNode; 6 | } else { 7 | last.next = newNode; 8 | last = newNode; 9 | } 10 | length++; 11 | } 12 | -------------------------------------------------------------------------------- /Graph/Add Edge: -------------------------------------------------------------------------------- 1 | public boolean addEdge(String vertex1, String vertex2) { 2 | if (adjList.get(vertex1) != null && adjList.get(vertex2) != null) { 3 | adjList.get(vertex1).add(vertex2); 4 | adjList.get(vertex2).add(vertex1); 5 | return true; 6 | } 7 | return false; 8 | } 9 | 10 | -------------------------------------------------------------------------------- /Linked List/prepend: -------------------------------------------------------------------------------- 1 | 2 | public void prepend(int value) { 3 | Node newNode = new Node(value); 4 | if (length == 0) { 5 | head = newNode; 6 | tail = newNode; 7 | } else { 8 | newNode.next = head; 9 | head = newNode; 10 | } 11 | length++; 12 | } 13 | -------------------------------------------------------------------------------- /Graph/Remove Vertex: -------------------------------------------------------------------------------- 1 | 2 | public boolean removeVertex(String vertex) { 3 | if (adjList.get(vertex) == null) return false; 4 | for (String otherVertex : adjList.get(vertex)) { 5 | adjList.get(otherVertex).remove(vertex); 6 | } 7 | adjList.remove(vertex); 8 | return true; 9 | } 10 | 11 | -------------------------------------------------------------------------------- /Graph/Remove Edge: -------------------------------------------------------------------------------- 1 | 2 | public boolean removeEdge(String vertex1, String vertex2) { 3 | if (adjList.get(vertex1) != null && adjList.get(vertex2) != null) { 4 | adjList.get(vertex1).remove(vertex2); 5 | adjList.get(vertex2).remove(vertex1); 6 | return true; 7 | } 8 | return false; 9 | } 10 | -------------------------------------------------------------------------------- /Stacks&Queues/dequeue: -------------------------------------------------------------------------------- 1 | public Node dequeue() { 2 | if(length == 0) return null; 3 | Node temp = first; 4 | if(length == 1) { 5 | first = null; 6 | last = null; 7 | } else { 8 | first = first.next; 9 | temp.next = null; 10 | } 11 | length--; 12 | return temp; 13 | } 14 | -------------------------------------------------------------------------------- /Doubly Linked Lists/Append: -------------------------------------------------------------------------------- 1 | public void append (int value) { 2 | Node newNode = new Node(value); 3 | if(length == 0) { 4 | head = newNode; 5 | tail = newNode; 6 | } else { 7 | tail.next = newNode; 8 | newNode.prev = tail; 9 | tail = newNode; 10 | } 11 | length++; 12 | } 13 | -------------------------------------------------------------------------------- /Doubly Linked Lists/prepend: -------------------------------------------------------------------------------- 1 | public void prepend(int value) { 2 | Node newNode = new Node(value); 3 | if(length == 0) { 4 | head = newNode; 5 | tail = newNode; 6 | } else { 7 | newNode.next = head; 8 | head.prev = newNode; 9 | head = newNode; 10 | } 11 | length++; 12 | } 13 | -------------------------------------------------------------------------------- /Time Complexity/O(n): -------------------------------------------------------------------------------- 1 | 2 | package course; 3 | import java.util.*; 4 | public class Course { 5 | 6 | public static void printItems(int n) { 7 | 8 | for(int i=0;i allKeys = new ArrayList<>(); 4 | for (int i = 0; i < dataMap.length; i++) { 5 | Node temp = dataMap[i]; 6 | while (temp != null) { 7 | allKeys.add(temp.key); 8 | temp = temp.next; 9 | } 10 | } 11 | return allKeys; 12 | } 13 | -------------------------------------------------------------------------------- /Doubly Linked Lists/Remove first: -------------------------------------------------------------------------------- 1 | public Node removeFirst() { 2 | if(length == 0) return null; 3 | Node temp = head; 4 | if(length == 1) { 5 | head = null; 6 | tail = null; 7 | } else { 8 | head = head.next; 9 | head.prev = null; 10 | temp.next = null; 11 | } 12 | length--; 13 | return temp; 14 | } 15 | -------------------------------------------------------------------------------- /cookie: -------------------------------------------------------------------------------- 1 | 2 | package course ; 3 | import java.util.*; 4 | 5 | public class Course{ 6 | public class Cookie { 7 | private String color; 8 | 9 | public Cookie(String color) { 10 | this.color = color; 11 | } 12 | public String getColor() { 13 | return color; 14 | } 15 | public void setColor(String color) { 16 | this.color = color; 17 | } 18 | 19 | }} -------------------------------------------------------------------------------- /Doubly Linked Lists/Remove last: -------------------------------------------------------------------------------- 1 | 2 | public Node removeLast() { 3 | if(length == 0) return null; 4 | Node temp = tail; 5 | if (length == 1) { 6 | head = null; 7 | tail = null; 8 | } else { 9 | tail = tail.prev; 10 | tail.next = null; 11 | temp.prev = null; 12 | } 13 | length--; 14 | return temp; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /Linked List/Remove: -------------------------------------------------------------------------------- 1 | public Node remove(int index) { 2 | if (index < 0 || index >= length) return null; 3 | if (index == 0) return removeFirst(); 4 | if (index == length - 1) return removeLast(); 5 | 6 | Node prev = get(index - 1); 7 | Node temp = prev.next; 8 | 9 | prev.next = temp.next; 10 | temp.next = null; 11 | length--; 12 | return temp; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /POINTERS/POINTERS.java: -------------------------------------------------------------------------------- 1 | 2 | package course ; 3 | import java.util.*; 4 | 5 | public class Course{ 6 | public static void main(String[] args) { 7 | HashMap map1=new HashMap<>(); 8 | HashMap map2=new HashMap<>(); 9 | map1.put("value",11); 10 | map2= map1; 11 | map1.put("value",22); 12 | System.out.println(map1); 13 | System.out.println(map2); 14 | } 15 | } 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /Doubly Linked Lists/remove: -------------------------------------------------------------------------------- 1 | public Node remove(int index) { 2 | if(index < 0 || index >= length) return null; 3 | if(index == 0) return removeFirst(); 4 | if(index == length - 1) return removeLast(); 5 | 6 | Node temp = get(index); 7 | 8 | temp.next.prev = temp.prev; 9 | temp.prev.next = temp.next; 10 | temp.next = null; 11 | temp.prev = null; 12 | 13 | length--; 14 | return temp; 15 | } 16 | -------------------------------------------------------------------------------- /Recursion/call stack: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void methodThree() { 4 | System.out.println("Three"); 5 | } 6 | 7 | public static void methodTwo() { 8 | methodThree(); 9 | System.out.println("Two"); 10 | } 11 | 12 | public static void methodOne() { 13 | methodTwo(); 14 | System.out.println("One"); 15 | } 16 | 17 | public static void main(String[] args) { 18 | methodOne(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Doubly Linked Lists/get: -------------------------------------------------------------------------------- 1 | public Node get(int index) { 2 | if (index < 0 || index >= length) return null; 3 | Node temp = head; 4 | if (index < length/2) { 5 | for (int i = 0; i < index; i++) { 6 | temp = temp.next; 7 | } 8 | } else { 9 | temp = tail; 10 | for (int i = length - 1; i > index; i--) { 11 | temp = temp.prev; 12 | } 13 | } 14 | return temp; 15 | } 16 | -------------------------------------------------------------------------------- /Dynamic Programming/fibonacci seq: -------------------------------------------------------------------------------- 1 | public class Fib_No_Memo { 2 | 3 | static int counter = 0; 4 | 5 | public static int fib(int n) { 6 | counter++; 7 | 8 | if (n == 0 || n == 1) { 9 | return n; 10 | } 11 | return fib(n - 1) + fib(n - 2); 12 | } 13 | 14 | 15 | 16 | public static void main(String[] args) { 17 | 18 | int n = 7; 19 | 20 | System.out.println("\nFib of " + n + " = " + fib(n)); 21 | 22 | System.out.println("\nCounter: " + counter); 23 | 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Linked List/Insert: -------------------------------------------------------------------------------- 1 | public boolean insert(int index, int value) { 2 | if (index < 0 || index > length) return false; 3 | if (index == 0) { 4 | prepend(value); 5 | return true; 6 | } 7 | if (index == length) { 8 | append(value); 9 | return true; 10 | } 11 | Node newNode = new Node(value); 12 | Node temp = get(index - 1); 13 | newNode.next = temp.next; 14 | temp.next = newNode; 15 | length++; 16 | return true; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Graph/Add Vertex: -------------------------------------------------------------------------------- 1 | package datastructures.graph; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | 6 | 7 | public class Graph { 8 | 9 | private HashMap> adjList = new HashMap<>(); 10 | 11 | public void printGraph() { 12 | System.out.println(adjList); 13 | } 14 | 15 | public boolean addVertex(String vertex) { 16 | if (adjList.get(vertex) == null) { 17 | adjList.put(vertex, new ArrayList()); 18 | return true; 19 | } 20 | return false; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Linked List/print list: -------------------------------------------------------------------------------- 1 | 2 | // These methods go in Linked List class 3 | 4 | 5 | public void printList() { 6 | Node temp = head; 7 | while (temp != null) { 8 | System.out.println(temp.value); 9 | temp = temp.next; 10 | } 11 | } 12 | 13 | public void getHead() { 14 | System.out.println("Head: " + head.value); 15 | } 16 | 17 | public void getTail() { 18 | System.out.println("Tail: " + tail.value); 19 | } 20 | 21 | public void getLength() { 22 | System.out.println("Length: " + length); 23 | } 24 | -------------------------------------------------------------------------------- /Doubly Linked Lists/insert: -------------------------------------------------------------------------------- 1 | public boolean insert(int index, int value) { 2 | if(index < 0 || index > length) return false; 3 | if(index == 0) { 4 | prepend(value); 5 | return true; 6 | } 7 | if(index == length) { 8 | append(value); 9 | return true; 10 | } 11 | Node newNode = new Node(value); 12 | Node before = get(index - 1); 13 | Node after = before.next; 14 | newNode.prev = before; 15 | newNode.next = after; 16 | before.next = newNode; 17 | after.prev = newNode; 18 | length++; 19 | return true; 20 | } 21 | -------------------------------------------------------------------------------- /Hash Table/Hash Table intervew q: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | 3 | public class Main { 4 | 5 | public static boolean itemInCommon(int[] array1, int[] array2) { 6 | HashMap myHashMap = new HashMap(); 7 | 8 | for (int i : array1) { 9 | myHashMap.put(i, true); 10 | } 11 | 12 | for (int j : array2) { 13 | if (myHashMap.get(j) != null) return true; 14 | } 15 | 16 | return false; 17 | } 18 | 19 | public static void main(String[] args) { 20 | 21 | int[] array1 = {1, 3, 5}; 22 | int[] array2 = {2, 4, 5}; 23 | 24 | System.out.println(itemInCommon(array1, array2)); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Hash Table/Set: -------------------------------------------------------------------------------- 1 | 2 | public void set(String key, int value) { 3 | int index = hash(key); 4 | Node newNode = new Node(key, value); 5 | if (dataMap[index] == null) { 6 | dataMap[index] = newNode; 7 | } else { 8 | Node temp = dataMap[index]; 9 | if (temp.key == key) { 10 | temp.value += value; 11 | return; 12 | } 13 | while (temp.next != null) { 14 | temp = temp.next; 15 | if (temp.key == key) { 16 | temp.value += value; 17 | return; 18 | } 19 | } 20 | temp.next = newNode; 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Dynamic Programming/Bottom Up: -------------------------------------------------------------------------------- 1 | public class Fib_Bottom_Up { 2 | static int counter = 0; 3 | 4 | public static int fib(int n) { 5 | int[] fibList = new int[n + 1]; 6 | fibList[0] = 0; 7 | fibList[1] = 1; 8 | 9 | for (int index = 2; index <= n; index++) { 10 | counter++; 11 | fibList[index] = fibList[index - 1] + fibList[index - 2]; 12 | } 13 | return fibList[n]; 14 | } 15 | 16 | 17 | 18 | public static void main(String[] args) { 19 | 20 | int n = 7; 21 | 22 | System.out.println("\nFib of " + n + " = " + fib(n)); 23 | 24 | System.out.println("\nCounter: " + counter); 25 | 26 | } 27 | 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Dynamic Programming/Memoization: -------------------------------------------------------------------------------- 1 | public class Fib_With_Memo { 2 | 3 | static Integer[] memo = new Integer[100]; 4 | static int counter = 0; 5 | 6 | public static int fib(int n) { 7 | counter++; 8 | 9 | if (memo[n] != null) { 10 | return memo[n]; 11 | } 12 | 13 | if (n == 0 || n == 1) { 14 | return n; 15 | } 16 | 17 | memo[n] = fib(n - 1) + fib(n - 2); 18 | 19 | return memo[n]; 20 | } 21 | 22 | 23 | 24 | public static void main(String[] args) { 25 | 26 | int n = 7; 27 | 28 | System.out.println("\nFib of " + n + " = " + fib(n)); 29 | 30 | System.out.println("\nCounter: " + counter); 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Hash Table/constructer: -------------------------------------------------------------------------------- 1 | 2 | package datastructures.hashtable; 3 | 4 | 5 | public class HashTable { 6 | private int size = 7; 7 | private Node[] dataMap; 8 | 9 | class Node { 10 | String key; 11 | int value; 12 | Node next; 13 | 14 | Node(String key, int value) { 15 | this.key = key; 16 | this.value = value; 17 | } 18 | } 19 | 20 | public HashTable() { 21 | dataMap = new Node[size]; 22 | } 23 | 24 | public void printTable() { 25 | for (int i = 0; i < dataMap.length; i++) { 26 | System.out.println(i + ":"); 27 | Node temp = dataMap[i]; 28 | while (temp != null) { 29 | System.out.println(" {" + temp.key + "= " + temp.value + "}"); 30 | temp = temp.next; 31 | } 32 | } 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Basic Sorts/Insertion Sort: -------------------------------------------------------------------------------- 1 | algorithms.sorts.insertionsort; 2 | 3 | import java.util.Arrays; 4 | 5 | public class InsertionSort { 6 | 7 | public static void insertionSort(int[] array) { 8 | for (int i = 1; i < array.length; i++) { 9 | int temp = array[i]; 10 | int j = i - 1; 11 | while (j > -1 && temp < array[j]) { 12 | array[j+1] = array[j]; 13 | array[j] = temp; 14 | j--; 15 | } 16 | } 17 | } 18 | 19 | 20 | 21 | public static void main(String[] args) { 22 | 23 | int[] myArray = {4,2,6,5,1,3}; 24 | 25 | insertionSort(myArray); 26 | 27 | System.out.println( Arrays.toString(myArray) ); 28 | 29 | /* 30 | EXPECTED OUTPUT: 31 | ---------------- 32 | [1, 2, 3, 4, 5, 6] 33 | 34 | */ 35 | 36 | } 37 | 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Basic Sorts/Bubble sort: -------------------------------------------------------------------------------- 1 | package algorithms.sorts.bubblesort; 2 | 3 | import java.util.Arrays; 4 | 5 | public class BubbleSort { 6 | 7 | public static void bubbleSort(int[] array) { 8 | for (int i = array.length - 1; i > 0; i--) { 9 | for (int j = 0; j < i; j++) { 10 | if (array[j] > array[j+1]) { 11 | int temp = array[j]; 12 | array[j] = array[j+1]; 13 | array[j+1] = temp; 14 | } 15 | } 16 | } 17 | } 18 | 19 | 20 | 21 | public static void main(String[] args) { 22 | 23 | int[] myArray = {4,2,6,5,1,3}; 24 | 25 | bubbleSort(myArray); 26 | 27 | System.out.println( Arrays.toString(myArray) ); 28 | 29 | /* 30 | EXPECTED OUTPUT: 31 | ---------------- 32 | [1, 2, 3, 4, 5, 6] 33 | 34 | */ 35 | 36 | } 37 | 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Stacks&Queues/constructer: -------------------------------------------------------------------------------- 1 | 2 | package datastructures.stack; 3 | 4 | public class Stack { 5 | 6 | private Node top; 7 | private int height; 8 | 9 | class Node { 10 | int value; 11 | Node next; 12 | 13 | Node(int value) { 14 | this.value = value; 15 | } 16 | } 17 | 18 | public Stack(int value) { 19 | Node newNode = new Node(value); 20 | top = newNode; 21 | height = 1; 22 | } 23 | 24 | public void printStack() { 25 | Node temp = top; 26 | while (temp != null) { 27 | System.out.println(temp.value); 28 | temp = temp.next; 29 | } 30 | } 31 | 32 | public void getTop() { 33 | if (top == null) { 34 | System.out.println("Top: null"); 35 | } else { 36 | System.out.println("Top: " + top.value); 37 | } 38 | } 39 | 40 | public void getHeight() { 41 | System.out.println("Height: " + height); 42 | } 43 | 44 | } 45 | 46 | -------------------------------------------------------------------------------- /Basic Sorts/Selection sort: -------------------------------------------------------------------------------- 1 | kage algorithms.sorts.selectionsort; 2 | 3 | import java.util.Arrays; 4 | 5 | public class SelectionSort { 6 | 7 | public static void selectionSort(int[] array) { 8 | for (int i = 0; i < array.length; i++) { 9 | int minIndex = i; 10 | for (int j = i+1; j < array.length; j++) { 11 | if (array[j] < array[minIndex]) { 12 | minIndex = j; 13 | } 14 | } 15 | if (i != minIndex) { 16 | int temp = array[i]; 17 | array[i] = array[minIndex]; 18 | array[minIndex] = temp; 19 | } 20 | } 21 | } 22 | 23 | 24 | 25 | public static void main(String[] args) { 26 | 27 | int[] myArray = {4,2,6,5,1,3}; 28 | 29 | selectionSort(myArray); 30 | 31 | System.out.println( Arrays.toString(myArray) ); 32 | 33 | /* 34 | EXPECTED OUTPUT: 35 | ---------------- 36 | [1, 2, 3, 4, 5, 6] 37 | 38 | */ 39 | 40 | } 41 | 42 | } 43 | 44 | -------------------------------------------------------------------------------- /Binary Search Tree/Insert: -------------------------------------------------------------------------------- 1 | 2 | package datastructures.binarysearchtree; 3 | 4 | 5 | public class BinarySearchTree { 6 | 7 | // ROOT MUST BE PUBLIC FOR CODE IN MAIN METHOD TO WORK 8 | public Node root; 9 | 10 | class Node { 11 | public int value; 12 | public Node left; 13 | public Node right; 14 | 15 | Node(int value) { 16 | this.value = value; 17 | } 18 | } 19 | 20 | public boolean insert(int value) { 21 | Node newNode = new Node(value); 22 | if (root == null) { 23 | root = newNode; 24 | return true; 25 | } 26 | Node temp = root; 27 | while (true) { 28 | if (newNode.value == temp.value) return false; 29 | if (newNode.value < temp.value) { 30 | if (temp.left == null) { 31 | temp.left = newNode; 32 | return true; 33 | } 34 | temp = temp.left; 35 | } else { 36 | if (temp.right == null) { 37 | temp.right = newNode; 38 | return true; 39 | } 40 | temp = temp.right; 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Doubly Linked Lists/constructer: -------------------------------------------------------------------------------- 1 | 2 | public class DoublyLinkedList { 3 | 4 | private Node head; 5 | private Node tail; 6 | private int length; 7 | 8 | class Node { 9 | int value; 10 | Node next; 11 | Node prev; 12 | 13 | Node(int value) { 14 | this.value = value; 15 | } 16 | } 17 | 18 | public DoublyLinkedList(int value) { 19 | Node newNode = new Node(value); 20 | head = newNode; 21 | tail = newNode; 22 | length = 1; 23 | } 24 | 25 | public void printList() { 26 | Node temp = head; 27 | while (temp != null) { 28 | System.out.println(temp.value); 29 | temp = temp.next; 30 | } 31 | } 32 | 33 | public void getHead() { 34 | if (head == null) { 35 | System.out.println("Head: null"); 36 | } else { 37 | System.out.println("Head: " + head.value); 38 | } 39 | } 40 | 41 | public void getTail() { 42 | if (head == null) { 43 | System.out.println("Tail: null"); 44 | } else { 45 | System.out.println("Tail: " + tail.value); 46 | } 47 | } 48 | 49 | public void getLength() { 50 | System.out.println("Length: " + length); 51 | } 52 | 53 | } 54 | 55 | -------------------------------------------------------------------------------- /Basic Sorts/QUICK sort/pivot: -------------------------------------------------------------------------------- 1 | package algorithms.sorts.quicksort; 2 | 3 | import java.util.Arrays; 4 | 5 | public class QuickSort { 6 | 7 | private static void swap(int[] array, int firstIndex, int secondIndex) { 8 | int temp = array[firstIndex]; 9 | array[firstIndex] = array[secondIndex]; 10 | array[secondIndex] = temp; 11 | } 12 | 13 | 14 | public static int pivot(int[] array, int pivotIndex, int endIndex) { 15 | int swapIndex = pivotIndex; 16 | for (int i = pivotIndex + 1; i <= endIndex; i++) { 17 | if (array[i] < array[pivotIndex]) { 18 | swapIndex++; 19 | swap(array, swapIndex, i); 20 | } 21 | } 22 | swap(array, pivotIndex, swapIndex); 23 | 24 | return swapIndex; 25 | } 26 | 27 | 28 | 29 | public static void main(String[] args) { 30 | 31 | int[] myArray = {4,6,1,7,3,2,5}; 32 | 33 | int returnedIndex = pivot(myArray, 0, 6); 34 | 35 | System.out.println( "\nReturned Index: " + returnedIndex); 36 | 37 | System.out.println( Arrays.toString( myArray ) ); 38 | 39 | /* 40 | EXPECTED OUTPUT: 41 | ---------------- 42 | Returned Index: 3 43 | [2, 1, 3, 4, 6, 7, 5] 44 | 45 | */ 46 | 47 | } 48 | 49 | } 50 | 51 | -------------------------------------------------------------------------------- /Basic Sorts/Merge Sort: -------------------------------------------------------------------------------- 1 | package algorithms.sorts.mergesort; 2 | 3 | import java.util.Arrays; 4 | 5 | public class MergeSort { 6 | 7 | public static int[] merge(int[] array1, int[] array2) { 8 | int[] combined = new int[array1.length + array2.length]; 9 | int index = 0; 10 | int i = 0; 11 | int j = 0; 12 | while (i < array1.length && j < array2.length) { 13 | if (array1[i] < array2[j]) { 14 | combined[index] = array1[i]; 15 | index++; 16 | i++; 17 | } else { 18 | combined[index] = array2[j]; 19 | index++; 20 | j++; 21 | } 22 | } 23 | while (i < array1.length) { 24 | combined[index] = array1[i]; 25 | index++; 26 | i++; 27 | } 28 | while (j < array2.length) { 29 | combined[index] = array2[j]; 30 | index++; 31 | j++; 32 | } 33 | return combined; 34 | } 35 | 36 | 37 | 38 | public static void main(String[] args) { 39 | 40 | int[] array1 = {1,3,7,8}; 41 | int[] array2 = {2,4,5,6}; 42 | 43 | System.out.println( Arrays.toString( merge(array1, array2) ) ); 44 | 45 | /* 46 | EXPECTED OUTPUT: 47 | ---------------- 48 | [1, 2, 3, 4, 5, 6, 7, 8] 49 | 50 | */ 51 | 52 | } 53 | 54 | } 55 | 56 | -------------------------------------------------------------------------------- /Basic Sorts/QUICK sort/quick sort code: -------------------------------------------------------------------------------- 1 | package algorithms.sorts.quicksort; 2 | 3 | import java.util.Arrays; 4 | 5 | public class QuickSort { 6 | 7 | private static void swap(int[] array, int firstIndex, int secondIndex) { 8 | int temp = array[firstIndex]; 9 | array[firstIndex] = array[secondIndex]; 10 | array[secondIndex] = temp; 11 | } 12 | 13 | private static int pivot(int[] array, int pivotIndex, int endIndex) { 14 | int swapIndex = pivotIndex; 15 | for (int i = pivotIndex + 1; i <= endIndex; i++) { 16 | if (array[i] < array[pivotIndex]) { 17 | swapIndex++; 18 | swap(array, swapIndex, i); 19 | } 20 | } 21 | swap(array, pivotIndex, swapIndex); 22 | 23 | return swapIndex; 24 | } 25 | 26 | 27 | 28 | private static void quickSortHelper(int[] array, int left, int right) { 29 | if (left < right) { 30 | int pivotIndex = pivot(array, left, right); 31 | quickSortHelper(array, left, pivotIndex-1); 32 | quickSortHelper(array, pivotIndex+1, right); 33 | } 34 | } 35 | 36 | public static void quickSort(int[] array) { 37 | quickSortHelper(array, 0, array.length-1); 38 | } 39 | 40 | 41 | 42 | 43 | public static void main(String[] args) { 44 | 45 | int[] myArray = {4,6,1,7,3,2,5}; 46 | 47 | quickSort(myArray); 48 | 49 | System.out.println( Arrays.toString( myArray ) ); 50 | 51 | /* 52 | EXPECTED OUTPUT: 53 | ---------------- 54 | [1, 2, 3, 4, 5, 6, 7] 55 | 56 | */ 57 | 58 | } 59 | 60 | } 61 | 62 | -------------------------------------------------------------------------------- /Linked List/Linked-List.java: -------------------------------------------------------------------------------- 1 | 2 | package course ; 3 | import java.util.*; 4 | import javafx.scene.Node; 5 | public class LinkedList { 6 | 7 | private Node head; 8 | private Node tail; 9 | private int length; 10 | 11 | class Node { 12 | int value; 13 | Node next; 14 | 15 | Node(int value) { 16 | this.value = value; 17 | } 18 | } 19 | 20 | public LinkedList(int value) { 21 | Node newNode = new Node(value); 22 | head = newNode; 23 | tail = newNode; 24 | length = 1; 25 | } 26 | 27 | public Node getHead() { 28 | return head; 29 | } 30 | 31 | public Node getTail() { 32 | return tail; 33 | } 34 | 35 | public int getLength() { 36 | return length; 37 | } 38 | 39 | public void printList() { 40 | Node temp = head; 41 | while (temp != null) { 42 | System.out.println(temp.value); 43 | temp = temp.next; 44 | } 45 | } 46 | 47 | public void printAll() { 48 | if (length == 0) { 49 | System.out.println("Head: null"); 50 | System.out.println("Tail: null"); 51 | } else { 52 | System.out.println("Head: " + head.value); 53 | System.out.println("Tail: " + tail.value); 54 | } 55 | System.out.println("Length:" + length); 56 | System.out.println("\nLinked List:"); 57 | if (length == 0) { 58 | System.out.println("empty"); 59 | } else { 60 | printList(); 61 | } 62 | } 63 | 64 | } 65 | 66 | // main 67 | public class Main { 68 | 69 | public static void main(String[] args) { 70 | 71 | LinkedList myLinkedList = new LinkedList(4); 72 | 73 | myLinkedList.printAll(); 74 | 75 | }} -------------------------------------------------------------------------------- /Stacks&Queues/Queue constructer: -------------------------------------------------------------------------------- 1 | package datastructures.queue; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | 7 | Queue myQueue = new Queue(4); 8 | 9 | myQueue.getFirst(); 10 | myQueue.getLast(); 11 | myQueue.getLength(); 12 | 13 | System.out.println("\nQueue:"); 14 | myQueue.printQueue(); 15 | 16 | /* 17 | EXPECTED OUTPUT: 18 | ---------------- 19 | First: 4 20 | Last: 4 21 | Length: 1 22 | 23 | Queue: 24 | 4 25 | 26 | */ 27 | 28 | } 29 | 30 | } 31 | 32 | 33 | 34 | 35 | 36 | // THIS CODE GOES IN YOUR QUEUE CLASS: 37 | // ----------------------------------- 38 | 39 | package datastructures.queue; 40 | 41 | public class Queue { 42 | 43 | private Node first; 44 | private Node last; 45 | private int length; 46 | 47 | class Node { 48 | int value; 49 | Node next; 50 | 51 | Node(int value) { 52 | this.value = value; 53 | } 54 | } 55 | 56 | public Queue(int value) { 57 | Node newNode = new Node(value); 58 | first = newNode; 59 | last = newNode; 60 | length = 1; 61 | } 62 | 63 | public void printQueue() { 64 | Node temp = first; 65 | while (temp != null) { 66 | System.out.println(temp.value); 67 | temp = temp.next; 68 | } 69 | } 70 | 71 | public void getFirst() { 72 | if (first == null) { 73 | System.out.println("First: null"); 74 | } else { 75 | System.out.println("First: " + first.value); 76 | } 77 | } 78 | 79 | public void getLast() { 80 | if (last == null) { 81 | System.out.println("Last: null"); 82 | } else { 83 | System.out.println("Last: " + last.value); 84 | } 85 | } 86 | 87 | public void getLength() { 88 | System.out.println("Length: " + length); 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /Hash Table/Hash Method: -------------------------------------------------------------------------------- 1 | 2 | package datastructures.hashtable; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | 8 | HashTable myHashTable = new HashTable(); 9 | 10 | // HASH METHOD MUST BE SET TO PUBLIC FOR THESE LINES TO WORK 11 | System.out.println( myHashTable.hash("paint") ); 12 | System.out.println( myHashTable.hash("bolts") ); 13 | System.out.println( myHashTable.hash("nails") ); 14 | System.out.println( myHashTable.hash("stuff") ); 15 | System.out.println( myHashTable.hash("lumber") ); 16 | 17 | /* 18 | EXPECTED OUTPUT: 19 | ---------------- 20 | 2 21 | 4 22 | 6 23 | 5 24 | 6 25 | 26 | */ 27 | 28 | 29 | } 30 | 31 | } 32 | 33 | 34 | 35 | 36 | 37 | // THIS CODE GOES IN YOUR HASHTABLE CLASS: 38 | // --------------------------------------- 39 | 40 | package datastructures.hashtable; 41 | 42 | 43 | public class HashTable { 44 | private int size = 7; 45 | private Node[] dataMap; 46 | 47 | class Node { 48 | String key; 49 | int value; 50 | Node next; 51 | 52 | Node(String key, int value) { 53 | this.key = key; 54 | this.value = value; 55 | } 56 | } 57 | 58 | public HashTable() { 59 | dataMap = new Node[size]; 60 | } 61 | 62 | public void printTable() { 63 | for (int i = 0; i < dataMap.length; i++) { 64 | System.out.println(i + ":"); 65 | Node temp = dataMap[i]; 66 | while (temp != null) { 67 | System.out.println(" {" + temp.key + "= " + temp.value + "}"); 68 | temp = temp.next; 69 | } 70 | } 71 | } 72 | 73 | // HASH METHOD MUST BE PUBLIC FOR CODE IN MAIN TO WORK 74 | public int hash(String key) { 75 | int hash = 0; 76 | char[] keyChars = key.toCharArray(); 77 | for (int i = 0; i < keyChars.length; i++) { 78 | int asciiValue = keyChars[i]; 79 | hash = (hash + asciiValue * 23) % dataMap.length; 80 | } 81 | return hash; 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /Linked List/Aappend.java: -------------------------------------------------------------------------------- 1 | 2 | package course; 3 | 4 | class linkedlist { 5 | 6 | private Node head; 7 | private Node tail; 8 | private int length; 9 | 10 | class Node { 11 | int value; 12 | Node next; 13 | 14 | Node(int value) { 15 | this.value = value; 16 | } 17 | } 18 | public linkedlist(int value) { 19 | Node newNode = new Node(value); 20 | head = newNode; 21 | tail = newNode; 22 | length = 1; 23 | } 24 | 25 | public Node getHead() { 26 | return head; 27 | } 28 | 29 | public Node getTail() { 30 | return tail; 31 | } 32 | 33 | public int getLength() { 34 | return length; 35 | } 36 | 37 | public void printList() { 38 | Node temp = head; 39 | while (temp != null) { 40 | System.out.print(temp.value+ " "); 41 | temp = temp.next; 42 | } 43 | System.out.println(); 44 | } 45 | public void append(int value) { 46 | Node newnode = new Node(value); 47 | if (length==0){ 48 | head = newnode; 49 | tail=newnode; 50 | } 51 | else { 52 | tail.next=newnode; 53 | tail=newnode; 54 | } 55 | length++; 56 | } 57 | public void printAll() { 58 | if (length == 0) { 59 | System.out.println("Head: null"); 60 | System.out.println("Tail: null"); 61 | } else { 62 | System.out.println("Head: " + head.value); 63 | System.out.println("Tail: " + tail.value); 64 | } 65 | System.out.println("Length:" + length); 66 | System.out.println("\nLinked List:"); 67 | if (length == 0) { 68 | System.out.println("empty"); 69 | } else { 70 | printList(); 71 | } 72 | } 73 | 74 | } 75 | 76 | // main 77 | package course ; 78 | import java.util.*; 79 | import javafx.scene.Node; 80 | public class Course{ 81 | public static void main(String[] args) { 82 | linkedlist mylinkedlist = new linkedlist(1); 83 | mylinkedlist.append(2); 84 | mylinkedlist.append(3); 85 | mylinkedlist.append(4); 86 | mylinkedlist.append(5); 87 | mylinkedlist.printList(); 88 | 89 | } 90 | } -------------------------------------------------------------------------------- /Linked List/remove last.java: -------------------------------------------------------------------------------- 1 | 2 | package course; 3 | 4 | class linkedlist { 5 | 6 | private Node head; 7 | private Node tail; 8 | private int length; 9 | 10 | class Node { 11 | int value; 12 | Node next; 13 | 14 | Node(int value) { 15 | this.value = value; 16 | } 17 | } 18 | public linkedlist(int value) { 19 | Node newNode = new Node(value); 20 | head = newNode; 21 | tail = newNode; 22 | length = 1; 23 | } 24 | 25 | public Node getHead() { 26 | return head; 27 | } 28 | 29 | public Node getTail() { 30 | return tail; 31 | } 32 | 33 | public int getLength() { 34 | return length; 35 | } 36 | 37 | public void printList() { 38 | Node temp = head; 39 | while (temp != null) { 40 | System.out.print(temp.value+ " "); 41 | temp = temp.next; 42 | } 43 | System.out.println(); 44 | } 45 | public void append(int value) { 46 | Node newnode = new Node(value); 47 | if (length==0){ 48 | head = newnode; 49 | tail=newnode; 50 | } 51 | else { 52 | tail.next=newnode; 53 | tail=newnode; 54 | } 55 | length++; 56 | } 57 | 58 | public Node removeLast() { 59 | if (length == 0) return null; 60 | Node temp = head; 61 | Node pre = head; 62 | while(temp.next != null) { 63 | pre = temp; 64 | temp = temp.next; 65 | } 66 | tail = pre; 67 | tail.next = null; 68 | length--; 69 | if (length == 0) { 70 | head = null; 71 | tail = null; 72 | } 73 | return temp; 74 | } 75 | 76 | 77 | public void printAll() { 78 | if (length == 0) { 79 | System.out.println("Head: null"); 80 | System.out.println("Tail: null"); 81 | } else { 82 | System.out.println("Head: " + head.value); 83 | System.out.println("Tail: " + tail.value); 84 | } 85 | System.out.println("Length:" + length); 86 | System.out.println("\nLinked List:"); 87 | if (length == 0) { 88 | System.out.println("empty"); 89 | } else { 90 | printList(); 91 | } 92 | } 93 | 94 | } 95 | //main 96 | 97 | package course ; 98 | import java.util.*; 99 | import javafx.scene.Node; 100 | public class Course{ 101 | public static void main(String[] args) { 102 | linkedlist mylinkedlist = new linkedlist(1); 103 | mylinkedlist.append(2); 104 | 105 | 106 | // (2) Items - Returns 2 Node 107 | System.out.println(mylinkedlist.removeLast().value); 108 | // (1) Item - Returns 1 Node 109 | System.out.println(mylinkedlist.removeLast().value); 110 | // (0) Items - Returns null 111 | System.out.println(mylinkedlist.removeLast()); 112 | 113 | 114 | 115 | } -------------------------------------------------------------------------------- /DFS/code: -------------------------------------------------------------------------------- 1 | 2 | package datastructures.binarysearchtree; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | 8 | BinarySearchTree myBST = new BinarySearchTree(); 9 | 10 | myBST.insert(47); 11 | myBST.insert(21); 12 | myBST.insert(76); 13 | myBST.insert(18); 14 | myBST.insert(27); 15 | myBST.insert(52); 16 | myBST.insert(82); 17 | 18 | System.out.println("\nBreadth First Search:"); 19 | System.out.println( myBST.BFS() ); 20 | 21 | /* 22 | EXPECTED OUTPUT: 23 | ---------------- 24 | Breadth First Search: 25 | [47, 21, 76, 18, 27, 52, 82] 26 | 27 | */ 28 | 29 | } 30 | 31 | } 32 | 33 | 34 | 35 | 36 | 37 | // THIS CODE GOES IN YOUR BST CLASS: 38 | // --------------------------------- 39 | 40 | package datastructures.binarysearchtree; 41 | 42 | import java.util.ArrayList; 43 | import java.util.LinkedList; 44 | import java.util.Queue; 45 | 46 | 47 | public class BinarySearchTree { 48 | 49 | public Node root; 50 | 51 | public static class Node { 52 | public int value; 53 | public Node left; 54 | public Node right; 55 | 56 | private Node(int value) { 57 | this.value = value; 58 | } 59 | } 60 | 61 | public boolean insert(int value) { 62 | Node newNode = new Node(value); 63 | if (root == null) { 64 | root = newNode; 65 | return true; 66 | } 67 | Node temp = root; 68 | while (true) { 69 | if (newNode.value == temp.value) return false; 70 | if (newNode.value < temp.value) { 71 | if (temp.left == null) { 72 | temp.left = newNode; 73 | return true; 74 | } 75 | temp = temp.left; 76 | } else { 77 | if (temp.right == null) { 78 | temp.right = newNode; 79 | return true; 80 | } 81 | temp = temp.right; 82 | } 83 | } 84 | } 85 | 86 | public boolean contains(int value) { 87 | if (root == null) return false; 88 | Node temp = root; 89 | while (temp != null) { 90 | if (value < temp.value) { 91 | temp = temp.left; 92 | } else if (value > temp.value) { 93 | temp = temp.right; 94 | } else { 95 | return true; 96 | } 97 | } 98 | return false; 99 | } 100 | 101 | public ArrayList BFS() { 102 | Node currentNode = root; 103 | Queue queue = new LinkedList<>(); 104 | ArrayList results = new ArrayList<>(); 105 | queue.add(currentNode); 106 | 107 | while (queue.size() > 0) { 108 | currentNode = queue.remove(); 109 | results.add(currentNode.value); 110 | if (currentNode.left != null) { 111 | queue.add(currentNode.left); 112 | } 113 | if (currentNode.right != null) { 114 | queue.add(currentNode.right); 115 | } 116 | } 117 | return results; 118 | } 119 | 120 | } 121 | -------------------------------------------------------------------------------- /DFS/DFS post order: -------------------------------------------------------------------------------- 1 | 2 | package datastructures.binarysearchtree; 3 | 4 | import java.util.ArrayList; 5 | import java.util.LinkedList; 6 | import java.util.Queue; 7 | 8 | 9 | public class BinarySearchTree { 10 | 11 | public Node root; 12 | 13 | public static class Node { 14 | public int value; 15 | public Node left; 16 | public Node right; 17 | 18 | private Node(int value) { 19 | this.value = value; 20 | } 21 | } 22 | 23 | public boolean insert(int value) { 24 | Node newNode = new Node(value); 25 | if (root == null) { 26 | root = newNode; 27 | return true; 28 | } 29 | Node temp = root; 30 | while (true) { 31 | if (newNode.value == temp.value) return false; 32 | if (newNode.value < temp.value) { 33 | if (temp.left == null) { 34 | temp.left = newNode; 35 | return true; 36 | } 37 | temp = temp.left; 38 | } else { 39 | if (temp.right == null) { 40 | temp.right = newNode; 41 | return true; 42 | } 43 | temp = temp.right; 44 | } 45 | } 46 | } 47 | 48 | public boolean contains(int value) { 49 | if (root == null) return false; 50 | Node temp = root; 51 | while (temp != null) { 52 | if (value < temp.value) { 53 | temp = temp.left; 54 | } else if (value > temp.value) { 55 | temp = temp.right; 56 | } else { 57 | return true; 58 | } 59 | } 60 | return false; 61 | } 62 | 63 | public ArrayList BFS() { 64 | Node currentNode = root; 65 | Queue queue = new LinkedList<>(); 66 | ArrayList results = new ArrayList<>(); 67 | queue.add(currentNode); 68 | 69 | while (queue.size() > 0) { 70 | currentNode = queue.remove(); 71 | results.add(currentNode.value); 72 | if (currentNode.left != null) { 73 | queue.add(currentNode.left); 74 | } 75 | if (currentNode.right != null) { 76 | queue.add(currentNode.right); 77 | } 78 | } 79 | return results; 80 | } 81 | 82 | public ArrayList DFSPreOrder() { 83 | ArrayList results = new ArrayList<>(); 84 | 85 | class Traverse { 86 | Traverse(Node currentNode) { 87 | results.add(currentNode.value); 88 | if (currentNode.left != null) { 89 | new Traverse(currentNode.left); 90 | } 91 | if (currentNode.right != null) { 92 | new Traverse(currentNode.right); 93 | } 94 | } 95 | } 96 | 97 | new Traverse(root); 98 | return results; 99 | } 100 | 101 | public ArrayList DFSPostOrder() { 102 | ArrayList results = new ArrayList<>(); 103 | 104 | class Traverse { 105 | Traverse(Node currentNode) { 106 | if (currentNode.left != null) { 107 | new Traverse(currentNode.left); 108 | } 109 | if (currentNode.right != null) { 110 | new Traverse(currentNode.right); 111 | } 112 | results.add(currentNode.value); 113 | } 114 | } 115 | 116 | new Traverse(root); 117 | return results; 118 | } 119 | 120 | } 121 | 122 | -------------------------------------------------------------------------------- /Linked List/all Linked List.java: -------------------------------------------------------------------------------- 1 | 2 | package course; 3 | 4 | class linkedlist { 5 | 6 | private Node head; 7 | private Node tail; 8 | private int length; 9 | 10 | class Node { 11 | int value; 12 | Node next; 13 | 14 | Node(int value) { 15 | this.value = value; 16 | } 17 | } 18 | 19 | public linkedlist(int value) { 20 | Node newNode = new Node(value); 21 | head = newNode; 22 | tail = newNode; 23 | length = 1; 24 | } 25 | 26 | public void printList() { 27 | Node temp = head; 28 | while (temp != null) { 29 | System.out.println(temp.value); 30 | temp = temp.next; 31 | } 32 | } 33 | 34 | public void getHead() { 35 | if (head == null) { 36 | System.out.println("Head: null"); 37 | } else { 38 | System.out.println("Head: " + head.value); 39 | } 40 | } 41 | 42 | public void getTail() { 43 | if (head == null) { 44 | System.out.println("Tail: null"); 45 | } else { 46 | System.out.println("Tail: " + tail.value); 47 | } 48 | } 49 | 50 | public void getLength() { 51 | System.out.println("Length: " + length); 52 | } 53 | 54 | public void append(int value) { 55 | Node newNode = new Node(value); 56 | if (length == 0) { 57 | head = newNode; 58 | tail = newNode; 59 | } else { 60 | tail.next = newNode; 61 | tail = newNode; 62 | } 63 | length++; 64 | } 65 | 66 | public Node removeLast() { 67 | if (length == 0) return null; 68 | Node temp = head; 69 | Node pre = head; 70 | while(temp.next != null) { 71 | pre = temp; 72 | temp = temp.next; 73 | } 74 | tail = pre; 75 | tail.next = null; 76 | length--; 77 | if (length == 0) { 78 | head = null; 79 | tail = null; 80 | } 81 | return temp; 82 | } 83 | 84 | public void prepend(int value) { 85 | Node newNode = new Node(value); 86 | if (length == 0) { 87 | head = newNode; 88 | tail = newNode; 89 | } else { 90 | newNode.next = head; 91 | head = newNode; 92 | } 93 | length++; 94 | } 95 | 96 | public Node removeFirst() { 97 | if (length == 0) return null; 98 | Node temp = head; 99 | head = head.next; 100 | temp.next = null; 101 | length--; 102 | if (length == 0) { 103 | tail = null; 104 | } 105 | return temp; 106 | } 107 | 108 | public Node get(int index) { 109 | if (index < 0 || index >= length) return null; 110 | Node temp = head; 111 | for(int i = 0; i < index; i++) { 112 | temp = temp.next; 113 | } 114 | return temp; 115 | } 116 | 117 | public boolean set(int index, int value) { 118 | Node temp = get(index); 119 | if (temp != null) { 120 | temp.value = value; 121 | return true; 122 | } 123 | return false; 124 | } 125 | 126 | public boolean insert(int index, int value) { 127 | if (index < 0 || index > length) return false; 128 | if (index == 0) { 129 | prepend(value); 130 | return true; 131 | } 132 | if (index == length) { 133 | append(value); 134 | return true; 135 | } 136 | Node newNode = new Node(value); 137 | Node temp = get(index - 1); 138 | newNode.next = temp.next; 139 | temp.next = newNode; 140 | length++; 141 | return true; 142 | } 143 | 144 | public Node remove(int index) { 145 | if (index < 0 || index >= length) return null; 146 | if (index == 0) return removeFirst(); 147 | if (index == length - 1) return removeLast(); 148 | 149 | Node prev = get(index - 1); 150 | Node temp = prev.next; 151 | 152 | prev.next = temp.next; 153 | temp.next = null; 154 | length--; 155 | return temp; 156 | } 157 | 158 | public void reverse() { 159 | Node temp = head; 160 | head = tail; 161 | tail = temp; 162 | Node after = temp.next; 163 | Node before = null; 164 | for (int i = 0; i < length; i++) { 165 | after = temp.next; 166 | temp.next = before; 167 | before = temp; 168 | temp = after; 169 | } 170 | } 171 | 172 | } 173 | 174 | -------------------------------------------------------------------------------- /DFS/Inorder: -------------------------------------------------------------------------------- 1 | 2 | package datastructures.binarysearchtree; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | 8 | BinarySearchTree myBST = new BinarySearchTree(); 9 | 10 | myBST.insert(47); 11 | myBST.insert(21); 12 | myBST.insert(76); 13 | myBST.insert(18); 14 | myBST.insert(27); 15 | myBST.insert(52); 16 | myBST.insert(82); 17 | 18 | System.out.println("\nDFS InOrder:"); 19 | System.out.println( myBST.DFSInOrder() ); 20 | 21 | /* 22 | EXPECTED OUTPUT: 23 | ---------------- 24 | DFS InOrder: 25 | [18, 21, 27, 47, 52, 76, 82] 26 | 27 | */ 28 | 29 | } 30 | 31 | } 32 | 33 | 34 | 35 | 36 | 37 | // THIS CODE GOES IN YOUR BST CLASS: 38 | // --------------------------------- 39 | 40 | package datastructures.binarysearchtree; 41 | 42 | import java.util.ArrayList; 43 | import java.util.LinkedList; 44 | import java.util.Queue; 45 | 46 | 47 | public class BinarySearchTree { 48 | 49 | public Node root; 50 | 51 | public static class Node { 52 | public int value; 53 | public Node left; 54 | public Node right; 55 | 56 | private Node(int value) { 57 | this.value = value; 58 | } 59 | } 60 | 61 | public boolean insert(int value) { 62 | Node newNode = new Node(value); 63 | if (root == null) { 64 | root = newNode; 65 | return true; 66 | } 67 | Node temp = root; 68 | while (true) { 69 | if (newNode.value == temp.value) return false; 70 | if (newNode.value < temp.value) { 71 | if (temp.left == null) { 72 | temp.left = newNode; 73 | return true; 74 | } 75 | temp = temp.left; 76 | } else { 77 | if (temp.right == null) { 78 | temp.right = newNode; 79 | return true; 80 | } 81 | temp = temp.right; 82 | } 83 | } 84 | } 85 | 86 | public boolean contains(int value) { 87 | if (root == null) return false; 88 | Node temp = root; 89 | while (temp != null) { 90 | if (value < temp.value) { 91 | temp = temp.left; 92 | } else if (value > temp.value) { 93 | temp = temp.right; 94 | } else { 95 | return true; 96 | } 97 | } 98 | return false; 99 | } 100 | 101 | public ArrayList BFS() { 102 | Node currentNode = root; 103 | Queue queue = new LinkedList<>(); 104 | ArrayList results = new ArrayList<>(); 105 | queue.add(currentNode); 106 | 107 | while (queue.size() > 0) { 108 | currentNode = queue.remove(); 109 | results.add(currentNode.value); 110 | if (currentNode.left != null) { 111 | queue.add(currentNode.left); 112 | } 113 | if (currentNode.right != null) { 114 | queue.add(currentNode.right); 115 | } 116 | } 117 | return results; 118 | } 119 | 120 | public ArrayList DFSPreOrder() { 121 | ArrayList results = new ArrayList<>(); 122 | 123 | class Traverse { 124 | Traverse(Node currentNode) { 125 | results.add(currentNode.value); 126 | if (currentNode.left != null) { 127 | new Traverse(currentNode.left); 128 | } 129 | if (currentNode.right != null) { 130 | new Traverse(currentNode.right); 131 | } 132 | } 133 | } 134 | 135 | new Traverse(root); 136 | return results; 137 | } 138 | 139 | public ArrayList DFSPostOrder() { 140 | ArrayList results = new ArrayList<>(); 141 | 142 | class Traverse { 143 | Traverse(Node currentNode) { 144 | if (currentNode.left != null) { 145 | new Traverse(currentNode.left); 146 | } 147 | if (currentNode.right != null) { 148 | new Traverse(currentNode.right); 149 | } 150 | results.add(currentNode.value); 151 | } 152 | } 153 | 154 | new Traverse(root); 155 | return results; 156 | } 157 | 158 | public ArrayList DFSInOrder() { 159 | ArrayList results = new ArrayList<>(); 160 | 161 | class Traverse { 162 | Traverse(Node currentNode) { 163 | if (currentNode.left != null) { 164 | new Traverse(currentNode.left); 165 | } 166 | results.add(currentNode.value); 167 | if (currentNode.right != null) { 168 | new Traverse(currentNode.right); 169 | } 170 | } 171 | } 172 | 173 | new Traverse(root); 174 | return results; 175 | } 176 | 177 | } 178 | --------------------------------------------------------------------------------