├── DoublyLinkedList ├── DoublyLinkedListImplementation │ ├── Node.java │ ├── DoublyLinkedListImplementation.java │ └── DoublyLinkedList.java └── DoublyLinkedList │ └── DoublyLinkedList.java ├── Queue ├── DequeueImplementation │ ├── DequeueImplementation.java │ └── Dequeue.java ├── QueueImplementation │ ├── QueueImplementation.java │ └── Queue.java ├── QueuewithLinkedList │ └── Queue.java └── QueuewithArray │ └── Queue.java ├── BinarySearchTree ├── BinarySearchTree 2 │ ├── Node │ │ └── Node.java │ ├── BinarySearchTreeImplementation │ │ └── BinarySearchTreeImplementation.java │ └── BinarySearchTree │ │ └── BinarySearchTree.java ├── BinarySearchTree 1 │ ├── BinarySearchTreeImplementation.java │ └── BinarySearchTree.java └── BinarySearchTree 3 │ └── BinarySearchTree.java ├── LICENSE ├── LinkedList ├── LinkedListImplementation │ ├── LinkedListImplementation.java │ └── LinkedList.java ├── LinkedList SwapNodes │ └── LinkedList.java ├── LinkedList │ └── LinkedList.java └── CircularLinkedList │ └── CircularLinkedList.java ├── Stack ├── StackImplementation │ ├── StackImplementation.java │ └── MyStack.java ├── StackwithArray │ └── Stack.java └── StackwithLinkedList │ └── Stack.java ├── Array ├── ArrayDeletion │ └── ArrayDeletion.java ├── ArrayInsert │ └── ArrayInsertion.java └── FullArray │ └── Array.java ├── ArrayList └── ArrayListImplementation │ ├── ArrayListImplementation.java │ └── ArrayList.java ├── PriorityQueue └── PriorityQueueImplementation │ ├── PriorityQueueImplementation.java │ └── PriorityQueue.java ├── DisjointSet └── DisjointSetImplementation │ ├── DisjointSetImplementation.java │ └── DisjointSet.java ├── B-Tree └── B-Tree Insert │ └── BTree.java ├── Splay Tree └── SplayTree.java ├── README.md ├── AVLTree ├── AvlTree Insert │ └── AvlTree.java └── AvlTree Delete │ └── AvlTree.java └── Red-Black Tree └── RedBlackTree.java /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 | } -------------------------------------------------------------------------------- /Queue/DequeueImplementation/DequeueImplementation.java: -------------------------------------------------------------------------------- 1 | //Program to execute Dequeue. 2 | public class DequeueImplementation{ 3 | public static void main(String [] args){ 4 | Dequeue queue = new Dequeue(5); 5 | queue.addRight(10); 6 | queue.display(); 7 | 8 | queue.addLeft(20); 9 | queue.display(); 10 | 11 | queue.addRight(12); 12 | queue.display(); 13 | 14 | queue.addLeft(18); 15 | queue.display(); 16 | 17 | queue.addRight(16); 18 | queue.display(); 19 | } 20 | } -------------------------------------------------------------------------------- /BinarySearchTree/BinarySearchTree 2/Node/Node.java: -------------------------------------------------------------------------------- 1 | //Program for Node of the BinarySearchTree. 2 | public class Node{ 3 | int key; //data value. 4 | Node parent; //points to the parent of the node. 5 | Node left; //points to the left child of the node. 6 | Node right; //points to the right child of the node. 7 | int height; //height of the node. 8 | 9 | public Node(int data){ 10 | this.key=data; 11 | this.parent=null; 12 | this.left=null; 13 | this.right=null; 14 | this.height=0; 15 | } 16 | } -------------------------------------------------------------------------------- /BinarySearchTree/BinarySearchTree 1/BinarySearchTreeImplementation.java: -------------------------------------------------------------------------------- 1 | //Program to execute the BinarySearchTree. 2 | public class BinarySearchTreeImplementation{ 3 | public static void main(String [] args){ 4 | BinarySearchTree tree = new BinarySearchTree(); 5 | try{ 6 | tree.add(10); 7 | tree.add(12); 8 | tree.add(20); 9 | tree.add(100); 10 | tree.add(56); 11 | tree.add(1); 12 | tree.add(67); 13 | tree.add(2); 14 | tree.add(4); 15 | tree.add(9); 16 | tree.add(82); 17 | 18 | System.out.println("Find 1 : " + tree.findNode(1)); 19 | 20 | System.out.println("InOrder"); 21 | tree.display("inOrder"); 22 | 23 | System.out.println("PreOrder"); 24 | tree.display("preOrder"); 25 | 26 | System.out.println("PostOrder"); 27 | tree.display("postOrder"); 28 | 29 | System.out.println("LevelOrder"); 30 | tree.display("levelOrder"); 31 | } 32 | catch(Exception e){ 33 | System.out.println(e); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Queue/QueueImplementation/QueueImplementation.java: -------------------------------------------------------------------------------- 1 | //Program to implement the created Queue. 2 | public class QueueImplementation{ 3 | public static void main(String [] args){ 4 | Queue queue = new Queue(5); //create queue of size=5. 5 | try{ 6 | System.out.println("Is Queue empty? : " + queue.empty()); 7 | 8 | queue.enqueue(10); 9 | queue.enqueue(12); 10 | queue.enqueue(14); 11 | queue.enqueue(16); 12 | queue.enqueue(18); 13 | 14 | queue.display(); 15 | 16 | System.out.println("Size of Queue : " + queue.size()); 17 | 18 | queue.dequeue(); 19 | queue.dequeue(); 20 | 21 | queue.display(); 22 | 23 | 24 | System.out.println("Size of Queue : " + queue.size()); 25 | 26 | queue.enqueue(20); 27 | 28 | System.out.println("Is Queue empty? : " + queue.empty()); 29 | 30 | System.out.println("Size of Queue : " + queue.size()); 31 | 32 | queue.dequeue(); 33 | queue.dequeue(); 34 | 35 | queue.display(); 36 | 37 | queue.dequeue(); 38 | queue.dequeue(); 39 | 40 | queue.display(); 41 | 42 | System.out.println("Size of Queue : " + queue.size()); 43 | } 44 | catch(Exception e){ 45 | System.out.println(e); 46 | } 47 | } 48 | } 49 | 50 | -------------------------------------------------------------------------------- /LinkedList/LinkedListImplementation/LinkedListImplementation.java: -------------------------------------------------------------------------------- 1 | //Program to execute the LinedList. 2 | public class LinkedListImplementation{ 3 | public static void main(String [] args){ 4 | LinkedList list = new LinkedList(); //create a LinkedList. 5 | try{ 6 | list.add(10); 7 | list.popFront(); 8 | list.add(12); 9 | 10 | list.display(); 11 | 12 | list.pushFront(22); 13 | list.display(); 14 | 15 | list.add(14); 16 | list.display(); 17 | 18 | list.add(16); 19 | list.display(); 20 | 21 | System.out.println("Size : " + list.size()); 22 | System.out.println("Top elements is : " + list.topFront()); 23 | 24 | list.popFront(); 25 | 26 | list.display(); 27 | 28 | System.out.println("Size : " + list.size()); 29 | System.out.println("Last element is : " + list.topBack()); 30 | 31 | list.pushBack(24); 32 | 33 | System.out.println("Last element is : " + list.topBack()); 34 | 35 | list.add(18); 36 | 37 | System.out.println("Size : " + list.size()); 38 | 39 | list.pushFront(20); 40 | 41 | System.out.println("Size : " + list.size()); 42 | 43 | list.display(); 44 | } 45 | catch(Exception e){ 46 | System.out.println(e); 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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;iarray.length-1){ 27 | int [] newArray = new int[2*array.length]; 28 | for(int i=0;i list = tree.rangeSearch(7,23); 29 | 30 | for(int i=0;i list2 = tree.rangeSearch(7,23); 46 | 47 | for(int i=0;i=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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Queue/QueuewithLinkedList/Queue.java: -------------------------------------------------------------------------------- 1 | //Program to implement Queue fusing LinkedList. 2 | public class Queue{ 3 | //class for elements. 4 | static class Node{ 5 | int key; //data value 6 | Node next;//points to next element in the queue. 7 | 8 | public Node(int data){ 9 | this.key=data; 10 | this.next=null; 11 | } 12 | } 13 | 14 | static int size; //number of elements in the queue. 15 | static Node head; //head of the queue i.e first element. 16 | 17 | public Queue(){ 18 | size=0; 19 | head=null; 20 | } 21 | 22 | //function to add elements in the queue. 23 | static public void enqueue(int number){ 24 | System.out.println("Inserting : " + number); 25 | Node node =new Node(number); 26 | 27 | //if queue is empty. 28 | if(head==null){ 29 | head=node; 30 | size++; 31 | return; 32 | } 33 | 34 | Node temp= head; 35 | while(temp.next!=null){ 36 | temp=temp.next; 37 | } 38 | temp.next=node; 39 | size++; 40 | } 41 | 42 | //function to delete elements from the queue. 43 | static public int dequeue(){ 44 | System.out.println("Dequeue Operation."); 45 | //if queue is empty. 46 | if(head==null){ 47 | System.out.println("Empty queue."); 48 | return -1; 49 | } 50 | int temp = head.key; 51 | head=head.next; 52 | size--; 53 | return temp; 54 | } 55 | 56 | static public void display(){ 57 | if(head==null){ 58 | System.out.println("Empty queue."); 59 | return; 60 | } 61 | 62 | System.out.print("Queue : "); 63 | Node temp = head; 64 | while(temp!=null){ 65 | System.out.print(temp.key + " "); 66 | temp = temp.next; 67 | } 68 | System.out.println(); 69 | } 70 | 71 | //main function to run the program. 72 | static public void main(String [] args){ 73 | Queue queue = new Queue(); 74 | queue.enqueue(10); 75 | queue.enqueue(20); 76 | queue.enqueue(30); 77 | queue.enqueue(40); 78 | 79 | queue.display(); 80 | 81 | queue.dequeue(); 82 | 83 | queue.enqueue(50); 84 | 85 | queue.display(); 86 | 87 | queue.dequeue(); 88 | queue.dequeue(); 89 | 90 | queue.display(); 91 | 92 | queue.dequeue(); 93 | queue.dequeue(); 94 | 95 | queue.display(); 96 | } 97 | } 98 | 99 | -------------------------------------------------------------------------------- /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;i2->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 | -------------------------------------------------------------------------------- /Stack/StackwithLinkedList/Stack.java: -------------------------------------------------------------------------------- 1 | //Program to implement Stack using LinkedList. 2 | public class Stack{ 3 | //class Node for LinkedList. 4 | static class Node{ 5 | int key; //data value 6 | Node next; //next pointer 7 | 8 | public Node(int number){ 9 | this.key=number; 10 | this.next=null; 11 | } 12 | } 13 | 14 | static Node head; //head of linkedlist 15 | static int size; 16 | 17 | public Stack(){ 18 | head=null; 19 | size=0; 20 | } 21 | 22 | //function to add elements into the stack. 23 | static public void push(int number){ 24 | Node node = new Node(number); //create new node. 25 | System.out.println("Push data : " + number); 26 | 27 | node.next = head; 28 | head = node; 29 | size++; 30 | } 31 | 32 | //method to get the element at top of the stack 33 | static public int top(){ 34 | if(head==null){ 35 | System.out.println("Stack is Empty!"); 36 | return -1; 37 | } 38 | 39 | System.out.println("Element at top of stack : " + head.key); 40 | return head.key; 41 | } 42 | 43 | 44 | //function to remove topmost element from the stack. 45 | static public int pop(){ 46 | //if head is null = stack empty. 47 | if(head==null){ 48 | System.out.println("Empty Stack."); 49 | return -1; 50 | } 51 | 52 | System.out.println("Pop Operation."); 53 | 54 | int temp = head.key; 55 | head = head.next; 56 | size--; 57 | 58 | return temp; 59 | } 60 | 61 | //function to display the stack. 62 | static public void display(){ 63 | //if stack is empty. 64 | if(head==null){ 65 | System.out.println("Stack is empty."); 66 | return; 67 | } 68 | 69 | System.out.println(); 70 | System.out.println("Stack "); 71 | Node temp=head; 72 | while(temp!=null){ 73 | System.out.println("----------"); 74 | System.out.println("| " + temp.key + " |"); 75 | temp=temp.next; 76 | } 77 | System.out.println("----------"); 78 | System.out.println(); 79 | } 80 | 81 | //method to get the number of elements in the stack. 82 | static public int getSize(){ 83 | return size; 84 | } 85 | 86 | static public void main(String [] args){ 87 | Stack stack = new Stack(); //create new stack. 88 | stack.push(10); //push num = 10 89 | stack.push(20); //push num = 20 90 | stack.push(30); //push num = 30 91 | 92 | stack.display(); //display stack. 93 | stack.top(); 94 | 95 | stack.pop(); //pop topmost element 96 | stack.pop(); //pop topmost element 97 | stack.display(); //display stack. 98 | stack.pop(); //pop topmost element 99 | stack.display(); //display stack. 100 | } 101 | } 102 | 103 | -------------------------------------------------------------------------------- /Queue/QueuewithArray/Queue.java: -------------------------------------------------------------------------------- 1 | //Program to implement Circular Queue using an array. 2 | 3 | public class Queue{ 4 | static int [] array; //arary to store values. 5 | static int size; //number of elements in the queue. 6 | static int addPointer;//points to position where the latest number is added. 7 | static int removePointer;//point to position of the number which is to be deleted. 8 | 9 | public Queue(int size){ 10 | array=new int[size+1]; 11 | this.size=0; 12 | addPointer=0; 13 | removePointer=0; 14 | } 15 | 16 | //function to add elements into the queue. 17 | static public void enqueue(int number){ 18 | //if queue is full. 19 | if(addPointer+1==removePointer){ 20 | System.out.println("Queue is Full."); 21 | return; 22 | } 23 | if(addPointer==array.length-1){ 24 | if(removePointer==0){ 25 | System.out.println("Queue is Full."); 26 | return; 27 | } 28 | else{ 29 | System.out.println("Enqueue num : " + number); 30 | array[addPointer]=number; 31 | addPointer=0; 32 | size++; 33 | return; 34 | } 35 | } 36 | 37 | System.out.println("Enqueue num : " + number); 38 | array[addPointer]=number; 39 | addPointer++; 40 | size++; 41 | } 42 | 43 | //function to remove element. 44 | static public int dequeue(){ 45 | //if queue is empty. 46 | if(removePointer==addPointer){ 47 | System.out.println("Empty Queue."); 48 | return -1; 49 | } 50 | 51 | System.out.println("Dequeue Operation."); 52 | 53 | if(removePointer==array.length-1){ 54 | int temp = array[removePointer]; 55 | removePointer=0; 56 | size--; 57 | return temp; 58 | } 59 | removePointer++; 60 | size--; 61 | return array[removePointer-1]; 62 | } 63 | 64 | //function to display queue. 65 | static public void display(){ 66 | if(size==0){ 67 | System.out.println("Empty Queue."); 68 | return; 69 | } 70 | 71 | System.out.print("Queue : ") ; 72 | for(int i=removePointer;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 | -------------------------------------------------------------------------------- /LinkedList/LinkedListImplementation/LinkedList.java: -------------------------------------------------------------------------------- 1 | //Program to implement LinkedList. 2 | 3 | public class LinkedList{ 4 | int data; 5 | LinkedList nextPointer; 6 | 7 | static int numofElements; 8 | static LinkedList headPointer; 9 | static LinkedList tailPointer; 10 | 11 | public LinkedList(){ 12 | nextPointer=null; 13 | } 14 | 15 | //function to add element into the linked list. 16 | public void add(int number){ 17 | System.out.println("Adding element at the back of the list : " + number); 18 | LinkedList node = new LinkedList(); 19 | node.data=number; 20 | 21 | if(numofElements==0){ 22 | headPointer=node; 23 | tailPointer=node; 24 | } 25 | else{ 26 | tailPointer.nextPointer=node; 27 | tailPointer=node; 28 | } 29 | numofElements++; 30 | 31 | } 32 | 33 | //function to add element at the front of the list. 34 | public void pushFront(int number){ 35 | System.out.println("Inserting element at the front of the list : " + number); 36 | LinkedList node = new LinkedList(); 37 | node.data=number; 38 | numofElements++; 39 | 40 | if(headPointer==null){ 41 | headPointer=node; 42 | tailPointer=node; 43 | } 44 | else{ 45 | if(headPointer==tailPointer){ 46 | node.nextPointer=tailPointer; 47 | headPointer=node; 48 | } 49 | else{ 50 | node.nextPointer=headPointer; 51 | headPointer=node; 52 | } 53 | } 54 | } 55 | 56 | //function to get element at the front of the list. 57 | public int topFront() throws Exception{ 58 | if(headPointer==null){ 59 | throw new Exception("LinkedList is empty"); 60 | } 61 | return headPointer.data; 62 | } 63 | 64 | 65 | //function to remove elements from the front of the list. 66 | public void popFront() throws Exception{ 67 | System.out.println("PopFront Operation."); 68 | if(headPointer==null){ 69 | throw new Exception("LinkedList is empty"); 70 | } 71 | numofElements--; 72 | if(headPointer==tailPointer){ 73 | headPointer=null; 74 | tailPointer=null; 75 | } 76 | else{ 77 | headPointer=headPointer.nextPointer; 78 | } 79 | 80 | } 81 | 82 | //function to add elements at the back of the list. 83 | public void pushBack(int number){ 84 | System.out.println("Inserting element at the back of the list : " + number); 85 | LinkedList node = new LinkedList(); 86 | node.data=number; 87 | numofElements++; 88 | 89 | if(tailPointer==null){ 90 | headPointer=node; 91 | tailPointer=node; 92 | } 93 | else{ 94 | tailPointer.nextPointer=node; 95 | tailPointer=node; 96 | } 97 | } 98 | 99 | //function to get element at the back of the list. i.e last element. 100 | public int topBack() throws Exception{ 101 | if(tailPointer==null){ 102 | throw new Exception("LinkedList is empty!"); 103 | } 104 | return tailPointer.data; 105 | } 106 | 107 | //function to display the LinekdList. 108 | public void display(){ 109 | System.out.print("LinkedList : "); 110 | System.out.print("[ "); 111 | LinkedList temp = headPointer; 112 | while(temp!=null){ 113 | System.out.print(temp.data + " "); 114 | temp=temp.nextPointer; 115 | } 116 | System.out.println("]"); 117 | } 118 | 119 | //funciton to get size of the list. 120 | public int size(){ 121 | return numofElements; 122 | } 123 | } 124 | 125 | -------------------------------------------------------------------------------- /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;itemp.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 1/BinarySearchTree.java: -------------------------------------------------------------------------------- 1 | //Program to implement BinarySearchTree. 2 | 3 | import java.util.*; 4 | public class BinarySearchTree{ 5 | int data; //data value 6 | BinarySearchTree parentNode; //points to the parent. 7 | BinarySearchTree leftNode; //points to left child. 8 | BinarySearchTree rightNode; //points to right child. 9 | 10 | static int numofElements=0; //number of elements in the Tree. 11 | static BinarySearchTree rootNode; //root of the tree. 12 | 13 | public BinarySearchTree(){ 14 | parentNode=null; 15 | leftNode=null; 16 | rightNode=null; 17 | } 18 | 19 | 20 | //function to add elements in the Tree. 21 | public void add(int number){ 22 | System.out.println("Inserting data : " + number); 23 | BinarySearchTree node = new BinarySearchTree(); 24 | node.data=number; 25 | 26 | 27 | if(numofElements==0){ 28 | rootNode=node; 29 | } 30 | else{ 31 | BinarySearchTree temp = rootNode; 32 | while(temp!=null){ 33 | if(temp.datanumber){ 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.data-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;idata){ 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 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # data-structures 2 | 3 | Implemented following Data Structures using Java Language : 4 | 1. Arrays 5 | 2. ArrayList 6 | 3. LinkedList 7 | 4. Doubly-LinkedList 8 | 5. Circular LinkedList 9 | 6. Stack using Array and LinkedList 10 | 7. Queue using Array and LinkedList 11 | 8. Dequeue - Double Sided Queue 12 | 9. Priority-Queue using Max-Heap Property 13 | 10. BinarySearch Tree 14 | 11. AVL Tree 15 | 12. Splay Tree 16 | 13. Disjoint Sets 17 | 14. Red-Black Tree 18 | 19 | 20 | * **Arrays**: An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. 21 | * Functions Implemented : 22 | * Insertion in an Array. 23 | * Deletion from an Array. 24 | * Binary Search 25 | * Linear Search 26 | * Bubble Sort 27 | 28 | * **ArrayList**: In computer science, a dynamic array, growable array, or array list is a random access, variable-size list data structure that allows elements to be added or removed. Dynamic arrays overcome a limit of static arrays, which have a fixed capacity that needs to be specified at allocation. 29 | 30 | * **LinkedList**: In computer science, a linked list is a linear collection of data elements, in which linear order is not given by their physical placement in memory. Each pointing to the next node by means of a pointer. It is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of data and a reference (in other words,a link) to the next node in the sequence. This structure allows for efficient insertion or removal of elements from any position in the sequence during iteration. 31 | 32 | * **DoublyLinkedList**: In computer science, a doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes. The beginning and ending nodes' previous and next links, respectively, point to some kind of terminator, typically a sentinel node or null, to facilitate traversal of the list. It can be conceptualized as two singly linked lists formed from the same data items, but in opposite sequential orders. 33 | 34 | * **CircularLinkedList**: Circular Linked List is a variation of Linked list in which the first element points to the last element and the last element points to the first element. Both Singly Linked List and Doubly Linked List can be made into a circular linked list. 35 | 36 | * **Stack**: In computer science, a stack is an abstract data type that serves as a collection of elements, with two principal operations: 37 | * push, which adds an element to the collection, and 38 | * pop, which removes the most recently added element that was not yet removed. 39 | 40 | The order in which elements come off a stack gives rise to its alternative name, LIFO (last in, first out). Additionally, a peek operation may give access to the top without modifying the stack. 41 | 42 | * **Queue**: In computer science, a queue is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principle (or only) operations on the collection are the addition of entities to the rear terminal position, known as enqueue, and removal of entities from the front terminal position, known as dequeue. This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. 43 | 44 | * **PriorityQueue**: In computer science, a priority queue is an abstract data type where each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority. Priority queues are often implemented with heaps, they are conceptually distinct from heaps. A priority queue is an abstract concept. Just as a list can be implemented with a linked list or an array, a priority queue can be implemented with a heap or a variety of other methods such as an unordered array. 45 | 46 | * **BinarySearchTree**: A binary search tree is a rooted binary tree, whose internal nodes each store a key (and optionally, an associated value) and each have two distinguished sub-trees, commonly denoted left and right. The tree additionally satisfies the binary search tree property, which states that the key in each node must be greater than or equal to any key stored in the left sub- tree, and less than or equal to any key stored in the right sub-tree. They allow fast lookup, addition and removal of items, and can be used to implement either dynamic sets of items, or lookup tables that allow finding an item by its key (e.g., finding the phone number of a person by name). 47 | 48 | * **AVL Tree**: In computer science, an AVL tree is a self-balancing binary search tree. It was the first such data structure to be invented. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Lookup, insertion, and deletion all take O(log n) time in both the average and worst cases, where n is the number of nodes in the tree prior to the operation. Insertions and deletions may require the tree to be rebalanced by one or more tree rotations. 49 | 50 | * **Splay Tree**: A splay tree is a self-adjusting binary search tree with the additional property that recently accessed elements are quick to access again. It performs basic operations such as insertion, look-up and removal in O(log n) amortized time. All normal operations on a binary search tree are combined with one basic operation, called splaying. Splaying the tree for a certain element rearranges the tree so that the element is placed at the root of the tree. One way to do this is to first perform a standard binary tree search for the element in question, and then use tree rotations in a specific fashion to bring the element to the top. 51 | 52 | * **Red-Black Tree**: A red–black tree is a kind of self-balancing binary search tree. Each node of the binary tree has an extra bit, and that bit is often interpreted as the color (red or black) of the node. These color bits are used to ensure the tree remains approximately balanced during insertions and deletions. The balancing of the tree is not perfect, but it is good enough to allow it to guarantee searching in O(log n) time, where n is the total number of elements in the tree. The insertion and deletion operations, along with the tree rearrangement and recoloring, are also performed in O(log n) time. 53 | 54 | * Each node is either red or black. 55 | * The root is black. 56 | * All leaves (NIL) are black. 57 | * If a node is red, then both its children are black. 58 | * Every path from a given node to any of its descendant NIL nodes contains the same number of black nodes. 59 | 60 | * **Disjoint-Sets**: In computer science, a disjoint-set data structure, also called a union–find data structure or merge–find set, is a data structure that keeps track of a set of elements partitioned into a number of disjoint (non-overlapping) subsets. It provides near-constant-time operations to add new sets, to merge existing sets, and to determine whether elements are in the same set. In addition to many other uses, disjoint-sets play a key role in Kruskal's algorithm for finding the minimum spanning tree of a graph. 61 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /BinarySearchTree/BinarySearchTree 3/BinarySearchTree.java: -------------------------------------------------------------------------------- 1 | //Program to implement BinarySearchTree. 2 | 3 | public class BinarySearchTree{ 4 | static class Node{ 5 | int key; //data value 6 | Node parent; //points to the parent node. 7 | Node left; //points to the right child. 8 | Node right; //points to the left child. 9 | 10 | public Node(int data){ 11 | this.key=data; 12 | this.parent=null; 13 | this.left=null; 14 | this.right=null; 15 | } 16 | 17 | } 18 | 19 | static Node root; //root of the tree. 20 | 21 | public BinarySearchTree(){ 22 | root=null; 23 | } 24 | 25 | //function to find specific data elements in the tree. 26 | public static Node find(int data){ 27 | Node temp=root; 28 | Node prev=null; 29 | 30 | while(temp!=null){ 31 | prev=temp; 32 | 33 | if(temp.key==data){ 34 | return prev; 35 | } 36 | if(temp.key>data){ 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 | -------------------------------------------------------------------------------- /Red-Black Tree/RedBlackTree.java: -------------------------------------------------------------------------------- 1 | //Program to implement Red-Black Tree. 2 | 3 | public class RedBlackTree{ 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 | char color; //color of the node. 10 | 11 | public Node(int data, char color){ 12 | this.data = data; 13 | this.left = null; 14 | this.right = null; 15 | this.parent = null; 16 | this.color = color; 17 | } 18 | } 19 | 20 | static Node root; //root of the tree. 21 | 22 | //function to create a Node. 23 | public static Node makeNode(int data){ 24 | Node node = new Node(data,'R'); 25 | node.left = new Node(-1,'B'); 26 | node.right = new Node(-1,'B'); 27 | return node; 28 | } 29 | 30 | //function to add elements in the tree. 31 | public static void add(int data){ 32 | System.out.println("Inserting data : " + data); 33 | Node node = makeNode(data); 34 | if(root == null){ 35 | root = node; 36 | root.color = 'B'; 37 | return; 38 | } 39 | 40 | Node temp = root; 41 | while(temp!=null){ 42 | if(temp.data>data){ 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 | -------------------------------------------------------------------------------- /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 | --------------------------------------------------------------------------------