├── .gitignore ├── Arrays and ArrayLists ├── ArrayRotation.java ├── ArrayRotation2.java ├── ArraySubsets.java ├── ArraysLists.java ├── CalculatingRunTime.java ├── MaxPalindromeSubString.java ├── PascalTriangle.java ├── SubArrayUsingRecursion.java ├── SumOfContiguousSubArrays.java └── TwoDimensionalArrayList.java ├── HashMaps ├── CheckSubstring.java └── OrderWithValuesDesc.java ├── HashSets ├── DeleteDuplicates.java └── PangramCheck.java ├── LinkedLists ├── DeleteDuplicateNodes.java ├── MergeTwoSortedLists.java ├── PrintInReverse.java ├── RemoveDuplicates.java ├── RemoveDuplicatesWithValue.java ├── RemoveNthNodeFromLast.java └── ReverseLinkedList.java ├── Queues └── StringRotation.java ├── README.md ├── Stacks ├── ReverseLinkedList.java └── ReverseString.java ├── Trees ├── CalculatingTreeHeight.java ├── LevelOrderTraversal.java ├── PopulateBST.java ├── PostOrderTraversal.java ├── PreOrderTraversal.java ├── SumOfNodes.java ├── TotalNumberOfNodes.java └── TreeHeight.java └── Vault ├── BinarySearchTree.png ├── BinaryTree.png ├── Heading.png └── PreOrderTraversalGIF.gif /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | -------------------------------------------------------------------------------- /Arrays and ArrayLists/ArrayRotation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Array Rotaion using Loops. 3 | * Alternate solution using Queues: https://github.com/sb255/Data-Structures/blob/master/Queues/StringRotation.java 4 | */ 5 | 6 | 7 | public class ArrayRotation { 8 | 9 | public static void main(String args[]){ 10 | 11 | int[] arr = {1,2,3,4,5,6,7,8,9,10}; 12 | int[] arr2 = new int[10]; 13 | int rotation = 5; 14 | 15 | //If roation is greater than the length of the array 16 | if(rotation>arr.length) 17 | rotation = rotation%arr.length; 18 | 19 | for(int i=0; iarr.length) 17 | rotation = rotation%arr.length; 18 | 19 | //Populate the new array with the value at the new index or the rotated index 20 | for(int i=0; i compareLists(List a, List b) { 9 | 10 | List list1 = new ArrayList(); 11 | list1 = a; 12 | 13 | List list2 = new ArrayList(); 14 | list2 = b; 15 | 16 | /* This line is tricky */ 17 | 18 | /* 19 | 20 | Integer[] arr1 = new Integer[list1.size]; 21 | arr1 = list1.toArray(); 22 | The above two lines can be merged and can be written as: 23 | Integer[] arr1 = list1.toArray(new Integer[list1.size()]); 24 | 25 | */ 26 | 27 | Integer[] arr1 = list1.toArray(new Integer[list1.size()]); 28 | 29 | Integer[] arr2 = list2.toArray(new Integer[list2.size()]); 30 | 31 | int aaa=0; 32 | int bbb=0; 33 | 34 | for(int i=0; iarr2[i]) 40 | aaa++; 41 | 42 | else if(arr1[i] arrFinal = new ArrayList(); 47 | arrFinal.add(aaa); 48 | arrFinal.add(bbb); 49 | 50 | 51 | return arrFinal; 52 | 53 | } 54 | 55 | /*-- Inputs from the main method --*/ 56 | 57 | public static void main(String[] args){ 58 | 59 | List A = new ArrayList<>(new Integer(10)); 60 | A.add(20); 61 | A.add(30); 62 | A.add(40); 63 | A.add(50); 64 | 65 | List B = new ArrayList<>(new Integer(30)); 66 | B.add(40); 67 | B.add(40); 68 | B.add(40); 69 | B.add(40); 70 | 71 | List list = compareLists(A,B); 72 | 73 | for(int i=0; i0; i--){ 15 | arr[(Integer.MAX_VALUE)/10000 - i] = i; 16 | } 17 | 18 | /*--- Calculating the run time for sorting using nested for loops: ---*/ 19 | 20 | long timeStarts = System.currentTimeMillis(); //Time starts here 21 | 22 | for(int i=0; iarr[j]) { 25 | int bucket = arr[i]; 26 | arr[i] = arr[j]; 27 | arr[j] = bucket; 28 | } 29 | } 30 | } 31 | 32 | System.out.print("The sorted array in ASC order is:"); 33 | 34 | for(int i=0; i0; i--){ 53 | arr2[(Integer.MAX_VALUE)/10000 - i] = i; 54 | } 55 | 56 | long timeStarts2 = System.currentTimeMillis(); //Time starts here 57 | 58 | Arrays.sort(arr2); 59 | 60 | System.out.print("The sorted array in ASC order is:"); 61 | 62 | for(int i=0; ii; j--){ 28 | 29 | String x = A.substring(i, j); 30 | 31 | reverseOfA = new StringBuilder(x).reverse().toString(); 32 | 33 | if(x.equals(reverseOfA)){ 34 | if(x.length()>a){ 35 | finalValue = x; 36 | a = x.length(); 37 | } 38 | } 39 | 40 | } 41 | } 42 | 43 | return finalValue; 44 | } 45 | 46 | public static void main(String[] args) { 47 | System.out.println(palindromicString("asasasasasasasasa")); 48 | } 49 | 50 | } 51 | 52 | -------------------------------------------------------------------------------- /Arrays and ArrayLists/PascalTriangle.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.ArrayList; 3 | 4 | /* 5 | * Program focusing on returning the Pascal triangle as an ArrayList! 6 | */ 7 | 8 | public class Test { 9 | 10 | static ArrayList> pascalTriangle(int A){ 11 | 12 | ArrayList> rows = new ArrayList<>(); 13 | 14 | int[][] arr = new int[A][A]; 15 | 16 | for(int i=0; i row = new ArrayList<>(); 31 | 32 | for(int j=0; j<=i; j++){ 33 | row.add(arr[i][j]); 34 | } 35 | 36 | rows.add(row); 37 | 38 | } 39 | 40 | return rows; 41 | } 42 | 43 | public static void main(String[] args) { 44 | 45 | ArrayList> arr = pascalTriangle(8); 46 | 47 | for(int i=0; ilimit){ 9 | } 10 | 11 | else if(tiplimit){ 21 | } 22 | 23 | else if(tip> list = new ArrayList<>(); 10 | ArrayList> list = new ArrayList>(); 11 | 12 | */ 13 | 14 | public static void main(String[] args){ 15 | 16 | ArrayList> rows = new ArrayList<>(); 17 | 18 | for(int i=0; i<5; i++){ 19 | ArrayList row = new ArrayList<>(); 20 | for(int j=0; j<5; j++){ 21 | row.add(j*j); 22 | } 23 | rows.add(row); 24 | } 25 | 26 | for(int i=0; i<5; i++){ 27 | for(int j=0; j<5; j++){ 28 | System.out.print(rows.get(i).get(j)+" "); 29 | } 30 | System.out.println(""); 31 | } 32 | 33 | } 34 | 35 | } 36 | 37 | -------------------------------------------------------------------------------- /HashMaps/CheckSubstring.java: -------------------------------------------------------------------------------- 1 | 2 | /*-- Check if B is a substring of A or not, if yes then return the index of first occurrence else return -1 --*/ 3 | 4 | import java.util.*; 5 | 6 | public class CheckSubstring { 7 | 8 | static int subString(String A, String B) { 9 | 10 | if(A.length()==0 || B.length()==0) 11 | return -1; 12 | 13 | if(A.length()==B.length() && A.equals(B)) 14 | return 0; 15 | 16 | HashMap hm = new HashMap<>(); 17 | 18 | for(int i=0; i<(A.length() - B.length()); i++){ 19 | 20 | hm.put(i, A.substring(i, i + B.length())); 21 | 22 | } 23 | 24 | 25 | try{ 26 | for(int i=0; i tm = new TreeMap<>(); 27 | 28 | int total = sc.nextInt(); 29 | 30 | sc.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 31 | 32 | for(int i=0; i st = new Stack<>(); 44 | 45 | tm.entrySet().stream() 46 | .sorted(Map.Entry.comparingByValue()) 47 | .forEach(e->{ 48 | st.push(e.getKey() + " " + e.getValue()); 49 | }); 50 | 51 | 52 | Iterator it = st.iterator(); 53 | 54 | while(it.hasNext()){ 55 | System.out.println(st.pop()); 56 | } 57 | 58 | } 59 | 60 | 61 | public static void main(String[] args){ 62 | 63 | orderWithRating(); 64 | 65 | } 66 | 67 | } 68 | 69 | 70 | -------------------------------------------------------------------------------- /HashSets/DeleteDuplicates.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class DeleteDuplicates { 4 | 5 | /*-- Removing duplicates from Linked List method using HashSet --*/ 6 | static Node removeDuplicatesFromLinkedList(Node head) { 7 | 8 | if(head==null) 9 | return null; 10 | 11 | Node n = head; 12 | LinkedHashSet lhs = new LinkedHashSet<>(); 13 | 14 | while(n.next!=null){ 15 | lhs.add(n.data); 16 | n = n.next; 17 | } 18 | 19 | lhs.add(n.data); 20 | 21 | Iterator it = lhs.iterator(); 22 | 23 | Node newHead = new Node(it.next()); 24 | 25 | Node listNodes = newHead; 26 | 27 | while(it.hasNext()){ 28 | listNodes.next = new Node(it.next()); 29 | listNodes = listNodes.next; 30 | 31 | } 32 | 33 | return newHead; 34 | 35 | } 36 | 37 | 38 | /*-- Inputs from the main method --*/ 39 | 40 | public static void main(String[] args){ 41 | 42 | Node A = new Node(10); 43 | A.next = new Node(10); 44 | A.next.next = new Node(20); 45 | A.next.next.next = new Node(20); 46 | 47 | Node C = removeDuplicatesFromLinkedList(A); 48 | 49 | while(C!=null){ 50 | System.out.println(C.data); 51 | C = C.next; 52 | } 53 | 54 | } 55 | 56 | } 57 | 58 | /*-- Node class Implementation --*/ 59 | 60 | class Node{ 61 | 62 | int data; 63 | Node next; 64 | 65 | public Node(int data){ 66 | this.data = data; 67 | } 68 | 69 | } 70 | 71 | -------------------------------------------------------------------------------- /HashSets/PangramCheck.java: -------------------------------------------------------------------------------- 1 | import java.util.HashSet; 2 | import java.util.Iterator; 3 | import java.util.Map; 4 | 5 | //Checking whether a String is a Pangram or not. 6 | 7 | public class PangramCheck { 8 | 9 | public static void main(String args[]){ 10 | 11 | HashSet hashset = new HashSet<>(); 12 | 13 | String x = "We promptly judged antique ivory buckles for the next prize"; 14 | x = x.replaceAll("\\s", "").trim().toLowerCase(); 15 | 16 | for(int i=0; i lhs = new LinkedHashSet<>(); 16 | 17 | while(n.next!=null){ 18 | lhs.add(n.data); 19 | n = n.next; 20 | } 21 | 22 | lhs.add(n.data); 23 | 24 | Iterator it = lhs.iterator(); 25 | 26 | Node newHead = new Node(it.next()); 27 | 28 | Node listNodes = newHead; 29 | 30 | while(it.hasNext()){ 31 | listNodes.next = new Node(it.next()); 32 | listNodes = listNodes.next; 33 | 34 | } 35 | 36 | return newHead; 37 | 38 | } 39 | 40 | /*-- Inputs from the main method --*/ 41 | 42 | public static void main(String[] args){ 43 | 44 | Node A = new Node(10); 45 | A.next = new Node(10); 46 | A.next.next = new Node(20); 47 | A.next.next.next = new Node(20); 48 | 49 | Node C = removeDuplicatesFromLinkedList(A); 50 | 51 | while(C!=null){ 52 | System.out.println(C.data); 53 | C = C.next; 54 | } 55 | 56 | } 57 | 58 | } 59 | 60 | /*-- Node class Implementation --*/ 61 | 62 | class Node{ 63 | 64 | int data; 65 | Node next; 66 | 67 | public Node(int data){ 68 | this.data = data; 69 | } 70 | 71 | } 72 | 73 | 74 | -------------------------------------------------------------------------------- /LinkedLists/MergeTwoSortedLists.java: -------------------------------------------------------------------------------- 1 | 2 | /*-- Merging two sorted Linked Lists --*/ 3 | 4 | import java.util.*; 5 | 6 | public class MergeTwoSortedLists { 7 | 8 | /*-- Merging two sorted Lists method --*/ 9 | public static Node mergeTwoSortedLists(Node A, Node B) { 10 | 11 | LinkedList list = new LinkedList<>(); 12 | 13 | while(A!=null){ 14 | list.add(A.data); 15 | A = A.next; 16 | } 17 | 18 | while(B!=null){ 19 | list.add(B.data); 20 | B = B.next; 21 | } 22 | 23 | Collections.sort(list); 24 | 25 | Node llist = new Node(list.get(0)); 26 | 27 | Node n = llist; 28 | 29 | for(int i=1; i stack = new Stack<>(); 14 | 15 | if(head.next==null) 16 | System.out.println(n.data); 17 | 18 | while(n.next!=null){ 19 | stack.push(n.data); 20 | n = n.next; 21 | } 22 | 23 | stack.push(n.data); 24 | 25 | while(!stack.isEmpty()){ 26 | System.out.println(stack.pop()); 27 | } 28 | 29 | } 30 | 31 | /*-- Inputs from the main method --*/ 32 | 33 | public static void main(String[] args){ 34 | 35 | Node A = new Node(5); 36 | A.next = new Node(10); 37 | A.next.next = new Node(20); 38 | A.next.next.next = new Node(30); 39 | 40 | printInReverse(A); 41 | 42 | } 43 | 44 | } 45 | 46 | /*-- Node class Implementation --*/ 47 | 48 | class Node{ 49 | 50 | int data; 51 | Node next; 52 | 53 | public Node(int data){ 54 | this.data = data; 55 | } 56 | 57 | } 58 | 59 | 60 | -------------------------------------------------------------------------------- /LinkedLists/RemoveDuplicates.java: -------------------------------------------------------------------------------- 1 | 2 | /*-- Removing duplicates from a LinkedList --*/ 3 | 4 | import java.util.*; 5 | 6 | public class RemoveDuplicates { 7 | 8 | /*-- Method for removing duplicates from a LinkedList --*/ 9 | 10 | static Node removeDuplicateNodes(Node head) { 11 | 12 | Node n = head; 13 | 14 | if(n==null) 15 | return null; 16 | 17 | if(n.next==null) 18 | return head; 19 | 20 | while(n.next!=null){ 21 | 22 | if(n.data==n.next.data){ 23 | n.next=n.next.next; 24 | continue; 25 | } 26 | 27 | n = n.next; 28 | } 29 | 30 | return head; 31 | 32 | } 33 | 34 | /*-- Inputs from the main method --*/ 35 | 36 | public static void main(String[] args){ 37 | 38 | Node A = new Node(5); 39 | A.next = new Node(10); 40 | A.next.next = new Node(20); 41 | A.next.next.next = new Node(20); 42 | 43 | Node C = removeDuplicateNodes(A); 44 | 45 | while(C!=null){ 46 | System.out.println(C.data); 47 | C = C.next; 48 | } 49 | 50 | } 51 | 52 | } 53 | 54 | /*-- Node class Implementation --*/ 55 | 56 | class Node{ 57 | 58 | int data; 59 | Node next; 60 | 61 | public Node(int data){ 62 | this.data = data; 63 | } 64 | 65 | } 66 | 67 | 68 | -------------------------------------------------------------------------------- /LinkedLists/RemoveDuplicatesWithValue.java: -------------------------------------------------------------------------------- 1 | /*-- Remove all the appereances of the Node with values having duplicates --*/ 2 | 3 | import java.util.*; 4 | 5 | public class RemoveDuplicatesWithValue { 6 | 7 | /*-- Method for removing duplicates including the original occurance from a LinkedList --*/ 8 | 9 | static Node removeAllDuplicates(Node A) { 10 | 11 | LinkedHashMap map = new LinkedHashMap<>(); 12 | 13 | while(A!=null){ 14 | 15 | Integer i = map.get(A.data); 16 | 17 | /*--- Getting all the node values ---*/ 18 | 19 | if(i==null) 20 | map.put(A.data, 1); 21 | 22 | else 23 | map.put(A.data, i+1); 24 | 25 | A = A.next; 26 | } 27 | 28 | LinkedList list = new LinkedList<>(); 29 | 30 | for(Integer n : map.keySet()){ 31 | if(map.get(n)==1){ 32 | list.add(new Node(n)); 33 | } 34 | } 35 | 36 | if(list.size()==0) 37 | return null; 38 | 39 | 40 | Node llist = list.get(0); 41 | Node lllist = llist; 42 | 43 | for(int i=1; i stack = new Stack<>(); 10 | 11 | Node n = head; 12 | 13 | while(n!=null){ 14 | stack.push(n.data); 15 | n = n.next; 16 | } 17 | 18 | Node newList = new Node(stack.pop()); 19 | Node nodesNewList = newList; 20 | 21 | while(!stack.isEmpty()){ 22 | nodesNewList.next = new Node(stack.pop()); 23 | nodesNewList = nodesNewList.next; 24 | } 25 | 26 | return newList; 27 | 28 | } 29 | 30 | /*-- Inputs from the main method --*/ 31 | 32 | public static void main(String[] args){ 33 | 34 | Node A = new Node(5); 35 | A.addNodeToTail(10); 36 | A.addNodeToTail(20); 37 | A.addNodeToTail(30); 38 | A.addNodeToTail(35); 39 | A.addNodeToTail(40); 40 | A.addNodeToTail(50); 41 | 42 | Node C = reverseLinkedList(A); 43 | 44 | while(C!=null){ 45 | System.out.println(C.data); 46 | C = C.next; 47 | } 48 | 49 | } 50 | 51 | } 52 | 53 | /*-- Node class Implementation --*/ 54 | 55 | class Node{ 56 | 57 | int data; 58 | Node next; 59 | 60 | public Node(int data){ 61 | this.data = data; 62 | } 63 | 64 | public void addNodeToTail(int addData){ 65 | 66 | Node current = this; 67 | 68 | while(current!=null){ 69 | 70 | if(current.next==null){ 71 | current.next = new Node(addData); 72 | break; 73 | } 74 | 75 | current = current.next; 76 | } 77 | 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /Queues/StringRotation.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.LinkedList; 3 | import java.util.Queue; 4 | 5 | /* 6 | * Left and right of the String using Queue! 7 | * 8 | * Input: "abcde", Roation: 2 9 | * Output: 10 | * Left Rotation: c, d, e, a, b 11 | * Right Rotation: d, e, a, b, c 12 | * 13 | */ 14 | 15 | public class StringRotation { 16 | 17 | public static void main(String[] args) { 18 | 19 | String input = "abcde"; 20 | Queue queue = new LinkedList<>(); //for left rotation 21 | Queue queueCopy = new LinkedList<>(); // for right rotation 22 | 23 | for(int i=0; i tempQueue = q; 43 | 44 | for(int i=0; i tempQueue = q; 56 | int queueSize = tempQueue.size(); 57 | 58 | for(int i=0; i for calculating the length of all types of array (int[], String[], char[]) 41 | 42 | `arr.size()` -> for calculating the size of an object array(Ex. List array as it stores only objects) 43 | 44 | --------------------------------------------------------------------------------------------- 45 | 46 | ## Point to remember 47 | 48 | `.length` -> It is used for calculating the length of all types of array (int[], String[], char[]) 49 | 50 | `.length()` -> It is used for calculating the length of a String 51 | 52 | ------------------------------------------------------------------------------------------------------------------------------ 53 | 54 | ### `Array and List declarations:` 55 | 56 | ```java 57 | /*---- General ways of creating int and String arrays ----*/ 58 | 59 | 60 | int[] intArray1 = new int[10]; /*-- Array size should be provided --*/ 61 | 62 | int[] intArray2 = {0,1,2,3,4,5,6,7,8,9}; /*-- Also a valid declaration --*/ 63 | 64 | int intArray3[] = new int[10]; /*-- Another valid declaration --*/ 65 | 66 | int intArray4[] = {0,1,2,3,4,5,6,7,8,9}; /*-- Another valid declaration --*/ 67 | 68 | String[] stringArray1 = new String[5]; 69 | 70 | String[] stringArray2 = {"Hello", "Hello", "Hello", "Hello", "Hello"}; 71 | 72 | 73 | 74 | 75 | /*---- Creating Lists for Integer objects ----*/ 76 | 77 | 78 | List list1 = new ArrayList(); /*-- Empty List, List size not required --*/ 79 | 80 | List list2 = new ArrayList<>(); /*-- Valid declaration, List size not required --*/ 81 | 82 | List list3 = new ArrayList(new Integer(100)); /*-- Integer object 100 as first element --*/ 83 | 84 | List list4 = new ArrayList<>(new Integer(120)); /*-- Integer object 120 as first element --*/ 85 | 86 | List list5 = new ArrayList(130); /*-- Integer object 130 as first element --*/ 87 | 88 | 89 | 90 | 91 | /*---- Creating Lists for String objects ----*/ 92 | 93 | /*-- We use Arrays.asList(String[] s) and (listName).toArray(String[] s) for dealing with Lists and String arrays--*/ 94 | 95 | 96 | List list6 = new ArrayList(Arrays.asList(new String("Hello"))); /*-- Valid declaration for String --*/ 97 | 98 | List list7 = new ArrayList<>(Arrays.asList(new String("Hello"))); /*-- Valid declaration for String --*/ 99 | 100 | List list8 = new ArrayList<> (Arrays.asList("Hello", "Hi")); /*-- Adding two elements in the list --*/ 101 | 102 | List list9 = new ArrayList<> (Arrays.asList(stringArray2)); /*-- Passing a String array in the asList method --*/ 103 | 104 | 105 | /*---- Referencing ----*/ 106 | 107 | 108 | int[] intArray5 = new int[10]; 109 | 110 | int[] intArray6 = intArray5; 111 | 112 | List list10 = new ArrayList<>(); 113 | 114 | List list11 = list10; 115 | 116 | ``` 117 | 118 | --------------------------------------------------------------------------------------------- 119 |
120 | 121 | ### `Converting List into int[] (until Java 7):` 122 | 123 | ```java 124 | public class Test { 125 | 126 | public static void main(String[] args) { 127 | 128 | List list = new ArrayList(); 129 | list.add(1); 130 | list.add(2); 131 | 132 | int[] intArray = new int[list.size()]; 133 | 134 | for(int i=0; i int */ 137 | 138 | } 139 | 140 | } 141 | 142 | } 143 | 144 | ``` 145 | 146 |
147 | 148 | ### `Converting List into int[] (Java 8):` 149 | 150 | ```java 151 | public class Test { 152 | 153 | public static void main(String[] args) { 154 | 155 | List list = new ArrayList(); 156 | list.add(1); 157 | list.add(2); 158 | 159 | int[] intArray = list.stream().mapToInt(i->i).toArray(); 160 | 161 | } 162 | 163 | } 164 | ``` 165 | 166 |
167 | 168 | ### `Converting int[] into List:` 169 | 170 | ```java 171 | public class Test { 172 | 173 | public static void main(String[] args) { 174 | 175 | int[] intArray = {1,2,3,4,5,6,7}; 176 | 177 | List list = new ArrayList(); 178 | 179 | for(int i : intArray){ 180 | 181 | list.add(i); /* Auto-boxing from int -> Integer */ 182 | 183 | } 184 | 185 | } 186 | 187 | } 188 | 189 | ``` 190 | 191 |
192 | 193 | ### `Converting List into String[]:` 194 | 195 | ```java 196 | public class Test { 197 | 198 | public static void main(String[] args) { 199 | 200 | List list = new ArrayList(); 201 | list.add("Item 1"); 202 | list.add("Item 2"); 203 | list.add("Item 3"); 204 | list.add("Item 4"); 205 | 206 | String[] stringArray = new String[list.size()]; 207 | stringArray = list.toArray(stringArray); 208 | 209 | } 210 | 211 | } 212 | 213 | ``` 214 | 215 |
216 | 217 | ### `Converting String[] into List:` 218 | 219 | ```java 220 | 221 | public class Test { 222 | 223 | public static void main(String[] args) { 224 | 225 | String[] stringArray = {"Hello", "Hi", "Whats up"}; 226 | List list = Arrays.asList(stringArray); 227 | 228 | for(int i=0; i 243 | 244 | ### `Overview of char[] and List (Optional read):` 245 | 246 | ```java 247 | public class Test { 248 | 249 | public static void main(String[] args){ 250 | 251 | String name = "A Character String"; 252 | /*---- Note: char is a data type and Character is the wrapper class ----*/ 253 | char[] nameArray = name.toCharArray(); /*---- Character Array Declaration ----*/ 254 | 255 | 256 | List list = new ArrayList<>(); /*---- List of Character objects ----*/ 257 | list.add('a'); 258 | list.add('b'); 259 | 260 | StringBuilder sb = new StringBuilder(); 261 | 262 | /*-- Now adding elements to String Builder from Character list--*/ 263 | /*--- NOTE: This method for printing elements works for Integers, Strings and Character lists---*/ 264 | for(char ch : list){ 265 | sb.append(ch); 266 | } 267 | 268 | } 269 | 270 | } 271 | ``` 272 | 273 |
274 | 275 | ### `Two Dimensional (2D) Array declaration:` 276 | 277 | ```java 278 | public class Test { 279 | 280 | public static void main(String[] args){ 281 | 282 | ArrayList> rows = new ArrayList<>(); 283 | 284 | for(int i=0; i<5; i++){ 285 | ArrayList row = new ArrayList<>(); 286 | for(int j=0; j<5; j++){ 287 | row.add(j*j); 288 | } 289 | rows.add(row); 290 | } 291 | 292 | for(int i=0; i<5; i++){ 293 | for(int j=0; j<5; j++){ 294 | System.out.print(rows.get(i).get(j)+" "); 295 | } 296 | System.out.println(""); 297 | } 298 | 299 | } 300 | 301 | } 302 | 303 | ``` 304 | 305 | #### `Common ArrayList Operations:` 306 | 307 | | SN | Operation | Method | Time Complexity | 308 | | :---: | :---: | :---: | :---: | 309 | | 01 | Adding an element to the ArrayList | `add(T element)` | O(1) | 310 | | 02 | Setting an element to the ArrayList at a particular index | `set(int index, T element)` | O(1) | 311 | | 03 | Return an element of the ArrayList using Index | `T get(int index)` | O(1) | 312 | | 04 | Getting the size of the ArrayList | `int size()` | O(1) | 313 | | 05 | Removing an element from a particular index in the ArrayList | `remove(int index)` | O(n) | 314 | | 06 | Sorting an ArrayList | `Collections.sort(List list)` | O(nlog(n)) | 315 | 316 | 317 | #### `Array and ArrayList Programs:` 318 | 319 | | SN | Array/ArrayList Program | Java Program File to Demonstrate the Operation | 320 | | :---: | :---: | :---: | 321 | | 01 | General ArrayList Program | [Program File](Arrays%20and%20ArrayLists/ArraysLists.java) | 322 | | 02 | Forming subsets of an Array | [Program File](Arrays%20and%20ArrayLists/ArraySubsets.java) | 323 | | 03 | Calculating run-time of methods | [Program File](Arrays%20and%20ArrayLists/CalculatingRunTime.java) | 324 | | 04 | Finding the max length of Palindrome substring possible | [Program File](Arrays%20and%20ArrayLists/MaxPalindromeSubString.java) | 325 | | 05 | Program to Introduce two dimensional ArrayList | [Program File](Arrays%20and%20ArrayLists/TwoDimensionalArrayList.java) | 326 | | 06 | Pascal Triangle as an ArrayList | [Program File](Arrays%20and%20ArrayLists/PascalTriangle.java) | 327 | | 07 | Array rotation using loops | [Program File](Arrays%20and%20ArrayLists/ArrayRotation.java) | 328 | | 08 | Array rotation using loops 2 | [Program File](Arrays%20and%20ArrayLists/ArrayRotation2.java) | 329 | | 09 | Finding all contiguous subarrays using recursion | [Program File](Arrays%20and%20ArrayLists/SubArrayUsingRecursion.java) | 330 | | 10 | Checking the sum of all contiguous subarrays using recursion | [Program File](Arrays%20and%20ArrayLists/SumOfContiguousSubArrays.java) | 331 | 332 | --------------------------------------------------------------------------------------------- 333 | 334 | # LinkedLists: 335 | 336 | LinkedList is a class in Java and also a Data Structure. It contains nodes. 337 | 338 | ### `LinkedList class:` 339 | 340 | ```java 341 | /*-- Node class Implementation --*/ 342 | 343 | class Node{ 344 | 345 | int data; 346 | Node next; 347 | 348 | public Node(int data){ 349 | this.data = data; 350 | } 351 | } 352 | ``` 353 | 354 | ### `LinkedList method addNodeToTail:` 355 | 356 | ```java 357 | 358 | /*-- Method to add a Node to the tail of a LinkedList --*/ 359 | 360 | public void addNodeToTail(int addData){ 361 | 362 | Node current = this; 363 | 364 | while(current!=null){ 365 | 366 | if(current.next==null){ 367 | current.next = new Node(addData); 368 | break; 369 | } 370 | 371 | current = current.next; 372 | } 373 | 374 | } 375 | ``` 376 | 377 | #### `Common LinkedList Operations:` 378 | 379 | | SN | Operation | Method | Time Complexity | 380 | | :---: | :---: | :---: | :---: | 381 | | 01 | Adding an element of the LinkedList | `add(T element)` | O(1) | 382 | | 02 | Getting the size of the LinkedList | `int size()` | O(1) | 383 | | 03 | Clearing the LinkedList | `clear()` | O(n) | 384 | | 04 | Replacing an element from a particular index with another element | `add(int index, T element)` | O(n) | 385 | 386 | 387 | #### `LinkedList Programs:` 388 | 389 | | SN | LinkedList Operation | Java Program File to Demonstrate the Operation | 390 | | :---: | :---: | :---: | 391 | | 01 | Deleting duplicate values from the LinkedList | [Program File](LinkedLists/DeleteDuplicateNodes.java) | 392 | | 02 | Merge two sorted LinkedList | [Program File](LinkedLists/MergeTwoSortedLists.java) | 393 | | 03 | Print the LinkedList in reverse order | [Program File](LinkedLists/PrintInReverse.java) | 394 | | 04 | Removing duplicates while keeping the real value | [Program File](LinkedLists/RemoveDuplicates.java) | 395 | | 05 | Removing duplicates and removing the original Value | [Program File](LinkedLists/RemoveDuplicatesWithValue.java) | 396 | | 06 | Removing Nth Node from the tail of the LinkedList | [Program File](LinkedLists/RemoveNthNodeFromLast.java) | 397 | | 07 | Reversing the LinkedList and returning the Head Node | [Program File](LinkedLists/ReverseLinkedList.java) | 398 | 399 | --------------------------------------------------------------------------------------------- 400 | # Stacks and Queues: 401 | 402 | Stack is a class in Java while Queue is an Interface, so both will have different kinds of declaration. Stack will have a regular declaration of Java class initialization while Queue Interface can be implemented with a LinkedList.
403 | 404 | ``` 405 | Stack stack = new Stack(); 406 | Queue queue = new LinkedList(); 407 | ``` 408 | 409 | **Time Complexity**: Stack based operation such as pop(), push(), peek() takes **O(1)** time. Queue based operations such as add(), poll(), peek() also takes **O(1)** time. It means that the time taken for Stack operations and Queue operations is constant. 410 | 411 | #### `Stack Operations:` 412 | 413 | | SN | Operation | Method | Time Complexity | 414 | | :---: | :---: | :---: | :---: | 415 | | 01 | Adding an element to the Stack | `push()` | O(1) | 416 | | 02 | Removing an element from the Stack | `T pop()` | O(1) | 417 | | 03 | Viewing the element at top of the Stack | `T peek()` | O(1) | 418 | | 04 | Checking if the Stack is empty or not | `boolean isEmpty()` | O(1) | 419 | 420 | #### `Queue Operations:` 421 | 422 | | SN | Operation | Terminology | Method | Time Complexity | 423 | | :---: | :---: | :---: | :---: | :---: | 424 | | 01 | Adding an element to the Queue | Enqueue | `add(T element)` | O(1) | 425 | | 02 | Removing an element from the Queue | Dequeue | `T poll()` | O(1) | 426 | | 03 | Viewing the element at top of the Queue | | `T peek()` | O(1) | 427 | 428 | --------------------------------------------------------------------------------------------- 429 | # Sets: 430 | 431 | `HashSet, TreeSet and LinkedHashSet` also uses concept of Hashing like Maps for storing the data and also `It does not contains duplicate values!!` 432 | 433 | * `HashSet` - Stores the added values in the `random order without duplicates.` 434 | * `TreeSet` - Stores the added values in the `naturally ordered way without duplicates.` 435 | * `LinkedHashSet` - Stores the added values in the `order of insertion without duplicates.` 436 | 437 | Let us see the working of a `HashSet`, `TreeSet` and a `LinkedHashSet`: 438 | 439 | ### `Implementation of a HashSet:` 440 | 441 | ```java 442 | int intValue = 87611122; 443 | String input = Integer.toString(intValue); 444 | 445 | Set hashSet = new HashSet(); 446 | 447 | for(int i=0; i it = hashSet.iterator(); 454 | 455 | while(it.hasNext()){ 456 | System.out.print(it.next()+" "); 457 | } 458 | ``` 459 | 460 | `Output:` 461 | 462 | ``` 463 | 1 2 6 7 8 464 | ``` 465 | 466 | 467 | ### `Implementation of a TreeSet:` 468 | 469 | ```java 470 | int intValue = 87611122; 471 | String input = Integer.toString(intValue); 472 | 473 | Set treeSet = new TreeSet(); 474 | 475 | for(int i=0; i it = treeSet.iterator(); 482 | 483 | while(it.hasNext()){ 484 | System.out.print(it.next()+" "); 485 | } 486 | ``` 487 | 488 | `Output:` 489 | 490 | ``` 491 | 1 2 6 7 8 492 | ``` 493 | 494 | ### `Implementation of a LinkedHashSet:` 495 | 496 | ```java 497 | int intValue = 87611122; 498 | String input = Integer.toString(intValue); 499 | 500 | Set treeSet = new LinkedHashSet(); 501 | 502 | for(int i=0; i it = treeSet.iterator(); 509 | 510 | while(it.hasNext()){ 511 | System.out.print(it.next()+" "); 512 | } 513 | ``` 514 | 515 | `Output:` 516 | 517 | ``` 518 | 8 7 6 1 2 519 | ``` 520 | 521 | #### `Set Operations:` 522 | 523 | | SN | Operation | Method | Time Complexity | 524 | | :---: | :---: | :---: | :---: | 525 | | 01 | Adding an element to the Set | `add()` | O(1) | 526 | | 02 | Get an Iterator object of the Set elements | `Iterator iterator()` | O(n) | 527 | 528 | --------------------------------------------------------------------------------------------- 529 | 530 | # Maps: 531 | 532 | `HashMap, TreeMap and LinkedHashMap:` 533 | 534 | ## Map is an Interface in Java while HashMap, TreeMap and LinkedHashMap are classes. 535 | 536 | ### HashMap, TreeMap and LinkedHashMap: 537 | 538 | * `HashMap` - Stores keys and values in an `unordered way` and `contains only unique keys.` 539 | * `TreeMap` - Stores keys and values in a `naturally ordered way` and `contains only unique keys.` 540 | * `LinkedHashMap` - Stores keys and values in the `order of keys insertions` and `contains only unique keys.` 541 | 542 | ### HashMap, TreeMap and LinkedHashMap can be used for the following kind of problems: 543 | 544 | * Find whether a substring is part of a String or not! 545 | * How many times a letter is occurring in a String? 546 | * Arrange the words of a String in ASC order of their length! 547 | 548 | ``` 549 | Map hm1 = new HashMap(); 550 | Map hm2 = new HashMap(); 551 | ``` 552 | 553 | Let us see the output of the code for a `HashMap`, `TreeMap` and a `LinkedHashMap`: 554 | 555 | ### `Implementation of a HashMap:` 556 | 557 | ```java 558 | //Counting the occurrence of digits in a number! 559 | int intValue = 87611122; 560 | String stringValue = Integer.toString(intValue); 561 | int[] input = new int[stringValue.length()]; 562 | int inputLength = input.length; 563 | 564 | for(int i=0; i hm = new HashMap<>(); 569 | 570 | for(int i=0; i charCounts = new HashMap<>(); 608 | 609 | for (int i = 0; i < inputLength; ++i){ 610 | 611 | int digit = input[i]; 612 | 613 | if (!charCounts.containsKey(digit)) 614 | charCounts.put(digit, 1); 615 | 616 | else 617 | charCounts.put(digit, charCounts.get(digit) + 1); 618 | 619 | } 620 | 621 | System.out.println(charCounts); 622 | ``` 623 | 624 | `Output:` 625 | 626 | ``` 627 | {1=3, 2=2, 6=1, 7=1, 8=1} 628 | ``` 629 | 630 | 631 | ### `Implementation of a TreeMap:` 632 | 633 | ```java 634 | //Counting the occurrence of digits in a number! 635 | int intValue = 87611122; 636 | String stringValue = Integer.toString(intValue); 637 | int[] input = new int[stringValue.length()]; 638 | int inputLength = input.length; 639 | 640 | for(int i=0; i tm = new TreeMap<>(); 645 | 646 | for(int i=0; i lm = new LinkedHashMap<>(); 682 | 683 | for(int i=0; i hm = new HashMap<>(); 717 | 718 | for(String word : input.split(" ")){ 719 | 720 | hm.put(word, word.length()); 721 | 722 | } 723 | 724 | /*--- Printing the keys and values of a HashMap using Lambdas: ---*/ 725 | 726 | hm.entrySet().forEach(e->{ 727 | System.out.println(e.getKey() + " " + e.getValue()); 728 | }); 729 | 730 | /*--- Printing the values of a HashMap using conventional for loop: ---*/ 731 | 732 | for(Entry e : hm.entrySet()){ 733 | System.out.println(e.getKey() + " "+ e.getValue()); 734 | } 735 | 736 | } 737 | 738 | } 739 | ``` 740 | ### `Printing the Keys of a Map in the ASC order of its Values:` 741 | 742 | ``` 743 | Input String: I have some work for all of you guys. 744 | ``` 745 | 746 | ``` 747 | Output String: I of for all you have some work guys 748 | ``` 749 | 750 | ```java 751 | public class Test { 752 | 753 | public static void main(String[] args) { 754 | 755 | String sentence = "I have some work for all of you guys."; 756 | 757 | String input = sentence.substring(0, sentence.length()-1); 758 | 759 | LinkedHashMap lhm = new LinkedHashMap<>(); 760 | 761 | for(String word : input.split(" ")){ 762 | 763 | lhm.put(word, word.length()); 764 | 765 | } 766 | 767 | /*--- Printing the keys and values in the order of ASC order of the values: ---*/ 768 | 769 | lhm.entrySet().stream() 770 | .sorted(Map.Entry.comparingByValue()) 771 | .forEach(e->{ 772 | System.out.print(e.getKey() + " "); 773 | }); 774 | 775 | } 776 | 777 | } 778 | ``` 779 | #### `Common Map Operations:` 780 | 781 | | SN | Operation | Method | Time Complexity | 782 | | :---: | :---: | :---: | :---: | 783 | | 01 | Adding a key and a value to the Map | `put(T key, T value)` | O(1) | 784 | | 02 | Getting the value of a key in a Map | `T get(T key)` | O(1) | 785 | | 03 | Checking if a key exists in a Map | `boolean containsKey(T key)` | O(1) | 786 | | 04 | Getting a set view of all the keys in a Map | `Set keySet()` | | 787 | | 05 | Getting a set view of all the keys and the values in a Map | `Set entrySet()` | | 788 | 789 | 790 | #### `Map/Set Programs:` 791 | 792 | | SN | Program Function | Application | Java Program File to Demonstrate the Operation | 793 | | :---: | :---: | :---: | :---: | 794 | | 01 | Checking Substring | HashMap | [Program File](HashMaps/CheckSubstring.java) | 795 | | 02 | Ordering values using a TreeMap | TreeMap | [Program File](HashMaps/OrderWithValuesDesc.java) | 796 | | 03 | Deleting duplicates from a LinkedList | LinkedHashSet | [Program File](HashSets/DeleteDuplicates.java) | 797 | | 04 | Pangram check on a String | HashSet | [Program File](HashSets/PangramCheck.java) | 798 | 799 | --------------------------------------------------------------------------------------------- 800 | 801 | # Trees: 802 | 803 | ### Tree: 804 | 805 | * Binary Tree 806 | * Binary Search Tree(BST) 807 | 808 | ### Tree-Algorithms: 809 | * Level-Order Traversal `(Breadth First Search)` 810 | * Pre-Order Traversal `(Depth First Search)` 811 | * Post-Order Traversal `(Depth First Search)` 812 | * In-Order Traversal `(Depth First Search)` 813 | 814 | #### `Binary Tree:` 815 | A Tree data structure where each node can at-most have 2 children i.e. a left child and a right child! 816 | 817 | ### `Tree Node class for Binary Tree and Binary Search Tree(BST):` 818 | 819 | ```java 820 | class TreeNode{ 821 | TreeNode left,right; 822 | int dValue; 823 | 824 | TreeNode(int v){ 825 | dValue = v; 826 | left = null; 827 | right = null; 828 | } 829 | } 830 | ``` 831 | 832 | ![alt text](Vault/BinaryTree.png "Binary Tree") 833 | 834 | #### `PreOrderTraversal in a Binary Tree:` 835 | 836 | 837 | 838 | #### `Binary Search Tree(BST):` 839 | A Binary Tree where the left child is less than the root and the right child is greater than the root and each node can have at-most have 2 children! All the nodes in the left subtree are less than the root node and all the nodes in the right subtree are greater than the root node. All the subtrees in a BST are BST. 840 | 841 | 842 | ### `Tree Node class for Binary Tree and Binary Search Tree(BST):` 843 | 844 | ```java 845 | class TreeNode{ 846 | TreeNode left,right; 847 | int dValue; 848 | 849 | TreeNode(int v){ 850 | dValue = v; 851 | left = null; 852 | right = null; 853 | } 854 | } 855 | ``` 856 | 857 | ![alt text](Vault/BinarySearchTree.png "Binary Search Tree") 858 | 859 | 860 | #### `Tree Programs:` 861 | 862 | | SN | Tree Program | Search Name | Java Program File to Demonstrate the Operation | 863 | | :---: | :---: | :---: | :---: | 864 | | 01 | Pre-Order Traversal in a Binary Tree | Depth First Search | [Program File](Trees/PreOrderTraversal.java) | 865 | | 02 | Post-Order Traversal in a Binary Tree | Depth First Search | [Program File](Trees/PostOrderTraversal.java) | 866 | | 03 | Level-Order Traversal in a Binary Tree | Breadth First Search | [Program File](Trees/LevelOrderTraversal.java) | 867 | | 04 | Populating a Binary Search Tree (BST) | | [Program File](Trees/PopulateBST.java) | 868 | | 05 | Calculating the height of a Binary Tree | | [Program File](Trees/TreeHeight.java) | 869 | | 06 | Calculating the number of nodes in a Binary Tree using recursion | | [Program File](Trees/TotalNumberOfNodes.java) | 870 | | 07 | Calculating the sum of all the nodes in a Binary Tree | | [Program File](Trees/SumOfNodes.java) | 871 | | 08 | Calculating the height of a Binary Tree II | | [Program File](Trees/CalculatingTreeHeight.java) | 872 | 873 | --------------------------------------------------------------------------------------------- 874 | -------------------------------------------------------------------------------- /Stacks/ReverseLinkedList.java: -------------------------------------------------------------------------------- 1 | /*-- Reversing a LinkedList using Stack --*/ 2 | 3 | import java.util.*; 4 | 5 | public class ReverseLinkedList { 6 | 7 | 8 | 9 | /*------------------- Reversing a LinkedList using Stack ------------------------------------------*/ 10 | 11 | 12 | 13 | static Node reverseLinkedList(Node head) { 14 | 15 | Stack stack = new Stack<>(); 16 | 17 | Node n = head; 18 | 19 | while(n!=null){ 20 | stack.push(n.data); 21 | n = n.next; 22 | } 23 | 24 | Node newList = new Node(stack.pop()); 25 | Node nodesNewList = newList; 26 | 27 | while(!stack.isEmpty()){ 28 | nodesNewList.next = new Node(stack.pop()); 29 | nodesNewList = nodesNewList.next; 30 | } 31 | 32 | return newList; 33 | 34 | } 35 | 36 | 37 | /*-------------------------------------------------------------------------------------------------*/ 38 | 39 | 40 | /*-- Inputs from the main method --*/ 41 | 42 | public static void main(String[] args){ 43 | 44 | Node A = new Node(5); 45 | A.addNodeToTail(10); 46 | A.addNodeToTail(20); 47 | A.addNodeToTail(30); 48 | A.addNodeToTail(35); 49 | A.addNodeToTail(40); 50 | A.addNodeToTail(50); 51 | 52 | Node C = reverseLinkedList(A); 53 | 54 | while(C!=null){ 55 | System.out.println(C.data); 56 | C = C.next; 57 | } 58 | 59 | } 60 | 61 | } 62 | 63 | /*-- Node class Implementation --*/ 64 | 65 | class Node{ 66 | 67 | int data; 68 | Node next; 69 | 70 | public Node(int data){ 71 | this.data = data; 72 | } 73 | 74 | public void addNodeToTail(int addData){ 75 | 76 | Node current = this; 77 | 78 | while(current.next!=null){ 79 | current = current.next; 80 | } 81 | 82 | current.next = new Node(addData); 83 | 84 | } 85 | 86 | } 87 | 88 | -------------------------------------------------------------------------------- /Stacks/ReverseString.java: -------------------------------------------------------------------------------- 1 | //Reverse the order of words in a String using Stack! 2 | 3 | import java.util.*; 4 | 5 | 6 | public class ReverseString { 7 | 8 | public static void main(String[] args){ 9 | 10 | String intro = "How are you doing today"; 11 | 12 | Stack st = new Stack(); 13 | 14 | for(String s : intro.split(" ")){ 15 | 16 | st.push(s); 17 | 18 | } 19 | 20 | int size = st.size(); 21 | 22 | for(int i=0; i(1 + heightOfTree(node.rightNode))?(1 + heightOfTree(node.leftNode)):(1 + heightOfTree(node.rightNode)); 62 | 63 | } 64 | 65 | public static void main(String[] args){ 66 | 67 | //-- POPULATING THE BINARY TREE 68 | 69 | TreeNode root = new TreeNode(10); 70 | root.leftNode = new TreeNode(20); 71 | root.rightNode = new TreeNode(30); 72 | root.leftNode.leftNode = new TreeNode(40); 73 | root.leftNode.rightNode = new TreeNode(50); 74 | root.rightNode.leftNode = new TreeNode(60); 75 | root.rightNode.rightNode = new TreeNode(70); 76 | root.leftNode.leftNode.leftNode = new TreeNode(80); 77 | root.leftNode.leftNode.rightNode = new TreeNode(90); 78 | root.rightNode.rightNode.leftNode = new TreeNode(100); 79 | root.rightNode.rightNode.rightNode = new TreeNode(110); 80 | 81 | int value = heightOfTree(root); 82 | System.out.println("THE HEIGHT OF THE TREE IS: "+value); 83 | 84 | } 85 | } 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /Trees/LevelOrderTraversal.java: -------------------------------------------------------------------------------- 1 | //-- LEVEL ORDER TRAVERSAL IN A BINARY TREE 2 | 3 | import java.util.*; 4 | 5 | public class LevelOrderTraversal { 6 | 7 | //Tree class implementation for Binary Tree 8 | static class TreeNode{ 9 | TreeNode left,right; 10 | int data; 11 | 12 | TreeNode(int data){ 13 | this.data = data; 14 | left = null; 15 | right = null; 16 | } 17 | 18 | } 19 | 20 | public static void levelOrderTraversal(TreeNode root){ 21 | 22 | Queue nodeQueue = new LinkedList<>(); 23 | nodeQueue.add(root); 24 | 25 | while(!nodeQueue.isEmpty()){ 26 | 27 | TreeNode currentNode = nodeQueue.poll(); 28 | 29 | if(currentNode.left!=null) 30 | nodeQueue.add(currentNode.left); 31 | 32 | if(currentNode.right!=null) 33 | nodeQueue.add(currentNode.right); 34 | 35 | System.out.println(currentNode.data); 36 | 37 | } 38 | 39 | } 40 | 41 | 42 | public static void main(String args[]){ 43 | 44 | //Creating a BINARY TREE 45 | TreeNode root = new TreeNode(10); 46 | root.left = new TreeNode(20); 47 | root.right = new TreeNode(30); 48 | root.left.left = new TreeNode(40); 49 | root.left.right = new TreeNode(50); 50 | root.right.left = new TreeNode(60); 51 | root.right.right = new TreeNode(70); 52 | 53 | //Perfoming level order traversal in the Binary Tree 54 | levelOrderTraversal(root); 55 | 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Trees/PopulateBST.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.LinkedList; 3 | import java.util.Queue; 4 | 5 | 6 | //-- POPULATING A BINARY SEARCH TREE!! 7 | 8 | public class PopulateBST{ 9 | 10 | static class TreeNode{ 11 | TreeNode left,right; 12 | int dValue; 13 | 14 | TreeNode(int v){ 15 | dValue = v; 16 | left = null; 17 | right = null; 18 | } 19 | } 20 | 21 | public static void levelOrderTraversal(TreeNode root){ 22 | 23 | Queue nodeQueue = new LinkedList<>(); 24 | nodeQueue.add(root); 25 | 26 | while(!nodeQueue.isEmpty()){ 27 | 28 | TreeNode currentNode = nodeQueue.poll(); 29 | 30 | if(currentNode.left!=null) 31 | nodeQueue.add(currentNode.left); 32 | 33 | if(currentNode.right!=null) 34 | nodeQueue.add(currentNode.right); 35 | 36 | System.out.println(currentNode.dValue); 37 | 38 | } 39 | 40 | } 41 | 42 | //-- POPULATING THE BINARY SEARCH TREE USING THE METHOD populateBST 43 | public static void populateBST(TreeNode root, TreeNode addNode){ 44 | 45 | if(addNode.dValueroot.dValue){ 55 | 56 | if(root.right==null) 57 | root.right = addNode; 58 | 59 | else 60 | populateBST(root.right, addNode); 61 | } 62 | 63 | else{ 64 | //DO NOTHING 65 | } 66 | 67 | } 68 | 69 | public static void main(String[] args){ 70 | 71 | TreeNode root = new TreeNode(50); 72 | 73 | populateBST(root, new TreeNode(10)); 74 | populateBST(root, new TreeNode(20)); 75 | populateBST(root, new TreeNode(30)); 76 | populateBST(root, new TreeNode(40)); 77 | populateBST(root, new TreeNode(50)); 78 | populateBST(root, new TreeNode(60)); 79 | populateBST(root, new TreeNode(70)); 80 | populateBST(root, new TreeNode(80)); 81 | populateBST(root, new TreeNode(90)); 82 | 83 | //Level Order Traversal in the above Tree 84 | levelOrderTraversal(root); 85 | 86 | 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /Trees/PostOrderTraversal.java: -------------------------------------------------------------------------------- 1 | /*----- Post Order Traversal in a Tree -----*/ 2 | 3 | import java.util.*; 4 | 5 | class Node{ 6 | Node left,right; 7 | int dValue; 8 | 9 | Node(int v){ 10 | dValue = v; 11 | left = null; 12 | right = null; 13 | } 14 | } 15 | 16 | 17 | public class PostOrderTraversal { 18 | public static ArrayList postorderTraversal(Node A) { 19 | 20 | ArrayList list = new ArrayList<>(); 21 | Stack stack = new Stack<>(); 22 | Stack stackReverse = new Stack<>(); 23 | 24 | stack.push(A); 25 | 26 | while(!stack.empty()){ 27 | 28 | Node node = stack.peek(); 29 | int data = stack.pop().dValue; 30 | stackReverse.push(data); 31 | 32 | if(node.left!=null) 33 | stack.push(node.left); 34 | 35 | if(node.right!=null) 36 | stack.push(node.right); 37 | 38 | } 39 | 40 | while(!stackReverse.empty()){ 41 | list.add(stackReverse.pop()); 42 | } 43 | 44 | 45 | return list; 46 | } 47 | 48 | public static void main(String args[]){ 49 | Node root = new Node(10); 50 | root.left = new Node(20); 51 | root.right = new Node(30); 52 | root.left.left = new Node(40); 53 | root.left.right = new Node(50); 54 | root.right.left = new Node(60); 55 | root.right.right = new Node(70); 56 | 57 | ArrayList list = postorderTraversal(root); 58 | 59 | for(int i=0; i preOrderTraversal(Node A) { 19 | 20 | ArrayList list = new ArrayList<>(); 21 | 22 | Stack stack = new Stack<>(); 23 | 24 | stack.push(A); 25 | 26 | while(!stack.empty()){ 27 | 28 | Node node = stack.pop(); 29 | list.add(node.dValue); 30 | 31 | if(node.right!=null) 32 | stack.push(node.right); 33 | 34 | if(node.left!=null) 35 | stack.push(node.left); 36 | } 37 | 38 | return list; 39 | 40 | } 41 | 42 | public static void main(String args[]){ 43 | Node root = new Node(10); 44 | root.left = new Node(20); 45 | root.right = new Node(30); 46 | root.left.left = new Node(40); 47 | root.left.right = new Node(50); 48 | root.right.left = new Node(60); 49 | root.right.right = new Node(70); 50 | 51 | ArrayList list = preOrderTraversal(root); 52 | 53 | for(int i=0; iheight2?(height1+1):(height2+1); 30 | 31 | } 32 | 33 | public static void main(String args[]){ 34 | TreeNode root = new TreeNode(10); 35 | root.left = new TreeNode(20); 36 | root.right = new TreeNode(30); 37 | root.left.left = new TreeNode(40); 38 | root.left.right = new TreeNode(50); 39 | root.right.left = new TreeNode(60); 40 | root.right.right = new TreeNode(70); 41 | 42 | System.out.println("The Height of the Tree is: "+getTreeHeight(root)); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Vault/BinarySearchTree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sb255/Data-Structures/cc24900cc8b79df3067a36f7d53ed4c5113b7226/Vault/BinarySearchTree.png -------------------------------------------------------------------------------- /Vault/BinaryTree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sb255/Data-Structures/cc24900cc8b79df3067a36f7d53ed4c5113b7226/Vault/BinaryTree.png -------------------------------------------------------------------------------- /Vault/Heading.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sb255/Data-Structures/cc24900cc8b79df3067a36f7d53ed4c5113b7226/Vault/Heading.png -------------------------------------------------------------------------------- /Vault/PreOrderTraversalGIF.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sb255/Data-Structures/cc24900cc8b79df3067a36f7d53ed4c5113b7226/Vault/PreOrderTraversalGIF.gif --------------------------------------------------------------------------------