├── AVLTree ├── AvlTree Delete │ └── AvlTree.java └── AvlTree Insert │ └── AvlTree.java ├── Array ├── ArrayDeletion │ └── ArrayDeletion.java ├── ArrayInsert │ └── ArrayInsertion.java └── FullArray │ └── Array.java ├── ArrayList └── ArrayListImplementation │ ├── ArrayList.java │ └── ArrayListImplementation.java ├── B-Tree └── B-Tree Insert │ └── BTree.java ├── BinarySearchTree ├── BinarySearchTree 1 │ ├── BinarySearchTree.java │ └── BinarySearchTreeImplementation.java ├── BinarySearchTree 2 │ ├── BinarySearchTree │ │ └── BinarySearchTree.java │ ├── BinarySearchTreeImplementation │ │ └── BinarySearchTreeImplementation.java │ └── Node │ │ └── Node.java └── BinarySearchTree 3 │ └── BinarySearchTree.java ├── DisjointSet └── DisjointSetImplementation │ ├── DisjointSet.java │ └── DisjointSetImplementation.java ├── DoublyLinkedList ├── DoublyLinkedList │ └── DoublyLinkedList.java └── DoublyLinkedListImplementation │ ├── DoublyLinkedList.java │ ├── DoublyLinkedListImplementation.java │ └── Node.java ├── LICENSE ├── LinkedList ├── CircularLinkedList │ └── CircularLinkedList.java ├── LinkedList SwapNodes │ └── LinkedList.java ├── LinkedList │ └── LinkedList.java └── LinkedListImplementation │ ├── LinkedList.java │ └── LinkedListImplementation.java ├── PriorityQueue └── PriorityQueueImplementation │ ├── PriorityQueue.java │ └── PriorityQueueImplementation.java ├── Queue ├── DequeueImplementation │ ├── Dequeue.java │ └── DequeueImplementation.java ├── QueueImplementation │ ├── Queue.java │ └── QueueImplementation.java ├── QueuewithArray │ └── Queue.java └── QueuewithLinkedList │ └── Queue.java ├── README.md ├── Red-Black Tree └── RedBlackTree.java ├── Splay Tree └── SplayTree.java └── Stack ├── StackImplementation ├── MyStack.java └── StackImplementation.java ├── StackwithArray └── Stack.java └── StackwithLinkedList └── Stack.java /AVLTree/AvlTree Delete/AvlTree.java: -------------------------------------------------------------------------------- 1 | //Program to implement AVL Tree. Insertion, Search and Deletion. 2 | public class AvlTree{ 3 | static class Node{ 4 | int data; //data value 5 | Node left; //points to left child 6 | Node right; //points to right child 7 | Node parent;//points to parent node. 8 | int height; //height of the tree. 9 | 10 | public Node(int data){ 11 | this.data = data; 12 | this.left = null; 13 | this.right = null; 14 | this.parent = null; 15 | this.height = 1; 16 | } 17 | } 18 | 19 | static Node root = null; //root of the tree. 20 | 21 | //function to add new elements in the tree. 22 | public static void add(int data){ 23 | System.out.println("Inserting node with data : " + data ); 24 | Node node = new Node(data); 25 | if(root == null){ 26 | root = node; 27 | return; 28 | } 29 | 30 | Node temp = root; 31 | while(temp!=null){ 32 | if(temp.datadata){ 46 | if(temp.left==null){ 47 | temp.left=node; 48 | node.parent = temp; 49 | Node prob = updateHeight(node); //updation of height after inserting the node. 50 | if(prob!=null){ 51 | rotations(prob); //perform rotations to balance the tree. 52 | } 53 | return; 54 | } 55 | temp = temp.left; 56 | continue; 57 | } 58 | } 59 | } 60 | 61 | //fuction for PreOrder Traversal of the tree. 62 | private static void preOrder(Node node){ 63 | if(node==null){ 64 | return; 65 | } 66 | System.out.print(node.data + " "); 67 | preOrder(node.left); 68 | preOrder(node.right); 69 | } 70 | 71 | 72 | //function for performing the rotations in the tree. 73 | private static void rotations(Node node){ 74 | if(node==null){ 75 | return; 76 | } 77 | System.out.println("Problematic Node : " + node.data); 78 | int leftC = 0; //left child 79 | int rightC = 0; // right child. 80 | 81 | if(node.left != null){ 82 | leftC = node.left.height; 83 | } 84 | if(node.right != null){ 85 | rightC = node.right.height; 86 | } 87 | 88 | int leftgC = 0; //left grand child 89 | int rightgC = 0; //right grand child 90 | 91 | if(leftC>rightC){ 92 | Node left = node.left; 93 | if(left.left!=null){ 94 | leftgC = left.left.height; 95 | } 96 | if(left.right!=null){ 97 | rightgC = left.right.height; 98 | } 99 | 100 | //right rotation. 101 | if(leftgC>rightgC){ 102 | rotateRight(left); 103 | node = updateHeight(left); 104 | if(node!=null){ 105 | rotations(node); 106 | } 107 | } 108 | 109 | //left rotation then right rotation. 110 | else{ 111 | Node leftR = left.right; 112 | rotateLeft(leftR); 113 | rotateRight(leftR); 114 | node = updateHeight(leftR); 115 | if(node!=null){ 116 | rotations(node); 117 | } 118 | } 119 | } 120 | else{ 121 | Node right = node.right; 122 | if(right.left!=null){ 123 | leftgC = right.left.height; 124 | } 125 | if(right.right!=null){ 126 | rightgC = right.right.height; 127 | } 128 | 129 | //left rotation. 130 | if(leftgC1){ 180 | return node; 181 | } 182 | node = node.parent; 183 | } 184 | return null; 185 | } 186 | 187 | 188 | //function to calculate the balance factor i.e bf=(left.child.height-right.child.height) 189 | private static int calcBalance(Node temp){ 190 | int leftheight = 0; 191 | if(temp.left!=null){ 192 | leftheight = temp.left.height; 193 | } 194 | int rightheight = 0; 195 | if(temp.right!=null){ 196 | rightheight = temp.right.height; 197 | } 198 | return Math.abs(leftheight-rightheight); 199 | } 200 | 201 | //function for performing the right rotation. 202 | private static void rotateRight(Node temp){ 203 | System.out.println("Rotating Right node : " + temp.data); 204 | Node right = temp.right; 205 | Node gparent = temp.parent.parent; 206 | temp.right = temp.parent; 207 | temp.parent.left = right; 208 | if(right!=null){ 209 | right.parent = temp.parent; 210 | } 211 | temp.parent.parent = temp; 212 | temp.parent = gparent; 213 | if(gparent==null){ 214 | root = temp; 215 | } 216 | 217 | else{ 218 | if(gparent.right==temp.right){ 219 | gparent.right = temp; 220 | } 221 | 222 | else{ 223 | gparent.left = temp; 224 | } 225 | } 226 | calcHeight(temp); 227 | } 228 | 229 | //function for performing the left rotation. 230 | private static void rotateLeft(Node temp){ 231 | System.out.println("Rotating Left node : " + temp.data); 232 | Node gparent = temp.parent.parent; 233 | Node left = temp.left; 234 | temp.left = temp.parent; 235 | temp.parent.right = left; 236 | if(left!=null){ 237 | left.parent = temp.parent; 238 | } 239 | temp.parent.parent = temp; 240 | temp.parent = gparent; 241 | if(gparent==null){ 242 | root = temp; 243 | } 244 | else{ 245 | if(gparent.left==temp.left){ 246 | gparent.left = temp; 247 | } 248 | 249 | else{ 250 | gparent.right = temp; 251 | } 252 | } 253 | calcHeight(temp); 254 | } 255 | 256 | //function to display the tree. 257 | private static void display(){ 258 | System.out.print("Tree's preOrder traversal : "); 259 | preOrder(root); 260 | System.out.println(); 261 | } 262 | 263 | //function to search for a data value. 264 | private static Node find(int data){ 265 | System.out.println("Finding node : " + data); 266 | Node temp = root; 267 | while(temp!=null){ 268 | if(temp.data == data){ 269 | return temp; 270 | } 271 | 272 | if(temp.data>data){ 273 | temp=temp.left; 274 | } 275 | else{ 276 | temp = temp.right; 277 | } 278 | } 279 | return null; 280 | } 281 | 282 | //function to remove elements from the tree. 283 | private static void remove(int data){ 284 | Node node = find(data); 285 | if(node==null){ 286 | System.out.println("Data not found!"); 287 | return; 288 | } 289 | System.out.println("Found! Deleting."); 290 | Node right = node.right; 291 | if(right!=null){ 292 | Node left = right; 293 | while(left.left!=null){ 294 | left = left.left; 295 | } 296 | node.data = left.data; 297 | if(left.parent.left == left){ 298 | left.parent.left = left.right; 299 | if(left.right!=null){ 300 | left.right.parent = left.parent; 301 | } 302 | Node prob = updateHeight(left.parent); 303 | if(prob!=null){ 304 | rotations(prob); 305 | } 306 | } 307 | else{ 308 | left.parent.right = left.right; 309 | if(left.right!=null){ 310 | left.right.parent = left.parent; 311 | } 312 | Node prob = updateHeight(left.parent); 313 | if(prob!=null){ 314 | rotations(prob); 315 | } 316 | } 317 | left = null; 318 | } 319 | else{ 320 | Node parent = node.parent; 321 | if(parent==null){ 322 | root = node.left; 323 | node.left.parent = null; 324 | rotations(root); 325 | } 326 | else{ 327 | if(parent.left == node){ 328 | parent.left = node.left; 329 | if(node.left!=null){ 330 | node.left.parent = parent; 331 | } 332 | Node prob = updateHeight(parent); 333 | if(prob!=null){ 334 | rotations(prob); 335 | } 336 | node = null; 337 | } 338 | else{ 339 | parent.right = node.left; 340 | if(node.left !=null){ 341 | node.left.parent = parent; 342 | } 343 | Node prob = updateHeight(parent); 344 | if(prob!=null){ 345 | rotations(prob); 346 | } 347 | node = null; 348 | } 349 | } 350 | } 351 | } 352 | 353 | //main function to run the program. 354 | public static void main(String [] args){ 355 | add(10); 356 | display(); 357 | 358 | add(1); 359 | display(); 360 | 361 | add(12); 362 | display(); 363 | 364 | add(65); 365 | display(); 366 | 367 | add(22); 368 | display(); 369 | 370 | add(5); 371 | display(); 372 | 373 | add(45); 374 | display(); 375 | 376 | add(34); 377 | display(); 378 | 379 | add(95); 380 | display(); 381 | 382 | add(8); 383 | display(); 384 | 385 | add(59); 386 | display(); 387 | 388 | add(90); 389 | display(); 390 | 391 | add(100); 392 | display(); 393 | 394 | add(13); 395 | display(); 396 | 397 | add(11); 398 | display(); 399 | 400 | remove(90); 401 | display(); 402 | 403 | remove(59); 404 | display(); 405 | 406 | } 407 | } 408 | 409 | -------------------------------------------------------------------------------- /AVLTree/AvlTree Insert/AvlTree.java: -------------------------------------------------------------------------------- 1 | //Program to implement Insertion Algorithm in an AVL Tree. 2 | 3 | public class AvlTree{ 4 | static class Node{ 5 | int data; //data value 6 | Node left; //points to left child. 7 | Node right; //points to right child. 8 | Node parent;//points to parent node. 9 | int height; //height of the node. 10 | 11 | public Node(int data){ 12 | this.data = data; 13 | this.left = null; 14 | this.right = null; 15 | this.parent = null; 16 | this.height = 1; 17 | } 18 | } 19 | 20 | static Node root = null; //root of the tree. 21 | 22 | //function to add elements in the tree. 23 | public static void add(int data){ 24 | System.out.println("Inserting node with data : " + data ); 25 | Node node = new Node(data); 26 | if(root == null){ 27 | root = node; 28 | return; 29 | } 30 | 31 | Node temp = root; 32 | while(temp!=null){ 33 | if(temp.datadata){ 47 | if(temp.left==null){ 48 | temp.left=node; 49 | node.parent = temp; 50 | Node prob = updateHeight(node); //updation of height as new node is inserted 51 | if(prob!=null){ 52 | rotations(prob); //balancing of the tree as tree can become unbalanced on insertion. 53 | } 54 | return; 55 | } 56 | temp = temp.left; 57 | continue; 58 | } 59 | } 60 | } 61 | 62 | //function for PreOrder Traversal of the tree. 63 | private static void preOrder(Node node){ 64 | if(node==null){ 65 | return; 66 | } 67 | System.out.print(node.data + " "); 68 | preOrder(node.left); 69 | preOrder(node.right); 70 | } 71 | 72 | //function for performing the rotations in the tree to make it balanced. 73 | private static void rotations(Node node){ 74 | System.out.println("Problematic Node : " + node.data); 75 | int leftC = 0; //left child height 76 | int rightC = 0; // right child height 77 | 78 | if(node.left != null){ 79 | leftC = node.left.height; 80 | } 81 | if(node.right != null){ 82 | rightC = node.right.height; 83 | } 84 | 85 | int leftgC = 0; //left grand child height 86 | int rightgC = 0; //right grand child height 87 | 88 | if(leftC>rightC){ 89 | Node left = node.left; 90 | if(left.left!=null){ 91 | leftgC = left.left.height; 92 | } 93 | if(left.right!=null){ 94 | rightgC = left.right.height; 95 | } 96 | 97 | //right rotation. 98 | if(leftgC>rightgC){ 99 | rotateRight(left); 100 | node = updateHeight(left); 101 | if(node!=null){ 102 | rotations(node); 103 | } 104 | } 105 | 106 | //left rotation then right rotation. 107 | else{ 108 | Node leftR = left.right; 109 | rotateLeft(leftR); 110 | rotateRight(leftR); 111 | node = updateHeight(leftR); 112 | if(node!=null){ 113 | rotations(node); 114 | } 115 | } 116 | } 117 | else{ 118 | Node right = node.right; 119 | if(right.left!=null){ 120 | leftgC = right.left.height; 121 | } 122 | if(right.right!=null){ 123 | rightgC = right.right.height; 124 | } 125 | 126 | //left rotation. 127 | if(leftgC1){ 178 | return node; 179 | } 180 | node = node.parent; 181 | } 182 | return null; 183 | } 184 | 185 | //function to find balance factor i.e bf = (left.child.height - right.chile.height) 186 | private static int calcBalance(Node temp){ 187 | int leftheight = 0; 188 | if(temp.left!=null){ 189 | leftheight = temp.left.height; 190 | } 191 | int rightheight = 0; 192 | if(temp.right!=null){ 193 | rightheight = temp.right.height; 194 | } 195 | return Math.abs(leftheight-rightheight); 196 | } 197 | 198 | //function for right rotation. 199 | private static void rotateRight(Node temp){ 200 | System.out.println("Rotating Right node : " + temp.data); 201 | Node right = temp.right; 202 | Node gparent = temp.parent.parent; 203 | temp.right = temp.parent; 204 | temp.parent.left = right; 205 | if(right!=null){ 206 | right.parent = temp.parent; 207 | } 208 | temp.parent.parent = temp; 209 | temp.parent = gparent; 210 | if(gparent==null){ 211 | root = temp; 212 | } 213 | 214 | else{ 215 | if(gparent.right==temp.right){ 216 | gparent.right = temp; 217 | } 218 | 219 | else{ 220 | gparent.left = temp; 221 | } 222 | } 223 | calcHeight(temp); 224 | } 225 | 226 | //function for left rotation. 227 | private static void rotateLeft(Node temp){ 228 | System.out.println("Rotating Left node : " + temp.data); 229 | Node gparent = temp.parent.parent; 230 | Node left = temp.left; 231 | temp.left = temp.parent; 232 | temp.parent.right = left; 233 | if(left!=null){ 234 | left.parent = temp.parent; 235 | } 236 | temp.parent.parent = temp; 237 | temp.parent = gparent; 238 | if(gparent==null){ 239 | root = temp; 240 | } 241 | else{ 242 | if(gparent.left==temp.left){ 243 | gparent.left = temp; 244 | } 245 | 246 | else{ 247 | gparent.right = temp; 248 | } 249 | } 250 | calcHeight(temp); 251 | } 252 | 253 | //function to display the tree. 254 | private static void display(){ 255 | System.out.print("Tree's preOrder traversal : "); 256 | preOrder(root); 257 | System.out.println(); 258 | } 259 | 260 | //main function to run the program. 261 | public static void main(String [] args){ 262 | add(10); 263 | display(); 264 | 265 | add(1); 266 | display(); 267 | 268 | add(12); 269 | display(); 270 | 271 | add(65); 272 | display(); 273 | 274 | add(22); 275 | display(); 276 | 277 | add(5); 278 | display(); 279 | 280 | add(45); 281 | display(); 282 | 283 | add(34); 284 | display(); 285 | 286 | add(95); 287 | display(); 288 | 289 | add(8); 290 | display(); 291 | 292 | add(59); 293 | display(); 294 | 295 | add(90); 296 | display(); 297 | 298 | add(100); 299 | display(); 300 | 301 | add(13); 302 | display(); 303 | 304 | add(11); 305 | display(); 306 | } 307 | } 308 | 309 | -------------------------------------------------------------------------------- /Array/ArrayDeletion/ArrayDeletion.java: -------------------------------------------------------------------------------- 1 | //Program to delete an element at a given index. 2 | // array = {1,2,3,4,5}, index = 3; 3 | // after deletion of 3rd element : array = {1,2,3,5,-1} 4 | 5 | import java.util.*; 6 | public class ArrayDeletion{ 7 | //this function takes two arguments - array to delete in and index of the element to delete. 8 | private static int [] delete(int [] array, int indextodeleteAt){ 9 | //check if index is valid. 10 | if(indextodeleteAt<0 || indextodeleteAt>array.length-1){ 11 | return array; 12 | } 13 | 14 | //shift elements towards left from last element till indextodeleteAt. 15 | for(int i=indextodeleteAt; i=array.length){ 10 | System.out.println("Array Full!"); 11 | return array; 12 | } 13 | 14 | //shift elements towards right from indextoinsertat. 15 | for(int i=size+1; i>indextoinsertat; i--){ 16 | array[i] = array[i-1]; 17 | } 18 | 19 | //insert the given element. 20 | array[indextoinsertat]=numbertoinsert; 21 | 22 | // increase the size of the array. 23 | size++; 24 | 25 | //display array 26 | System.out.println("After Inserting " + numbertoinsert + " at index " + indextoinsertat + ": "); 27 | display(array); 28 | 29 | return array; 30 | } 31 | 32 | //function to display the array. 33 | private static void display(int [] array){ 34 | for(int i=0;i=position;i--){ 27 | array[i+1]=array[i]; 28 | } 29 | array[position]=number; 30 | size++; 31 | } 32 | 33 | //function to delete elements from the array. 34 | static public void delete(int position){ 35 | for(int i=position;iarray[j+1]){ 64 | int temp=array[j]; 65 | array[j]=array[j+1]; 66 | array[j+1]=temp; 67 | } 68 | } 69 | } 70 | } 71 | 72 | 73 | //function to to Binary Search on the array. 74 | static public boolean binarySearch(int number,int low, int high){ 75 | if(low>high){ 76 | return false; 77 | } 78 | int mid=low + (high-low)/2; 79 | if(array[mid]==number){ 80 | return true; 81 | } 82 | if(array[mid]>number){ 83 | return binarySearch(number,low,mid-1); 84 | } 85 | else{ 86 | return binarySearch(number,mid+1,high); 87 | } 88 | 89 | } 90 | 91 | //main function 92 | public static void main(String [] args){ 93 | Array array = new Array(); //create an array. 94 | array.getValues(); //get values from the user. 95 | array.display(); //display the array. 96 | 97 | array.insert(10,4);//insert num=10 at pos=4 98 | array.display(); //display the array. 99 | 100 | array.delete(3); //delete num at pos=3 from the array. 101 | array.display(); //display the array. 102 | 103 | System.out.println(array.linearSearch(40)); //check 40 is present in the array using linearSearch. 104 | 105 | array.bubbleSort();//sort the array. 106 | array.display(); //display the array. 107 | 108 | System.out.println(array.binarySearch(56,0,size)); //search for num=56 using Binary Search. 109 | } 110 | } 111 | 112 | 113 | 114 | 115 | 116 | -------------------------------------------------------------------------------- /ArrayList/ArrayListImplementation/ArrayList.java: -------------------------------------------------------------------------------- 1 | //Program to implement ArrayList i.e Dynamic Array. 2 | public class ArrayList{ 3 | int [] array; //array 4 | int numofElements; //number of elements in the array. 5 | int addPointer; //pointer points to the position where new number will be added. 6 | 7 | public ArrayList(){ 8 | array = new int[4]; //initial size of the array = 4 9 | numofElements=0; 10 | addPointer=0; 11 | } 12 | 13 | //function to access elements at a given position. 14 | public int getElement(int index){ 15 | return array[index]; 16 | } 17 | 18 | //funciton to change the value of the element at the given position 19 | public void setValue(int value, int index){ 20 | array[index]=value; 21 | } 22 | 23 | //function to add element in the array. 24 | public void addElement(int number){ 25 | //If array is full, then create new array of twice the size and copy elements from previous array to the new array. 26 | if(addPointer>array.length-1){ 27 | int [] newArray = new int[2*array.length]; 28 | for(int i=0;i-1;i--){ 28 | if(data<=node.data[i]){ 29 | node.data[i+1] = node.data[i]; 30 | index = i; 31 | } 32 | else{ 33 | break; 34 | } 35 | } 36 | node.data[index] = data; 37 | node.size++; 38 | } 39 | 40 | 41 | //function to split the node if it is completely filled. i.e node.data.size == 2*t-1 42 | private static Node splitNode(Node node, int key){ 43 | System.out.println("Splitting Node..."); 44 | int mid = node.size/2; 45 | Node node1 = new Node(t); 46 | Node node2 = new Node(t); 47 | Node parent = node.parent; 48 | 49 | for(int i=0;itemp){ 79 | return node2; 80 | } 81 | return node1; 82 | } 83 | 84 | int index = parent.size; 85 | int data = node.data[mid]; 86 | for(int i=parent.size-1;i>-1;i--){ 87 | if(datadata){ 104 | return node2; 105 | } 106 | return node1; 107 | } 108 | 109 | 110 | //function to add new data values in the Tree. 111 | public static void add(int data){ 112 | System.out.println("\nInserting Data : " + data); 113 | if(root==null){ 114 | Node node = new Node(t); 115 | insertData(node,data); 116 | root = node; 117 | return; 118 | } 119 | 120 | Node temp = root; 121 | while(!temp.leaf || (temp.size==2*t-1 && temp.children[0]==null)){ 122 | if(temp.size==2*t-1){ 123 | temp = splitNode(temp,data); 124 | continue; 125 | } 126 | if(datatemp.data[temp.size-1]){ 131 | temp = temp.children[temp.size]; 132 | continue; 133 | } 134 | for(int i=1;inumber){ 45 | if(temp.leftNode==null){ 46 | temp.leftNode=node; 47 | node.parentNode=temp; 48 | break; 49 | } 50 | else{ 51 | temp=temp.leftNode; 52 | continue; 53 | } 54 | } 55 | } 56 | } 57 | numofElements++; 58 | } 59 | 60 | 61 | //function to display the tree. Pattern can be = InOrder, PreOrder, PostOrder Traversal, Level-Order Traversal. 62 | public void display(String pattern) throws Exception { 63 | System.out.print("Tree : "); 64 | if(rootNode==null){ 65 | throw new Exception("Empty tree!"); 66 | } 67 | //InOrder Traversal 68 | if(pattern=="inOrder"){ 69 | inOrderDisplay(rootNode); 70 | System.out.println(); 71 | } 72 | else{ 73 | //PreOrder Traversal. 74 | if(pattern=="preOrder"){ 75 | preOrderDisplay(rootNode); 76 | System.out.println(); 77 | } 78 | else{ 79 | //PostOrder Traversal. 80 | if(pattern=="postOrder"){ 81 | postOrderDisplay(rootNode); 82 | System.out.println(); 83 | } 84 | //Level-Order Traversal. 85 | else{ 86 | levelOrderDisplay(rootNode); 87 | System.out.println(); 88 | } 89 | } 90 | } 91 | } 92 | 93 | //function for InOrder Traversal of Tree. 94 | private BinarySearchTree inOrderDisplay(BinarySearchTree root){ 95 | if(root==null){ 96 | return root; 97 | } 98 | inOrderDisplay(root.leftNode); 99 | System.out.print(root.data + " "); 100 | inOrderDisplay(root.rightNode); 101 | return root; 102 | } 103 | 104 | //function for PreOrder Traversal of the Tree. 105 | private BinarySearchTree preOrderDisplay(BinarySearchTree root){ 106 | if(root==null){ 107 | return root; 108 | } 109 | System.out.print(root.data + " "); 110 | inOrderDisplay(root.leftNode); 111 | inOrderDisplay(root.rightNode); 112 | return root; 113 | } 114 | 115 | //function for PostOrder Traversal of the Tree. 116 | private BinarySearchTree postOrderDisplay(BinarySearchTree root){ 117 | if(root==null){ 118 | return root; 119 | } 120 | inOrderDisplay(root.leftNode); 121 | inOrderDisplay(root.rightNode); 122 | System.out.print(root.data + " "); 123 | return root; 124 | } 125 | 126 | //function for Level-Order Traversal of the Tree. 127 | private BinarySearchTree levelOrderDisplay(BinarySearchTree root){ 128 | if(root==null){ 129 | return root; 130 | } 131 | Queue queue = new LinkedList(); 132 | queue.add(root); 133 | while(queue.isEmpty()!=true){ 134 | BinarySearchTree temp = queue.remove(); 135 | System.out.print(temp.data + " "); 136 | if(temp.leftNode!=null){ 137 | queue.add(temp.leftNode); 138 | } 139 | if(temp.rightNode!=null){ 140 | queue.add(temp.rightNode); 141 | } 142 | } 143 | return root; 144 | } 145 | 146 | //function to find specific data in the Tree. 147 | public boolean findNode(int number)throws Exception{ 148 | if(rootNode==null){ 149 | throw new Exception("Empty tree!"); 150 | } 151 | BinarySearchTree temp = rootNode; 152 | boolean result=false; 153 | while(temp!=null){ 154 | if(temp.data==number){ 155 | result=true; 156 | break; 157 | } 158 | if(temp.data>number){ 159 | temp=temp.leftNode; 160 | continue; 161 | } 162 | if(temp.datatemp.key){ 26 | prev=temp; 27 | temp=temp.right; 28 | continue; 29 | } 30 | if(dataparent.key){ 53 | parent.right=node; 54 | } 55 | else{ 56 | parent.left=node; 57 | } 58 | } 59 | 60 | //finds just the next big node than the given node.Works correctlly only when the given node is actually in the tree. 61 | //if the given key is largest then it returns the same node; 62 | public Node findNext(int data){ 63 | Node temp = find(data); 64 | if(temp.key!=data){ 65 | System.out.println(data + " not present in the tree."); 66 | return null; 67 | } 68 | Node right = temp.right; 69 | Node parent = temp.parent; 70 | 71 | if(right!=null){ 72 | while(right.left!=null){ 73 | right=right.left; 74 | } 75 | return right; 76 | } 77 | 78 | while(parent!=null){ 79 | if(parent.key>temp.key){ 80 | return parent; 81 | } 82 | parent=parent.parent; 83 | } 84 | return temp; 85 | } 86 | 87 | 88 | //return the numbers between data1 and data2, including data1 and data2 if they are present in the tree. 89 | public List rangeSearch(int data1, int data2){ 90 | System.out.println("Range Search between " + data1 + " and " + data2); 91 | List list = new ArrayList(); 92 | Node node1 = find(data1); 93 | if(node1==null){ 94 | return list; 95 | } 96 | 97 | while(node1.key<=data2){ 98 | if(node1.key>=data1){ 99 | list.add(node1); 100 | } 101 | Node temp = findNext(node1.key); 102 | if(temp==node1){ 103 | break; 104 | } 105 | node1=temp; 106 | } 107 | return list; 108 | } 109 | 110 | //function to remove elements from the tree. 111 | public void delete(int data){ 112 | System.out.println("Remove : " + data); 113 | Node temp = find(data); 114 | if(temp.key!=data){ 115 | System.out.println(data + " is not present in the tree."); 116 | return; 117 | } 118 | if(temp.right==null){ 119 | if(temp.parent==null){ 120 | root=temp.left; 121 | if(root!=null){ 122 | root.parent=null; 123 | } 124 | temp=null; 125 | return; 126 | } 127 | if(temp.left!=null){ 128 | temp.left.parent=temp.parent; 129 | } 130 | if(temp.parent.right==temp){ 131 | temp.parent.right=temp.left; 132 | temp=null; 133 | return; 134 | } 135 | else{ 136 | temp.parent.left=temp.left; 137 | temp=null; 138 | return; 139 | } 140 | } 141 | 142 | Node nextTemp = findNext(temp.key); 143 | if(nextTemp.right!=null){ 144 | nextTemp.right.parent=nextTemp.parent; 145 | } 146 | if(nextTemp.parent==null){ 147 | root=nextTemp.right; 148 | } 149 | if(nextTemp.parent!=null && nextTemp.parent.left==nextTemp){ 150 | nextTemp.parent.left=nextTemp.right; 151 | } 152 | if(nextTemp.parent!=null && nextTemp.parent.right==nextTemp){ 153 | nextTemp.parent.right=nextTemp.right; 154 | } 155 | 156 | 157 | if(temp.left!=null){ 158 | temp.left.parent=nextTemp; 159 | } 160 | if(temp.right!=null){ 161 | temp.right.parent=nextTemp; 162 | } 163 | nextTemp.left=temp.left; 164 | nextTemp.right=temp.right; 165 | nextTemp.parent=temp.parent; 166 | 167 | 168 | if(temp.parent==null){ 169 | root=nextTemp; 170 | temp=null; 171 | return; 172 | } 173 | if(temp.parent.right==temp){ 174 | temp.parent.right=nextTemp; 175 | temp=null; 176 | return; 177 | } 178 | temp.parent.left=nextTemp; 179 | temp=null; 180 | } 181 | 182 | } 183 | 184 | 185 | 186 | -------------------------------------------------------------------------------- /BinarySearchTree/BinarySearchTree 2/BinarySearchTreeImplementation/BinarySearchTreeImplementation.java: -------------------------------------------------------------------------------- 1 | //Program to execute the BinarySearchTree. 2 | import java.util.*; 3 | public class BinarySearchTreeImplementation{ 4 | public static void main(String [] args){ 5 | BinarySearchTree tree = new BinarySearchTree(); //create a BST. 6 | tree.insert(10); 7 | tree.insert(20); 8 | tree.insert(8); 9 | tree.insert(22); 10 | tree.insert(9); 11 | 12 | System.out.println("Find 8 : " + tree.find(8).key); 13 | System.out.println("Find 10 : " + tree.find(10).key); 14 | System.out.println("Find 20 : " + tree.find(20).key); 15 | System.out.println("Find 22 : " + tree.find(22).key); 16 | System.out.println("Find 9 : " + tree.find(9).key); 17 | System.out.println(); 18 | 19 | System.out.println("FindNext 8 : " + tree.findNext(8).key); 20 | System.out.println("FindNext 10 : " + tree.findNext(10).key); 21 | System.out.println("FindNext 20 : " + tree.findNext(20).key); 22 | System.out.println("FindNext 22 : " + tree.findNext(22).key); 23 | System.out.println("FindNext 9 : " + tree.findNext(9).key); 24 | System.out.println(); 25 | 26 | 27 | 28 | List list = tree.rangeSearch(7,23); 29 | 30 | for(int i=0;i list2 = tree.rangeSearch(7,23); 46 | 47 | for(int i=0;idata){ 37 | temp=temp.left; 38 | } 39 | else{ 40 | temp=temp.right; 41 | } 42 | } 43 | return prev; 44 | } 45 | 46 | //function to add elements in the tree. 47 | public static void add(int data){ 48 | System.out.println("Inserting data : " + data); 49 | Node node = new Node(data); 50 | Node positionNode = find(data); 51 | if(positionNode==null){ 52 | root=node; 53 | return; 54 | } 55 | 56 | node.parent=positionNode; 57 | 58 | if(node.key>positionNode.key){ 59 | positionNode.right=node; 60 | } 61 | else{ 62 | positionNode.left=node; 63 | } 64 | } 65 | 66 | //function to find the next greater element from the given data value. 67 | public static Node next(int data){ 68 | System.out.println("Find Next of : " + data); 69 | if(root==null){ 70 | return null; 71 | } 72 | 73 | Node node = find(data); 74 | if(node.right!=null){ 75 | return leftDecesdant(node.right); 76 | } 77 | else{ 78 | return rightAncestor(node); 79 | } 80 | } 81 | 82 | //function to find leftmost node. 83 | private static Node leftDecesdant(Node node){ 84 | Node temp=node; 85 | while(temp.left!=null){ 86 | temp=temp.left; 87 | } 88 | return temp; 89 | } 90 | 91 | //function to find the parent node with data value greater than the given node data value. 92 | private static Node rightAncestor(Node node){ 93 | Node temp=node.parent; 94 | while(temp!=null){ 95 | if(temp.key>node.key){ 96 | break; 97 | } 98 | temp=temp.parent; 99 | } 100 | if(temp==null){ 101 | System.out.println("Max Element"); 102 | return node; 103 | } 104 | else{ 105 | return temp; 106 | } 107 | } 108 | 109 | //function to remove elements from the tree. 110 | private static Node remove(int data){ 111 | System.out.println("Remove : " + data); 112 | Node node = find(data); 113 | if(node.right==null){ 114 | if(root==node){ 115 | root=node.left; 116 | return node; 117 | } 118 | if(node.parent.left==node){ 119 | node.parent.left=node.left; 120 | } 121 | else{ 122 | node.parent.right=node.left; 123 | } 124 | return node; 125 | } 126 | 127 | else{ 128 | Node temp = next(data); 129 | node.key=temp.key; 130 | if(temp.parent.left==temp){ 131 | temp.parent.left=temp.right; 132 | } 133 | if(temp.parent.right==temp){ 134 | temp.parent.right=temp.right; 135 | } 136 | } 137 | return node; 138 | } 139 | 140 | //function to find the max data value in the tree. 141 | public static Node maxNode(){ 142 | if(root==null){ 143 | return null; 144 | } 145 | Node temp=root; 146 | while(temp.right!=null){ 147 | temp=temp.right; 148 | } 149 | return temp; 150 | } 151 | 152 | //function to find the min data value in the tree. 153 | public static Node minNode(){ 154 | if(root==null){ 155 | return null; 156 | } 157 | Node temp = root; 158 | while(temp.left!=null){ 159 | temp=temp.left; 160 | } 161 | return temp; 162 | } 163 | 164 | //function for InOrder Traversal of the tree. 165 | private static void inorderTraversal(Node rootNode){ 166 | if(rootNode==null){ 167 | return; 168 | } 169 | inorderTraversal(rootNode.left); 170 | System.out.print(rootNode.key + " "); 171 | inorderTraversal(rootNode.right); 172 | } 173 | 174 | //function for PreOrder Traversal of the tree. 175 | private static void preorderTraversal(Node rootNode){ 176 | if(rootNode==null){ 177 | return; 178 | } 179 | System.out.print(rootNode.key + " " ); 180 | preorderTraversal(rootNode.left); 181 | preorderTraversal(rootNode.right); 182 | } 183 | 184 | //function for PostOrder Traversal of the Tree. 185 | private static void postorderTraversal(Node rootNode){ 186 | if(rootNode==null){ 187 | return; 188 | } 189 | postorderTraversal(rootNode.left); 190 | postorderTraversal(rootNode.right); 191 | System.out.print(rootNode.key + " "); 192 | } 193 | 194 | //function to display the tree. 195 | public static void display(){ 196 | System.out.println(); 197 | System.out.println("Inorder Traversal"); 198 | inorderTraversal(root); 199 | 200 | System.out.println(); 201 | System.out.println("PreOrder Traversal"); 202 | preorderTraversal(root); 203 | 204 | System.out.println(); 205 | System.out.println("PostOrder Traversal"); 206 | postorderTraversal(root); 207 | 208 | System.out.println(); 209 | } 210 | 211 | //main function to run the program. 212 | public static void main(String [] args){ 213 | BinarySearchTree tree = new BinarySearchTree(); 214 | tree.add(10); 215 | tree.add(6); 216 | tree.add(14); 217 | tree.add(8); 218 | tree.add(12); 219 | tree.add(4); 220 | tree.add(16); 221 | 222 | tree.display(); 223 | 224 | System.out.println(); 225 | Node maxNode = tree.maxNode(); 226 | System.out.println("Max value : " + maxNode.key); 227 | Node minNode = tree.minNode(); 228 | System.out.println("Min value : " + minNode.key); 229 | 230 | System.out.println(); 231 | Node node1 = tree.find(8); 232 | System.out.println("Find 8 : " + node1.key); 233 | Node node2 = tree.find(16); 234 | System.out.println("Find 16 : " + node2.key); 235 | Node node3 = tree.find(10); 236 | System.out.println("Find 10 : " + node3.key); 237 | Node node4 = tree.find(4); 238 | System.out.println("Find 4 : " + node4.key); 239 | 240 | System.out.println(); 241 | tree.display(); 242 | System.out.println(); 243 | 244 | Node next1 = tree.next(16); 245 | System.out.println(next1.key); 246 | 247 | Node next2 = tree.next(10); 248 | System.out.println(next2.key); 249 | 250 | Node next3 = tree.next(4); 251 | System.out.println( next3.key); 252 | 253 | Node next4 = tree.next(8); 254 | System.out.println(next4.key); 255 | 256 | Node next5 = tree.next(14); 257 | System.out.println(next5.key); 258 | 259 | Node next6 = tree.next(12); 260 | System.out.println(next6.key); 261 | 262 | Node next7 = tree.next(6); 263 | System.out.println(next7.key); 264 | 265 | System.out.println(); 266 | 267 | Node del1 = tree.remove(14); 268 | System.out.println(del1.key); 269 | 270 | Node del2 = tree.remove(10); 271 | System.out.println(del2.key); 272 | 273 | Node del3 = tree.remove(4); 274 | System.out.println(del3.key); 275 | 276 | Node del4 = tree.remove(6); 277 | System.out.println(del4.key); 278 | 279 | Node del5 = tree.remove(16); 280 | System.out.println(del5.key); 281 | 282 | System.out.println(); 283 | tree.display(); 284 | 285 | System.out.println(); 286 | Node max = tree.maxNode(); 287 | System.out.println("Max value : " + max.key); 288 | Node min = tree.minNode(); 289 | System.out.println("Min value : " + min.key); 290 | 291 | } 292 | } 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | -------------------------------------------------------------------------------- /DisjointSet/DisjointSetImplementation/DisjointSet.java: -------------------------------------------------------------------------------- 1 | //Program to Impelement DisjointSet Data Structure. 2 | 3 | public class DisjointSet{ 4 | int [] parent; //array to store the elements ids. 5 | int [] rank; //rank is the height of the tree. Because this set works on tree property. Elements in same set have common id which is the root of tree, tree is made from these elements. 6 | int numofElements; //size of the set. 7 | 8 | public DisjointSet(int length){ 9 | numofElements=length; 10 | parent= new int[numofElements]; 11 | rank = new int[numofElements]; 12 | } 13 | 14 | //function to make the Set. Id identifies one set from another. 15 | public void makeSet(int id)throws Exception{ 16 | System.out.println("Inserting Element with id : " + id); 17 | if(id>=numofElements){ 18 | throw new Exception("Size exceeded!"); 19 | } 20 | parent[id]=id; 21 | rank[id]=0; 22 | } 23 | 24 | //function to find root of set in which the element with the given id belong. 25 | public int find(int id)throws Exception{ 26 | if(id>=numofElements){ 27 | throw new Exception("Invalid Id!"); 28 | } 29 | if(id==parent[id]){ 30 | return parent[id]; 31 | } 32 | return find(parent[id]); 33 | } 34 | 35 | //function to take union of two sets. 36 | public void union(int id1, int id2)throws Exception{ 37 | System.out.println("Union Operation of " + id1 + " and " + id2); 38 | if(id1>=numofElements || id2>=numofElements){ 39 | throw new Exception("Invalid Id!"); 40 | } 41 | int parentid1 = find(id1); //get the root of set with id1. 42 | int parentid2 = find(id2); //get the root of set with id2. 43 | 44 | //if already in the same set 45 | if(parentid1==parentid2){ 46 | return; 47 | } 48 | 49 | //find the tree with samller rank and change the root id of smaller set to larger set's root id. 50 | if(rank[parentid1]>rank[parentid2]){ 51 | parent[parentid2]=parentid1; 52 | } 53 | else{ 54 | parent[parentid1]=parentid2; 55 | if(rank[parentid1]==rank[parentid2]){ 56 | rank[parentid2]=rank[parentid2]+1; 57 | } 58 | } 59 | } 60 | } 61 | 62 | 63 | -------------------------------------------------------------------------------- /DisjointSet/DisjointSetImplementation/DisjointSetImplementation.java: -------------------------------------------------------------------------------- 1 | //Program to execute the Disjoint Set. 2 | public class DisjointSetImplementation{ 3 | public static void main(String [] args){ 4 | DisjointSet set = new DisjointSet(5); //create Disjoint Set of size = 5. 5 | try{ 6 | set.makeSet(0); 7 | set.makeSet(1); 8 | set.makeSet(2); 9 | set.makeSet(3); 10 | set.makeSet(4); 11 | 12 | System.out.println("Find 0 : " + set.find(0)); 13 | System.out.println("Find 1 : " + set.find(1)); 14 | System.out.println("Find 2 : " + set.find(2)); 15 | System.out.println("Find 3 : " + set.find(3)); 16 | System.out.println("Find 4 : " + set.find(4)); 17 | 18 | set.union(4,3); 19 | System.out.println("Find 4 : " + set.find(4)); 20 | System.out.println("Find 3 : " + set.find(3)); 21 | 22 | set.union(0,1); 23 | System.out.println("Find 1 : " + set.find(1)); 24 | System.out.println("Find 0 : " + set.find(0)); 25 | 26 | set.union(1,2); 27 | System.out.println("Find 2 : " + set.find(2)); 28 | System.out.println("Find 1 : " + set.find(1)); 29 | 30 | set.union(2,3); 31 | System.out.println("Find 3 : " + set.find(3)); 32 | System.out.println("Find 2 : " + set.find(2)); 33 | 34 | System.out.println("\nFind 0 : " + set.find(0)); 35 | System.out.println("Find 1 : " + set.find(1)); 36 | System.out.println("Find 2 : " + set.find(2)); 37 | System.out.println("Find 3 : " + set.find(3)); 38 | System.out.println("Find 4 : " + set.find(4)); 39 | System.out.println("Find 5 : " + set.find(5)); 40 | } 41 | catch(Exception e){ 42 | System.out.println(e); 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /DoublyLinkedList/DoublyLinkedList/DoublyLinkedList.java: -------------------------------------------------------------------------------- 1 | //Program to Implement DoubleLinkedList. 2 | 3 | public class DoublyLinkedList{ 4 | static class Node{ 5 | int key; //data value 6 | Node next; //points to next element in the list. 7 | Node prev; //points to previous element in the list. 8 | 9 | public Node(int data){ 10 | this.key=data; 11 | this.next=null; 12 | this.prev=null; 13 | } 14 | } 15 | 16 | static Node head; //head of the list i.e first element 17 | static Node tail; //tail of the list i.e last element 18 | static int size; //number of elements in the list. 19 | 20 | public DoublyLinkedList(){ 21 | head=null; 22 | tail=null; 23 | size=0; 24 | } 25 | 26 | //function to add elements at the front of the list. 27 | public static void pushFront(int number){ 28 | System.out.println("Inserting data at front : " + number); 29 | Node node = new Node(number); 30 | if(head==null){ 31 | head=node; 32 | tail=node; 33 | size++; 34 | return; 35 | } 36 | 37 | node.next=head; 38 | head.prev=node; 39 | head=node; 40 | size++; 41 | } 42 | 43 | //function to add elements at the back of the list. 44 | public static void pushBack(int number){ 45 | System.out.println("Inserting data at back : " + number); 46 | Node node =new Node(number); 47 | if(head==null){ 48 | head=node; 49 | tail=node; 50 | size++; 51 | return; 52 | } 53 | 54 | Node temp=head; 55 | while(temp.next!=null){ 56 | temp=temp.next; 57 | } 58 | temp.next=node; 59 | node.prev=temp; 60 | tail=node; 61 | size++; 62 | } 63 | 64 | //function to remove element from the front of list. 65 | public static void popFront(){ 66 | System.out.println("PopFront Operation."); 67 | if(head==null){ 68 | System.out.println("Empty List."); 69 | return; 70 | } 71 | if(head.next==null){ 72 | head=null; 73 | tail=null; 74 | size--; 75 | return; 76 | } 77 | head=head.next; 78 | head.prev=null; 79 | size--; 80 | } 81 | 82 | //function to remove elements from the back of the list. 83 | public static void popBack(){ 84 | System.out.println("PopBack Operation."); 85 | if(head==null){ 86 | System.out.println("Empty List."); 87 | return; 88 | } 89 | 90 | if(head.next==null){ 91 | head=null; 92 | tail=null; 93 | size--; 94 | return; 95 | } 96 | 97 | Node temp=head; 98 | while(temp.next.next!=null){ 99 | temp=temp.next; 100 | } 101 | temp.next.prev=null; 102 | temp.next=null; 103 | tail=temp; 104 | size--; 105 | } 106 | 107 | //function to display the list. 108 | public static void displayF(){ 109 | System.out.print("List : "); 110 | if(head==null){ 111 | System.out.println("Empty List."); 112 | return; 113 | } 114 | Node temp=head; 115 | while(temp!=null){ 116 | System.out.print(temp.key + " "); 117 | temp=temp.next; 118 | } 119 | System.out.println(); 120 | } 121 | 122 | public static void displayB(){ 123 | System.out.print("Reverse List : "); 124 | if(head==null && tail==null){ 125 | System.out.println("Empty List."); 126 | return; 127 | } 128 | 129 | Node temp=tail; 130 | while(temp!=null){ 131 | System.out.print(temp.key + " "); 132 | temp=temp.prev; 133 | } 134 | System.out.println(); 135 | } 136 | 137 | public static void main(String [] args){ 138 | DoublyLinkedList list = new DoublyLinkedList(); 139 | list.pushFront(10); 140 | list.pushBack(12); 141 | list.pushFront(8); 142 | list.pushBack(14); 143 | list.displayF(); 144 | list.displayB(); 145 | 146 | list.popFront(); 147 | list.displayF(); 148 | list.displayB(); 149 | list.popBack(); 150 | list.displayF(); 151 | list.displayB(); 152 | 153 | list.popFront(); 154 | list.displayB(); 155 | list.popFront(); 156 | list.displayF(); 157 | } 158 | } 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | -------------------------------------------------------------------------------- /DoublyLinkedList/DoublyLinkedListImplementation/DoublyLinkedList.java: -------------------------------------------------------------------------------- 1 | //Program to implement Doubly Linked List. 2 | 3 | public class DoublyLinkedList{ 4 | Node head; //head of the list. i.e first element. 5 | Node tail; //tail of the list. i.e last element. 6 | int size; //number of elements in the list. 7 | 8 | public DoublyLinkedList(){ 9 | head=null; 10 | tail=null; 11 | size=0; 12 | } 13 | 14 | //function to add elements at the front of the list. 15 | public void pushFront(int data){ 16 | System.out.println("Inserting data at front : " + data); 17 | Node node = new Node(data); 18 | if(head==null && tail==null){ 19 | head=node; 20 | tail=node; 21 | size++; 22 | return; 23 | } 24 | 25 | node.next=head; 26 | head.prev=node; 27 | head=node; 28 | size++; 29 | } 30 | 31 | //function to add elements at the back of the list. 32 | public void pushBack(int data){ 33 | System.out.println("Inserting data at back : " + data); 34 | Node node = new Node(data); 35 | if(tail==null && head==null){ 36 | head=node; 37 | tail=node; 38 | size++; 39 | return; 40 | } 41 | 42 | tail.next=node; 43 | node.prev=tail; 44 | tail=node; 45 | size++; 46 | } 47 | 48 | //function to remove elements from the front of the list. 49 | public void popFront() throws Exception{ 50 | System.out.println("PopFront Operation."); 51 | if(head==null && tail==null){ 52 | throw new Exception("List is empty!"); 53 | } 54 | if(head==tail){ 55 | head=null; 56 | tail=null; 57 | size--; 58 | return; 59 | } 60 | 61 | head.next.prev=null; 62 | head=head.next; 63 | size--; 64 | } 65 | 66 | //function to remove elements from the back of the list. 67 | public void popBack() throws Exception{ 68 | System.out.println("PopBack Operation."); 69 | if(head==null && tail==null){ 70 | throw new Exception("List is empty!"); 71 | } 72 | if(head==tail){ 73 | head=null; 74 | tail=null; 75 | size--; 76 | return; 77 | } 78 | tail.prev.next=null; 79 | tail=tail.prev; 80 | size--; 81 | } 82 | 83 | //function to get the first element of the list. 84 | public int topFront() throws Exception{ 85 | if(head==null){ 86 | throw new Exception("List is empty!"); 87 | } 88 | return head.key; 89 | } 90 | 91 | //function to get last element of the list. 92 | public int topBack() throws Exception{ 93 | if(tail==null){ 94 | throw new Exception("List is empty!"); 95 | } 96 | return tail.key; 97 | } 98 | 99 | //function to find specific element in the list. 100 | public boolean Find(int data){ 101 | Node temp=head; 102 | while(temp!=null){ 103 | if(temp.key==data){ 104 | return true; 105 | } 106 | temp=temp.next; 107 | } 108 | return false; 109 | } 110 | 111 | //function to get size of the list. 112 | public int size(){ 113 | return size; 114 | } 115 | 116 | //function to display the list. 117 | public void forwardDisplay() throws Exception{ 118 | System.out.print("List : "); 119 | Node temp = head; 120 | if(temp==null){ 121 | throw new Exception("List is empty!"); 122 | } 123 | while(temp!=null){ 124 | System.out.print(temp.key + " "); 125 | temp=temp.next; 126 | } 127 | System.out.println(); 128 | } 129 | 130 | //function to display the list in reverse direction. 131 | public void reverseDisplay() throws Exception{ 132 | System.out.print("Reverse List : "); 133 | Node temp = tail; 134 | if(temp==null){ 135 | throw new Exception("List is empty!"); 136 | } 137 | while(temp!=null){ 138 | System.out.print(temp.key + " "); 139 | temp=temp.prev; 140 | } 141 | System.out.println(); 142 | } 143 | 144 | //function to remove specific element from the list. 145 | public void remove(int data) throws Exception{ 146 | System.out.println("Remove data : " + data); 147 | Node temp = head; 148 | Node prevNode = null; 149 | if(temp==null){ 150 | throw new Exception("List is empty!"); 151 | } 152 | size--; 153 | 154 | if(temp==tail){ 155 | if(temp.key==data){ 156 | head=null; 157 | tail=null; 158 | return; 159 | } 160 | else{ 161 | throw new Exception("Data not found"); 162 | } 163 | } 164 | if(head.key==data){ 165 | head=head.next; 166 | head.prev=null; 167 | return; 168 | } 169 | while(temp!=null && temp.key!=data){ 170 | prevNode=temp; 171 | temp=temp.next; 172 | } 173 | prevNode.next=prevNode.next.next; 174 | prevNode.next.prev=prevNode; 175 | temp=null; 176 | } 177 | } 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | -------------------------------------------------------------------------------- /DoublyLinkedList/DoublyLinkedListImplementation/DoublyLinkedListImplementation.java: -------------------------------------------------------------------------------- 1 | //Program to Execute the Doubly LinkedList. 2 | public class DoublyLinkedListImplementation{ 3 | public static void main(String [] args){ 4 | DoublyLinkedList list = new DoublyLinkedList(); //create Doubly LinkedList. 5 | try{ 6 | list.pushBack(10); 7 | list.pushBack(20); 8 | list.pushFront(8); 9 | list.pushFront(6); 10 | list.pushBack(22); 11 | list.pushBack(16); 12 | 13 | System.out.println("Front element : " + list.topFront()); 14 | System.out.println("Last element : " + list.topBack()); 15 | System.out.println("List size : " + list.size()); 16 | 17 | list.forwardDisplay(); 18 | 19 | list.remove(6); 20 | 21 | list.reverseDisplay(); 22 | 23 | System.out.println("Find 8 : " + list.Find(8)); 24 | 25 | list.popFront(); 26 | 27 | System.out.println("List size : " + list.size()); 28 | 29 | list.forwardDisplay(); 30 | 31 | list.popBack(); 32 | 33 | System.out.println("Find 20 : " + list.Find(20)); 34 | System.out.println("List size : " + list.size()); 35 | 36 | list.reverseDisplay(); 37 | 38 | list.popFront(); 39 | 40 | System.out.println("List size : " + list.size()); 41 | 42 | list.forwardDisplay(); 43 | } 44 | catch(Exception e){ 45 | System.out.println(e); 46 | } 47 | 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /DoublyLinkedList/DoublyLinkedListImplementation/Node.java: -------------------------------------------------------------------------------- 1 | //Class Node 2 | public class Node{ 3 | int key; //data value. 4 | Node next; //points to next element in the list. 5 | Node prev; //points to prev element in the list. 6 | 7 | public Node(int data){ 8 | this.key=data; 9 | this.next=null; 10 | this.prev=null; 11 | } 12 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Navjinder Virdee 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LinkedList/CircularLinkedList/CircularLinkedList.java: -------------------------------------------------------------------------------- 1 | //Program to implement Circular LinkedList. 2 | 3 | public class CircularLinkedList{ 4 | static class Node{ 5 | int key; //Data value 6 | Node next; //points to next element in the list. 7 | 8 | public Node(int data){ 9 | this.key=data; 10 | this.next=null; 11 | } 12 | 13 | } 14 | 15 | static Node head; //head of the list. 16 | 17 | public CircularLinkedList(){ 18 | head=null; 19 | } 20 | 21 | //function to add element at the front of the list. 22 | static public void pushFront(int number){ 23 | System.out.println("Inserting data at front : " + number); 24 | Node node = new Node(number); 25 | if(head==null){ 26 | head=node; 27 | node.next=head; 28 | return; 29 | } 30 | 31 | //if list contains only single element. 32 | if(head.next==head){ 33 | node.next=head; 34 | head=node; 35 | node.next.next=head; //point the last element to head of the list. 36 | return; 37 | } 38 | 39 | Node temp=head; 40 | while(temp.next!=head){ 41 | temp=temp.next; 42 | } 43 | 44 | node.next=head; 45 | head=node; 46 | temp.next=head; //point the last element to the head of the list. 47 | } 48 | 49 | 50 | //fucntion to add elements at the back of the list. 51 | static public void pushBack(int number){ 52 | System.out.println("Inserting data at back : " + number); 53 | Node node = new Node(number); 54 | 55 | if(head==null){ 56 | head=node; 57 | node.next=head; 58 | return; 59 | } 60 | 61 | //if list contains only single element. 62 | if(head.next==head){ 63 | head.next=node; 64 | node.next=head; 65 | return; 66 | } 67 | Node temp = head; 68 | while(temp.next!=head){ 69 | temp=temp.next; 70 | } 71 | temp.next=node; 72 | node.next=head; 73 | } 74 | 75 | //function to remove elements from the front of the list. 76 | public static void popFront(){ 77 | System.out.print("PopFront Operation : "); 78 | if(head==null){ 79 | System.out.println("Empty List."); 80 | return; 81 | } 82 | 83 | if(head.next==head){ 84 | head=null; 85 | System.out.println("Successfull."); 86 | return; 87 | } 88 | 89 | Node temp=head; 90 | while(temp.next!=head){ 91 | temp=temp.next; 92 | } 93 | head=head.next; 94 | temp.next=head; //update the last element to point to new head. 95 | System.out.println("Successfull."); 96 | } 97 | 98 | //function to remove elements from the back of the list. 99 | public static void popBack(){ 100 | System.out.print("PopBack Operation : "); 101 | if(head==null){ 102 | System.out.println("Empty List."); 103 | return; 104 | } 105 | 106 | if(head.next==head){ 107 | head=null; 108 | System.out.println("Successfull."); 109 | return; 110 | } 111 | 112 | Node temp=head; 113 | while(temp.next.next!=head){ 114 | temp=temp.next; 115 | } 116 | temp.next=temp.next.next; //update new last element to point to head. 117 | System.out.println("Successfull."); 118 | } 119 | 120 | //function to display the list. 121 | public static void display(){ 122 | System.out.print("List : "); 123 | if(head==null){ 124 | System.out.println("Empty List."); 125 | return; 126 | } 127 | 128 | Node temp=head; 129 | while(temp.next!=head){ 130 | System.out.print(temp.key + " "); 131 | temp=temp.next; 132 | } 133 | System.out.println(temp.key); 134 | } 135 | 136 | //main function to run the program. 137 | public static void main(String [] args){ 138 | CircularLinkedList list = new CircularLinkedList(); 139 | list.pushFront(10); 140 | list.pushBack(12); 141 | list.pushFront(8); 142 | list.pushBack(14); 143 | 144 | list.display(); 145 | 146 | list.popFront(); 147 | list.display(); 148 | 149 | list.popBack(); 150 | list.display(); 151 | list.popFront(); 152 | list.popBack(); 153 | list.display(); 154 | } 155 | } 156 | 157 | 158 | 159 | 160 | -------------------------------------------------------------------------------- /LinkedList/LinkedList SwapNodes/LinkedList.java: -------------------------------------------------------------------------------- 1 | //Program to swap data values of first and last node. 2 | //e.g List = 1->2->3->4 , after swap List = 4->2->3->1 3 | 4 | public class LinkedList{ 5 | static class Node{ 6 | int key; //store the data value. 7 | Node next; //points to next element of the list. 8 | 9 | public Node(int data){ 10 | this.key=data; 11 | this.next=null; 12 | } 13 | } 14 | 15 | static Node head; //head node i.e first element. 16 | static int size; //number of elements in the list. 17 | 18 | public LinkedList(){ 19 | head=null; 20 | } 21 | 22 | //function to add elements at the front of the list. 23 | static public void pushFront(int number){ 24 | System.out.println("Inserting data at front : " + number); 25 | Node node = new Node(number); 26 | if(head==null){ 27 | head=node; 28 | size++; 29 | return; 30 | } 31 | 32 | node.next=head; 33 | head=node; 34 | size++; 35 | } 36 | 37 | //function to add elements at the back of the list. 38 | static public void pushBack(int number){ 39 | System.out.println("Inserting data at back : " + number); 40 | Node node = new Node(number); 41 | if(head==null){ 42 | head=node; 43 | size++; 44 | return; 45 | } 46 | Node temp=head; 47 | while(temp.next!=null){ 48 | temp=temp.next; 49 | } 50 | temp.next=node; 51 | size++; 52 | } 53 | 54 | //function to swap the data values of the first and last element. 55 | public static void swap(){ 56 | System.out.println("Swapping data values of first and last node."); 57 | //if either list is empty or contains only single element. 58 | if(head==null || head.next==null){ 59 | return; 60 | } 61 | 62 | Node temp=head; 63 | Node headNode = head; 64 | while(temp.next!=null){ 65 | temp=temp.next; 66 | } 67 | int data = headNode.key; 68 | headNode.key=temp.key; 69 | temp.key=data; 70 | } 71 | 72 | //function to display the list. 73 | static public void display(){ 74 | System.out.print("List : "); 75 | if(head==null){ 76 | System.out.println("Empty."); 77 | return; 78 | } 79 | 80 | Node temp=head; 81 | while(temp!=null){ 82 | System.out.print(temp.key + " "); 83 | temp=temp.next; 84 | } 85 | System.out.println(); 86 | } 87 | 88 | //main function to run the program. 89 | public static void main(String [] args){ 90 | LinkedList list = new LinkedList(); 91 | list.pushFront(10); 92 | list.pushBack(12); 93 | list.pushFront(8); 94 | list.pushFront(6); 95 | 96 | list.display(); 97 | 98 | list.swap(); 99 | list.display(); 100 | } 101 | } 102 | 103 | -------------------------------------------------------------------------------- /LinkedList/LinkedList/LinkedList.java: -------------------------------------------------------------------------------- 1 | //Program to Implement LinkedList. 2 | 3 | public class LinkedList{ 4 | //Node class. 5 | static class Node{ 6 | int key; //data value. 7 | Node next; //next Pointer. 8 | 9 | public Node(int data){ 10 | this.key=data; 11 | this.next=null; 12 | } 13 | } 14 | 15 | static Node head; //head of the linked list. i.e first element. 16 | static int size; //number of elements in the list. 17 | 18 | 19 | public LinkedList(){ 20 | head=null; 21 | size=0; 22 | } 23 | 24 | //function to add elements at the front of the list. 25 | static public void pushFront(int number){ 26 | System.out.println("Inserting node at front : " + number); 27 | Node node = new Node(number); 28 | if(head==null){ 29 | head=node; 30 | size++; 31 | return; 32 | } 33 | 34 | node.next=head; 35 | head=node; 36 | size++; 37 | } 38 | 39 | //function to add elements at the back of the list. 40 | static public void pushBack(int number){ 41 | System.out.println("Inserting node at back : " + number); 42 | Node node = new Node(number); 43 | if(head==null){ 44 | head=node; 45 | size++; 46 | return; 47 | } 48 | Node temp=head; 49 | while(temp.next!=null){ 50 | temp=temp.next; 51 | } 52 | temp.next=node; 53 | size++; 54 | } 55 | 56 | //function to remove element from the front of the list. 57 | static public void popFront(){ 58 | System.out.println("PopFront Operation."); 59 | if(head==null){ 60 | return; 61 | } 62 | 63 | head=head.next; 64 | size--; 65 | } 66 | 67 | //function to remove elements from the back of the list. 68 | static public void popBack(){ 69 | System.out.println("PopBack Operation."); 70 | if(head==null){ 71 | return; 72 | } 73 | 74 | if(head.next==null){ 75 | head=null; 76 | size--; 77 | return; 78 | } 79 | 80 | Node temp=head; 81 | while(temp.next.next!=null){ 82 | temp=temp.next; 83 | } 84 | temp.next=null; 85 | size--; 86 | } 87 | 88 | //function to remove element at a specific position. 89 | static public void pop(int position){ 90 | System.out.println("Poping element at pos : " + position); 91 | if(head==null){ 92 | return; 93 | } 94 | if(position==0){ 95 | head=head.next; 96 | size--; 97 | return; 98 | } 99 | 100 | Node temp=head; 101 | int x=0; 102 | while(temp.next!=null && x0){ 25 | if(array[(indexofnumtoSift-1)/2]array.length-1){ 61 | throw new Exception("Cannot insert,Size overflow error!"); 62 | } 63 | array[addPointer]=number; 64 | siftUp(addPointer); 65 | addPointer++; 66 | } 67 | 68 | //this function extracts the max element.In this we first swap the first element with last and then siftdown the first element upto the secondlast element because last is the max element.// 69 | public int extractMax() throws Exception{ 70 | System.out.println("Extract Max Operation."); 71 | if(addPointer==0){ 72 | throw new Exception("PriorityQueue is empty"); 73 | } 74 | int numtoExtract=array[0]; 75 | swap(0,addPointer-1); 76 | addPointer--; 77 | siftDown(0,addPointer-1); 78 | return numtoExtract; 79 | } 80 | 81 | //this function returns the max element.// 82 | public int getMax() throws Exception{ 83 | if(addPointer==0){ 84 | throw new Exception("PriorityQueue is empty."); 85 | } 86 | return array[0]; 87 | } 88 | 89 | //this function changes the priority and sift the element up or down according to newValue of element.// 90 | public void changePriority(int indexofElement, int newValue){ 91 | System.out.println("Change Priority Operation. Index = " + indexofElement + " New Value = " + newValue); 92 | if(array[indexofElement]>newValue){ 93 | array[indexofElement]=newValue; 94 | siftDown(indexofElement,addPointer-1); 95 | } 96 | else{ 97 | array[indexofElement]=newValue; 98 | siftUp(indexofElement); 99 | } 100 | } 101 | 102 | //this function removes the element at the given index.// 103 | public void remove(int index){ 104 | System.out.println("Remove element at index : " + index); 105 | array[index]=Integer.MAX_VALUE; 106 | siftUp(index); 107 | try{ 108 | extractMax(); 109 | } 110 | catch(Exception e){ 111 | System.out.println(e); 112 | } 113 | } 114 | 115 | public void display(){ 116 | System.out.print("\nPriorityQueue : "); 117 | if(addPointer==0){ 118 | System.out.println("Empty Queue."); 119 | return; 120 | } 121 | for(int i=0;idata){ 43 | if(temp.left.data==-1){ 44 | temp.left = node; 45 | node.parent = temp; 46 | balance(node); //balance the tree. 47 | return; 48 | } 49 | temp = temp.left; 50 | continue; 51 | } 52 | if(temp.datadata){ 78 | temp = temp.left; 79 | continue; 80 | } 81 | if(temp.data 214 | public static void balance(Node node){ 215 | System.out.println("Balancing Node : " + node.data); 216 | 217 | //if given node is root node. 218 | if(node.parent == null){ 219 | root = node; 220 | root.color = 'B'; 221 | return; 222 | } 223 | 224 | //if node's parent color is black. 225 | if(node.parent.color=='B'){ 226 | return; 227 | } 228 | 229 | //get the node's parent's sibling node. 230 | Node sibling = null; 231 | if(node.parent.parent.left == node.parent){ 232 | sibling = node.parent.parent.right; 233 | } 234 | else{ 235 | sibling = node.parent.parent.left; 236 | } 237 | 238 | //if sibling color is red. 239 | if(sibling.color == 'R'){ 240 | node.parent.color = 'B'; 241 | sibling.color = 'B'; 242 | node.parent.parent.color = 'R'; 243 | balance(node.parent.parent); 244 | return; 245 | } 246 | 247 | //if sibling color is black. 248 | else{ 249 | if(node.parent.left == node && node.parent.parent.left == node.parent){ 250 | rightRotate(node.parent); 251 | balance(node.parent); 252 | return; 253 | } 254 | if(node.parent.right == node && node.parent.parent.right == node.parent){ 255 | leftRotate(node.parent); 256 | balance(node.parent); 257 | return; 258 | } 259 | if(node.parent.right == node && node.parent.parent.left == node.parent){ 260 | leftRotate(node); 261 | rightRotate(node); 262 | balance(node); 263 | return; 264 | } 265 | if(node.parent.left == node && node.parent.parent.right == node.parent){ 266 | rightRotate(node); 267 | leftRotate(node); 268 | balance(node); 269 | return; 270 | } 271 | } 272 | } 273 | 274 | //function to perform Left Rotation. 275 | private static void leftRotate(Node node){ 276 | System.out.println("Rotating left : " + node.data); 277 | Node parent = node.parent; 278 | Node left = node.left; 279 | node.left = parent; 280 | parent.right = left; 281 | if(left!=null){ 282 | left.parent = parent; 283 | } 284 | char c = parent.color; 285 | parent.color = node.color; 286 | node.color = c; 287 | Node gp = parent.parent; 288 | parent.parent = node; 289 | node.parent = gp; 290 | 291 | if(gp==null){ 292 | root = node; 293 | return; 294 | } 295 | else{ 296 | if(gp.left == parent){ 297 | gp.left = node; 298 | } 299 | else{ 300 | gp.right = node; 301 | } 302 | } 303 | } 304 | 305 | //function to perform Right Rotation. 306 | private static void rightRotate(Node node){ 307 | System.out.println("Rotating right : " + node.data); 308 | Node parent = node.parent; 309 | Node right = node.right; 310 | node.right = parent; 311 | parent.left = right; 312 | if(right!=null){ 313 | right.parent = parent; 314 | } 315 | char c = parent.color; 316 | parent.color = node.color; 317 | node.color = c; 318 | Node gp = parent.parent; 319 | parent.parent = node; 320 | node.parent = gp; 321 | 322 | if(gp==null){ 323 | root = node; 324 | return; 325 | } 326 | else{ 327 | if(gp.left == parent){ 328 | gp.left = node; 329 | } 330 | else{ 331 | gp.right = node; 332 | } 333 | } 334 | } 335 | 336 | //function for PreOrder Traversal of the tree. 337 | private static void preOrder(Node node){ 338 | if(node.data==-1){ 339 | return; 340 | } 341 | System.out.print(node.data + "-" + node.color + " "); 342 | preOrder(node.left); 343 | preOrder(node.right); 344 | } 345 | 346 | //function to display the tree. 347 | public static void display(){ 348 | if(root == null){ 349 | System.out.println("Empty Tree"); 350 | return; 351 | } 352 | 353 | System.out.print("Tree's PreOrder Traversal : "); 354 | preOrder(root); 355 | System.out.println(); 356 | } 357 | 358 | //main function to run the program. 359 | public static void main(String [] args){ 360 | add(10); 361 | display(); 362 | 363 | add(5); 364 | display(); 365 | 366 | add(30); 367 | display(); 368 | 369 | add(1); 370 | display(); 371 | 372 | add(7); 373 | display(); 374 | 375 | add(20); 376 | display(); 377 | 378 | add(38); 379 | display(); 380 | 381 | add(35); 382 | display(); 383 | 384 | add(8); 385 | display(); 386 | 387 | remove(20); 388 | display(); 389 | 390 | remove(30); 391 | display(); 392 | 393 | remove(35); 394 | display(); 395 | 396 | remove(1); 397 | display(); 398 | } 399 | } 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | -------------------------------------------------------------------------------- /Splay Tree/SplayTree.java: -------------------------------------------------------------------------------- 1 | //Program to implement a Splay Tree. 2 | 3 | public class SplayTree{ 4 | static class Node{ 5 | int data; //data value 6 | Node left; //points to left child. 7 | Node right; //points to right child 8 | Node parent; //points to the parent node. 9 | 10 | public Node(int data){ 11 | this.data = data; 12 | this.left = null; 13 | this.right = null; 14 | this.parent = null; 15 | } 16 | } 17 | 18 | static Node root; //root of the tree. 19 | 20 | //function for performing a Left Rotation. 21 | private static void leftRotate(Node node){ 22 | System.out.println("Rotating left : " + node.data); 23 | Node parent = node.parent; 24 | Node left = node.left; 25 | node.left = parent; 26 | parent.right = left; 27 | if(left!=null){ 28 | left.parent = parent; 29 | } 30 | Node gp = node.parent.parent; 31 | parent.parent = node; 32 | node.parent = gp; 33 | 34 | if(gp==null){ 35 | root = node; 36 | } 37 | else{ 38 | if(gp.left == parent){ 39 | gp.left = node; 40 | } 41 | else{ 42 | gp.right = node; 43 | } 44 | } 45 | } 46 | 47 | //function for performing a Right Rotation. 48 | private static void rightRotate(Node node){ 49 | System.out.println("Rotating right : " + node.data); 50 | Node parent = node.parent; 51 | Node right = node.right; 52 | node.right = parent; 53 | parent.left = right; 54 | if(right!=null){ 55 | right.parent = parent; 56 | } 57 | Node gp = parent.parent; 58 | node.parent = gp; 59 | parent.parent = node; 60 | 61 | if(gp==null){ 62 | root = node; 63 | } 64 | else{ 65 | if(gp.left==parent){ 66 | gp.left = node; 67 | } 68 | else{ 69 | gp.right = node; 70 | } 71 | } 72 | } 73 | 74 | //function for performing the Splay Operation. 75 | private static void splay(Node node){ 76 | System.out.println("Splaying node : " + node.data); 77 | 78 | if(node.parent == null){ 79 | root = node; 80 | return; 81 | } 82 | 83 | //zig Rotation. 84 | if(node.parent.parent==null){ 85 | if(node.parent.right == node){ 86 | leftRotate(node); 87 | root = node; 88 | } 89 | else{ 90 | rightRotate(node); 91 | root = node; 92 | } 93 | return; 94 | } 95 | 96 | //zig-zag rotation - left rotation then right rotation 97 | if(node.parent.right==node && node.parent.parent.left == node.parent){ 98 | leftRotate(node); 99 | rightRotate(node); 100 | splay(node); 101 | return; 102 | } 103 | 104 | //zigzag rotation - right rotation then left rotaion. 105 | if(node.parent.left == node && node.parent.parent.right == node.parent){ 106 | rightRotate(node); 107 | leftRotate(node); 108 | splay(node); 109 | return; 110 | } 111 | 112 | //zig-zig rotation left rotation then left rotation. 113 | if(node.parent.right==node && node.parent.parent.right == node.parent){ 114 | leftRotate(node.parent); 115 | leftRotate(node); 116 | splay(node); 117 | return; 118 | } 119 | 120 | //zig-zig rotation right rotation then right rotation. 121 | if(node.parent.left == node && node.parent.parent.left == node.parent){ 122 | rightRotate(node.parent); 123 | rightRotate(node); 124 | splay(node); 125 | return; 126 | } 127 | } 128 | 129 | //function to add new elements in the tree. 130 | public static void add(int data){ 131 | System.out.println("Inserting data : " + data); 132 | Node node = new Node(data); 133 | if(root == null){ 134 | root = node; 135 | return; 136 | } 137 | 138 | Node temp = root; 139 | 140 | while(temp!=null){ 141 | if(temp.data>data){ 142 | if(temp.left==null){ 143 | temp.left = node; 144 | node.parent = temp; 145 | splay(node); //perform splay operation. 146 | return; 147 | } 148 | temp = temp.left; 149 | } 150 | 151 | if(temp.datadata){ 179 | temp = temp.left; 180 | } 181 | else{ 182 | temp = temp.right; 183 | } 184 | } 185 | System.out.println("Node not found."); 186 | return null; 187 | } 188 | 189 | //function to find the min value from the given node. 190 | public static Node findMin(Node node){ 191 | if(node==null){ 192 | System.out.println("Empty Tree."); 193 | return null; 194 | } 195 | System.out.println("Finding Minimum."); 196 | Node min = node; 197 | while(min.left!=null){ 198 | min = min.left; 199 | } 200 | splay(min); 201 | System.out.println("Minimum node : " + min.data); 202 | return min; 203 | } 204 | 205 | //function to remove elements from the tree. 206 | public static void delete(int data){ 207 | System.out.println("Deleting node : " + data); 208 | Node node = find(data); 209 | if(node==null){ 210 | System.out.println("Node not present in the tree."); 211 | return; 212 | } 213 | 214 | Node min = findMin(node.right); 215 | if(min==null){ 216 | root = root.left; 217 | root.parent = null; 218 | return; 219 | } 220 | root.left = root.left.left; 221 | if(root.left!=null){ 222 | root.left.parent = root; 223 | } 224 | } 225 | 226 | //function for PreOrder Traversal of the tree. 227 | private static void preOrder(Node node){ 228 | if(node==null){ 229 | return; 230 | } 231 | System.out.print(node.data + " "); 232 | preOrder(node.left); 233 | preOrder(node.right); 234 | } 235 | 236 | //function to display the tree. 237 | public static void display(){ 238 | System.out.print("Tree's PreOrder Traveral : "); 239 | preOrder(root); 240 | System.out.println(); 241 | } 242 | 243 | //main function to run the program. 244 | public static void main(String [] args){ 245 | add(10); 246 | display(); 247 | add(8); 248 | display(); 249 | add(16); 250 | display(); 251 | add(11); 252 | display(); 253 | add(15); 254 | display(); 255 | add(6); 256 | display(); 257 | 258 | delete(16); 259 | display(); 260 | 261 | delete(8); 262 | display(); 263 | } 264 | } -------------------------------------------------------------------------------- /Stack/StackImplementation/MyStack.java: -------------------------------------------------------------------------------- 1 | //Program to implement Stack using Array. 2 | 3 | import java.lang.Exception; 4 | public class MyStack{ 5 | int topPointer; //points to the position of latest added element. 6 | int [] arraytostoreStack; //array. 7 | 8 | public MyStack(int size){ 9 | topPointer=-1; //initially points to nothing as empty. 10 | arraytostoreStack = new int[size]; 11 | } 12 | 13 | //function to add elements into the stack. 14 | public void push(int number) throws Exception { 15 | //if stack is full. 16 | if(topPointer>=arraytostoreStack.length-1){ 17 | throw new Exception("Cannot push,Stack is full"); 18 | } 19 | else{ 20 | System.out.println("Push num : " + number); 21 | topPointer=topPointer+1; 22 | arraytostoreStack[topPointer]=number; 23 | } 24 | } 25 | 26 | //function to get the latest added element without removing it. 27 | public int top() throws Exception{ 28 | //if stack is empty. 29 | if(topPointer==-1){ 30 | throw new Exception("Stack is empty."); 31 | } 32 | return arraytostoreStack[topPointer]; 33 | } 34 | 35 | //function to remove the latest added element. 36 | public int pop() throws Exception { 37 | //if stack is empty. 38 | if(topPointer==-1){ 39 | throw new Exception("Cannot pop,Stack is empty."); 40 | } 41 | System.out.println("Poping."); 42 | int topElement=arraytostoreStack[topPointer]; 43 | topPointer=topPointer-1; 44 | return topElement; 45 | } 46 | 47 | //function to get the number of elements in the stack. 48 | public int size(){ 49 | return topPointer+1; 50 | } 51 | 52 | //function to display the stack. 53 | public void displayStack(){ 54 | System.out.print("Stack : [" + " "); 55 | for(int i=0;i<=topPointer;i++){ 56 | System.out.print(arraytostoreStack[i]+" "); 57 | } 58 | System.out.println("]"); 59 | } 60 | } 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /Stack/StackImplementation/StackImplementation.java: -------------------------------------------------------------------------------- 1 | //Using the MyStack class. 2 | public class StackImplementation{ 3 | public static void main(String [] args){ 4 | MyStack stack = new MyStack(5); //create stack of size = 5 5 | try{ 6 | stack.push(10); //push num=10 7 | stack.push(12); //push num=12 8 | stack.push(14); //push num=14 9 | 10 | System.out.println("Top Element : " + stack.top()); 11 | 12 | stack.push(16); //push num=16 13 | stack.push(18); //push num=18 14 | 15 | stack.displayStack(); //display stack. 16 | System.out.println("Size = "+stack.size()); //get size 17 | 18 | stack.pop(); //remove topmost element 19 | stack.displayStack(); //display stack. 20 | stack.pop(); //remove topmost element 21 | stack.displayStack(); //display stack. 22 | stack.pop(); //remove topmost element 23 | stack.displayStack(); //display stack. 24 | stack.pop(); //remove topmost element 25 | stack.displayStack(); //display stack. 26 | stack.pop(); //remove topmost element 27 | stack.displayStack(); //display stack. 28 | 29 | System.out.println("Size = "+stack.size()); //get size 30 | } 31 | catch(Exception e){ 32 | System.out.println(e); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Stack/StackwithArray/Stack.java: -------------------------------------------------------------------------------- 1 | //Program to implement Stack using an array. 2 | 3 | public class Stack{ 4 | static int [] array; //array to store numbers. 5 | static int size; //stores the number of elements in the array. 6 | static int addPointer; //points to the position, where new number is to be added. 7 | 8 | public Stack(){ 9 | array=new int[100]; //length = 100 10 | size=0; //initial size = 0. 11 | addPointer=0; //initially points to position = 0; 12 | } 13 | 14 | //function to add elements in the stack. 15 | static public void push(int number){ 16 | //if stack is full. 17 | if(addPointer>=array.length){ 18 | System.out.println("Stack is Full!"); 19 | return; 20 | } 21 | 22 | System.out.println("Push Operation : " + number); 23 | 24 | //else just add the element. 25 | array[addPointer]=number; 26 | addPointer++; 27 | size++; 28 | } 29 | 30 | //function to remove the top element. 31 | static public int pop(){ 32 | //if stack is empty. 33 | if(addPointer==0){ 34 | System.out.println("Stack is Empty!"); 35 | return -1; 36 | } 37 | 38 | System.out.println("Pop Operation."); 39 | 40 | //else just return and remove the element. 41 | addPointer--; 42 | size--; 43 | return array[addPointer]; 44 | 45 | } 46 | 47 | //function to display the stack. 48 | static public void display(){ 49 | if(addPointer==0){ 50 | System.out.println("Empty Stack."); 51 | return; 52 | } 53 | System.out.print("Stack : "); 54 | for(int i=0;i