diff --git a/.gitignore b/.gitignore deleted file mode 100644 index f68d109..0000000 --- a/.gitignore +++ /dev/null @@ -1,29 +0,0 @@ -### IntelliJ IDEA ### -out/ -!**/src/main/**/out/ -!**/src/test/**/out/ - -### Eclipse ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache -bin/ -!**/src/main/**/bin/ -!**/src/test/**/bin/ - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ - -### VS Code ### -.vscode/ - -### Mac OS ### -.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index cf9abe6..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 47f5fd2..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml deleted file mode 100644 index 2b63946..0000000 --- a/.idea/uiDesigner.xml +++ /dev/null @@ -1,124 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/DataStructuresAndAlgorithms.iml b/DataStructuresAndAlgorithms.iml deleted file mode 100644 index c90834f..0000000 --- a/DataStructuresAndAlgorithms.iml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..194abf9 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# DataStructuresAndAlgorithmsInJAVA +Learn How to implement different Data Structures and Algorithms in JAVA + + +OPEN THE MASTER BRANCH + +TO VIEW THE CODE diff --git a/src/DataStructuresOrdinary/Arrays/Array.java b/src/DataStructuresOrdinary/Arrays/Array.java deleted file mode 100644 index e2ab239..0000000 --- a/src/DataStructuresOrdinary/Arrays/Array.java +++ /dev/null @@ -1,65 +0,0 @@ -package DataStructuresOrdinary.Arrays; - -import java.util.Arrays; - -public class Array { - public static void main(String[] args) { - - // ADDING && REMOVING operations has a 0(1) COMPLEXITY - // creating a one dimensional ARRAY - - int[] array1 = new int[10]; - int count = 1; - for(int i = 0; i< array1.length; i++){ - array1[i] = count; - count++; - } - for (int j=0; j linkedList = new LinkedList<>(); - linkedList.add(1); - linkedList.add(2); - linkedList.add(4); - linkedList.add(4); - linkedList.add(4,5); - - linkedList.remove(3); - - Iterator iteratorObj = linkedList.iterator(); - while (iteratorObj.hasNext()){ - System.out.print(iteratorObj.next() +" "); - } - System.out.println(); - System.out.println(linkedList.size()); - boolean search = linkedList.contains(4); - System.out.println(search); - - System.out.println(); - //Copying elements from one data structure to another - - Stack stack1 = new Stack<>(); - LinkedList linkedList2 = new LinkedList<>(); - - stack1.push("douglas"); - stack1.push("nyabasa"); - - linkedList2.addAll(stack1); - Iterator iteratorObj1 = linkedList2.iterator(); - while (iteratorObj1.hasNext()){ - System.out.print(iteratorObj1.next() +" "); - } - } - - - - - - - - -} diff --git a/src/DataStructuresOrdinary/Queue/QueueSolution.java b/src/DataStructuresOrdinary/Queue/QueueSolution.java deleted file mode 100644 index eec539d..0000000 --- a/src/DataStructuresOrdinary/Queue/QueueSolution.java +++ /dev/null @@ -1,64 +0,0 @@ -package DataStructuresOrdinary.Queue; - -class QueueImplementingArray{ - - int capacity ; - static int [] queue1; - int front, rear; - - QueueImplementingArray(int size) { - capacity= size; - queue1 = new int[capacity]; - } - - void enQueue ( int element){ - if (rear == capacity) { - System.out.println("Queue is not empty"); - } - else - { - queue1[rear] = element; - rear++; - System.out.println("Inserted" + element); - } - } - void deQueue () { - if (rear == 0){ - System.out.println("Queue empty"); - }else { - for (int i=0; i stack = new Stack<>(); - - stack.push(4); - stack.push(2); - stack.push(3); - stack.push(1); - stack.push(5); - System.out.println("\n STACK AFTER PUSHING "+ stack); - stack.pop(); - System.out.println("\n STACK AFTER POP " + stack); - System.out.println(stack.peek()); - boolean status = stack.empty(); - System.out.println(status); - int indexfound = stack.search(3); - System.out.println(indexfound); - - // Iterate over a stack - System.out.println("Iterating the stack"); - Iterator iterator = stack.iterator(); - while (iterator.hasNext()){ - Object s2 = iterator.next(); - System.out.print(s2); - } - // checking stack size - System.out.println(); - System.out.println(stack.size()); - - stack.sort(null); - System.out.println(stack); - - int array[]; - int top; - int capacity; - - //create the stackSolution -// Stack( int size){ -// array = new int[size]; -// capacity = size; -// top = -1; -// } -// // add elements to the stackSolution -// public void push ( int x){ -// if (isFull()) { -// System.out.println("DataStructuresOrdinary.Stack.StackSolution is filled"); -// System.exit(1); -// } -// System.out.println("Insert" + x); -// array[++top] = x; -// } -// private boolean isFull () { -// return top == capacity - 1; -// } -// public int pop () { -// if (isEmpty()) { -// System.out.println("DataStructuresOrdinary.Stack.StackSolution is empty"); -// System.exit(1); -// } -// return array[top--]; -// } -// public Boolean isEmpty () { -// return top == -1; -// } -// -// public int size () { -// return top + 1; -// } -// -// public void printStack () { -// for (int i = 0; i <= top; i++) { -// System.out.println(array[i]); -// } - - } - - } - diff --git a/src/DataStructuresOrdinary/circularLinkedList/CircularLinkedList.java b/src/DataStructuresOrdinary/circularLinkedList/CircularLinkedList.java deleted file mode 100644 index 55606ce..0000000 --- a/src/DataStructuresOrdinary/circularLinkedList/CircularLinkedList.java +++ /dev/null @@ -1,57 +0,0 @@ -package DataStructuresOrdinary.circularLinkedList; - -public class CircularLinkedList { - - Node head, tail; - class Node{ - int data; - Node tail; - Node next; - Node(int data){ - this.data=data; - } - } - void delete(){ - if (tail != head){ - head =head.next; - tail.next = head; - }else { - head= tail = null; - } - } - void insert(int data){ - Node newNode = new Node(data); - if (head ==null){ - head = newNode; - tail= newNode; - tail.next = newNode; - } - else { - tail.next=newNode; - tail = newNode; - tail.next = head; - } - } - void display(){ - Node n1 = head; - if (tail ==null && head == null){ - System.out.println("Circular linkedlist is empty"); - } - else { - do{ - System.out.println(n1.data); - n1=n1.next; - }while (n1 !=head); - } - } - public static void main(String[] args) { - CircularLinkedList circularLinkedList = new CircularLinkedList(); - circularLinkedList.insert(2); - circularLinkedList.insert(3); - circularLinkedList.insert(5); - circularLinkedList.display(); - System.out.println("After deleting elements"); - circularLinkedList.delete(); - circularLinkedList.display(); - } -} diff --git a/src/DynamicProgram/TowerOfHanoi.java b/src/DynamicProgram/TowerOfHanoi.java deleted file mode 100644 index 1eec75a..0000000 --- a/src/DynamicProgram/TowerOfHanoi.java +++ /dev/null @@ -1,20 +0,0 @@ -package DynamicProgram; - -public class TowerOfHanoi { - public static void main(String[] args) { - int discNo=3; - - shiftDisk(discNo,'A','B','C'); - } - public static void shiftDisk(int diskNo,char TA,char TB,char TC){ - if (diskNo==1){ - System.out.println("disk 1 is shifted from Tower " + TA +" to Tower " + TC); - }else { - shiftDisk(diskNo-1,TA,TC,TB); - System.out.println("Disk " + diskNo + " from " + TA + " is shifted to " + TC); - shiftDisk(diskNo-1,TB,TA,TC); - - } - - } -} diff --git a/src/GraphBasedDataStructureAndAlgos/AdjacencyMatrix/AdjacencyMatrix.java b/src/GraphBasedDataStructureAndAlgos/AdjacencyMatrix/AdjacencyMatrix.java deleted file mode 100644 index 00b4b1b..0000000 --- a/src/GraphBasedDataStructureAndAlgos/AdjacencyMatrix/AdjacencyMatrix.java +++ /dev/null @@ -1,52 +0,0 @@ -package GraphBasedDataStructureAndAlgos.AdjacencyMatrix; - -// 0(1) time complexity - // Adjacency Matrix representation in Java - - public class AdjacencyMatrix { - private boolean adjMatrix[][]; - private int numVertices; - - // Initialize the matrix - public AdjacencyMatrix(int numVertices) { - this.numVertices = numVertices; - adjMatrix = new boolean[numVertices][numVertices]; - } - - // Add edges - public void addEdge(int i, int j) { - adjMatrix[i][j] = true; - adjMatrix[j][i] = true; - } - - // Remove edges - public void removeEdge(int i, int j) { - adjMatrix[i][j] = false; - adjMatrix[j][i] = false; - } - - // Print the matrix - public String toString() { - StringBuilder s = new StringBuilder(); - for (int i = 0; i < numVertices; i++) { - s.append(i + ": "); - for (boolean j : adjMatrix[i]) { - s.append((j ? 1 : 0) + " "); - } - s.append("\n"); - } - return s.toString(); - } - - public static void main(String args[]) { - AdjacencyMatrix g = new AdjacencyMatrix(4); - - g.addEdge(0, 1); - g.addEdge(0, 2); - g.addEdge(1, 2); - g.addEdge(2, 0); - g.addEdge(2, 3); - - System.out.print(g.toString()); - } - } diff --git a/src/GraphBasedDataStructureAndAlgos/BreadthFirstSearch/GraphSearch.java b/src/GraphBasedDataStructureAndAlgos/BreadthFirstSearch/GraphSearch.java deleted file mode 100644 index bca57e9..0000000 --- a/src/GraphBasedDataStructureAndAlgos/BreadthFirstSearch/GraphSearch.java +++ /dev/null @@ -1,61 +0,0 @@ -package GraphBasedDataStructureAndAlgos.BreadthFirstSearch; - -import java.util.ArrayList; -import java.util.Iterator; - -// BreadthFirstSearch is an algorithm to trace nodes of a graph in a Breadth-First order -// that is the neighbour nodes in a single level are traversed first, -// then the next level will follow. -public class GraphSearch { - - int V; - ArrayList adjacency[]; - GraphSearch(int noOfvertex){ - V=noOfvertex; - adjacency = new ArrayList[noOfvertex]; - for (int i=0; i(); - } - } - void edge(int x, int y){ - adjacency[x].add(y); - - } - void breadthFirstSearch(int sourceVertex){ - boolean[] visited = new boolean[V]; - ArrayList arrayList = new ArrayList<>(); - visited[sourceVertex]= true; - arrayList.add(sourceVertex); - while (!arrayList.isEmpty()){ - sourceVertex=arrayList.remove(0); - System.out.print(sourceVertex+" "); - Iterator i = adjacency[sourceVertex].iterator(); - while (i.hasNext()){ - int n = (int) i.next(); - if (!visited[n]){ - visited[n]=true; - arrayList.add(n); - - } - } - } - } - public static void main(String[] args) { - GraphSearch graphSearch = new GraphSearch(6); - graphSearch.edge(0,1); - graphSearch.edge(0,2); - graphSearch.edge(0,5); - graphSearch.edge(1,2); - graphSearch.edge(2,0); - graphSearch.edge(2,1); - graphSearch.edge(2,3); - graphSearch.edge(2,4); - graphSearch.edge(3,2); - graphSearch.edge(4,2); - graphSearch.edge(4,5); - graphSearch.edge(5,0); - graphSearch.edge(5,4); - graphSearch.breadthFirstSearch(0); - - } -} diff --git a/src/GraphBasedDataStructureAndAlgos/DepthFirstSearch/GraphSearch.java b/src/GraphBasedDataStructureAndAlgos/DepthFirstSearch/GraphSearch.java deleted file mode 100644 index de94fd4..0000000 --- a/src/GraphBasedDataStructureAndAlgos/DepthFirstSearch/GraphSearch.java +++ /dev/null @@ -1,59 +0,0 @@ -package GraphBasedDataStructureAndAlgos.DepthFirstSearch; - -import java.util.ArrayList; -import java.util.Stack; - -//Depth-First Search is an algorithm for traversing or searching tree or graph data structures. One starts at the root -//and explores as far as possible along each branch before backtracking. -public class GraphSearch { - int V; - ArrayList[] adjacency; - GraphSearch(int noOfVertex){ - V=noOfVertex; - adjacency=new ArrayList[noOfVertex]; - for (int i =0; i(); - - } - } - void edge(int x , int y){ - adjacency[x].add(y); - } - void depthFirstSearch(int sourceVertex){ - boolean[] visited = new boolean[V]; - Stack stack = new Stack<>(); - stack.push(sourceVertex); - int node; - while (!stack.isEmpty()){ - sourceVertex = stack.peek(); - stack.pop(); - - for (int i=0;iG[i][j]){ - x=i; - y=j; - } - } - } - } - } - System.out.println(x + " - " + y + " : " + G[x][y]); - Visited[y]=true; - edgeNo++; - } - } - - public static void main(String[] args) { - PrimsAlgorithm primsAlgorithm = new PrimsAlgorithm(); - int nNode = 5; - int[][] G = { { 0, 9, 75, 0, 0 }, { 9, 0, 95, 19, 42 }, { 75, 95, 0, 51, 66 }, { 0, 19, 51, 0, 31 }, { 0, 42, 66, 31, 0 } }; - primsAlgorithm.Prim(G,nNode); - } - - - - -} diff --git a/src/GreedyAlgorithms/kruskalsAlgorithm/KruskalsAlgorithm.java b/src/GreedyAlgorithms/kruskalsAlgorithm/KruskalsAlgorithm.java deleted file mode 100644 index 0a126e8..0000000 --- a/src/GreedyAlgorithms/kruskalsAlgorithm/KruskalsAlgorithm.java +++ /dev/null @@ -1,4 +0,0 @@ -package GreedyAlgorithms.kruskalsAlgorithm; - -public class KruskalsAlgorithm { -} diff --git a/src/HashTables/HashTables.java b/src/HashTables/HashTables.java deleted file mode 100644 index 2a87cfa..0000000 --- a/src/HashTables/HashTables.java +++ /dev/null @@ -1,22 +0,0 @@ -package HashTables; - - -import java.util.Hashtable; - -// RUN TIME COMPLEXITY -// BEST CASE 0(1) -// Worst CASE 0(n) -public class HashTables { - public static void main(String[] args) { - Hashtable hashtable= new Hashtable<>(10); - hashtable.put("100","GOKU"); - hashtable.put("123","NARUTO"); - hashtable.put("321","ICHIGO"); - hashtable.put("555","GON"); - hashtable.put("777","ASTA"); - - for (String KEY: hashtable.keySet()){ - System.out.println(KEY.hashCode()%10 +"\t"+KEY+"\t"+hashtable.get(KEY)); - } - } -} diff --git a/src/Main.java b/src/Main.java deleted file mode 100644 index e1470cd..0000000 --- a/src/Main.java +++ /dev/null @@ -1,7 +0,0 @@ -public class Main { - public static void main(String[] args) { - - } -} - - diff --git a/src/SearchingAndSortingAlgorithms/InsertionSortImpl/InsertionSortAlgo.java b/src/SearchingAndSortingAlgorithms/InsertionSortImpl/InsertionSortAlgo.java deleted file mode 100644 index 9498d2e..0000000 --- a/src/SearchingAndSortingAlgorithms/InsertionSortImpl/InsertionSortAlgo.java +++ /dev/null @@ -1,24 +0,0 @@ -package SearchingAndSortingAlgorithms.InsertionSortImpl; - -public class InsertionSortAlgo { - public static void insertionSort(int[] array){ - for (int secondIndex = 1;secondIndex< array.length;secondIndex++){ - int leftIndex =secondIndex -1; - int temp = array[secondIndex]; - while (leftIndex >=0 && array[leftIndex]>temp){ - array[leftIndex+1]=array[leftIndex]; - leftIndex--; - } - array[leftIndex+1]= temp; - } - - } - public static void main(String[] args) { - - int array[] = {9, 5, 4, 2, 7, 8, 1, 3, 6, 10}; - insertionSort(array); - for (int i:array) { - System.out.print(i); - } - } -} diff --git a/src/SearchingAndSortingAlgorithms/LinearSearchImpl/LinearSearch1.java b/src/SearchingAndSortingAlgorithms/LinearSearchImpl/LinearSearch1.java deleted file mode 100644 index fcb85f6..0000000 --- a/src/SearchingAndSortingAlgorithms/LinearSearchImpl/LinearSearch1.java +++ /dev/null @@ -1,28 +0,0 @@ -package SearchingAndSortingAlgorithms.LinearSearchImpl; - - -public class LinearSearch1 { - - //FIRST IMPLEMENTATION EXAMPLE - public static int linearSearch(int[] Array, int key) { - - for (int i = 0; i <= Array.length; i++) { - if (Array[i] == key) { - return i; - } - } - return -1; - } - - public static void main(String[] args) { - - int Array[] = {1, 2, 3, 5, 6, 7, 8, 9, 10}; - int index = linearSearch(Array, 9); - if (index == -1) { - System.out.println("element not found" ); - } else { - System.out.println("element found at index :" + index); - } - - } -} diff --git a/src/SearchingAndSortingAlgorithms/LinearSearchImpl/LinearSearch2.java b/src/SearchingAndSortingAlgorithms/LinearSearchImpl/LinearSearch2.java deleted file mode 100644 index 9555efc..0000000 --- a/src/SearchingAndSortingAlgorithms/LinearSearchImpl/LinearSearch2.java +++ /dev/null @@ -1,27 +0,0 @@ -package SearchingAndSortingAlgorithms.LinearSearchImpl; - -import java.util.Scanner; - -public class LinearSearch2 { - - - //SECOND IMPLEMENTATION EXAMPLE - public static void main(String[] args) { - - int array[] = new int[]{2,4,6,3,1,5,8,7,9}; - Scanner input = new Scanner(System.in); - System.out.println("Enter the element you are searching"); - int key = input.nextInt(); - int count =0; - for (int i=0; i< array.length; i++){ - if (array[i] == key){ - System.out.println("element found at index "+ i); - count++; - } - } - if (count ==0){ - System.out.println("element not found"); - } - } -} - diff --git a/src/SearchingAndSortingAlgorithms/MergeSortImpl/MergeSort.java b/src/SearchingAndSortingAlgorithms/MergeSortImpl/MergeSort.java deleted file mode 100644 index fb10b6f..0000000 --- a/src/SearchingAndSortingAlgorithms/MergeSortImpl/MergeSort.java +++ /dev/null @@ -1,63 +0,0 @@ -package SearchingAndSortingAlgorithms.MergeSortImpl; - -public class MergeSort { - public static void main(String[] args) { - int array[] = {9, 5, 4, 2, 7, 8, 1, 3, 6, 10}; - - mergeSort(array); - - for (int i =0;iarray[j]){ - min=j; - } - } - int temp = array[i]; - array[i] = array[min]; - array[min]= temp; - } - } - -} diff --git a/src/SearchingAndSortingAlgorithms/binarySearchImpl/IterativeMethod.java b/src/SearchingAndSortingAlgorithms/binarySearchImpl/IterativeMethod.java deleted file mode 100644 index d9ca712..0000000 --- a/src/SearchingAndSortingAlgorithms/binarySearchImpl/IterativeMethod.java +++ /dev/null @@ -1,80 +0,0 @@ -package SearchingAndSortingAlgorithms.binarySearchImpl; - -//Binary Search is a searching algorithm for finding an element's position in a sorted array. -//Binary Search Algorithm can be implemented in two ways which are : -// -// 1) Iterative Method -// 2) Recursive Method (follows the divide and conquer approach) - - -//IT FOLLOWS THE DIVIDE AND CONQUER PARADIGM -// BEST CASE TIME COMPLEXITY IS 0(1) -// WORST CASE TIME COMPLEXITY IS 0(Log n) -// SPACE COMPLEXITY IS 0(1) -public class IterativeMethod { - public int binarySearch(int Array[],int key,int low,int high){ - while (high>=low){ - int mid = low + (high-low)/2; - if (Array[mid]==key){ - return mid; - }if (Array[mid] < key){ - low = mid+1; - }if (Array[mid]> key){ - high = mid-1; - } - } - return -1; - } - - public static void main(String[] args) { - IterativeMethod iterativeMethod = new IterativeMethod(); - int Array[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; - int index = iterativeMethod.binarySearch(Array,16,0, Array.length-1); - if (index == -1){ - System.out.println("element not found"); - }else { - System.out.println("element found at index "+ index); - } - } - -// public static int binarySearch(int array[],int key){ -// -// int low =0; -// int high = array.length -1; -// while (high>=low){ -// int mid = low +(high-low)/2; -// int value = array[mid]; -// -// if (valuekey){ -// high=mid-1; -// } -// else { -// return mid; //element found -// } -// } -// return -1; -// } -// public static void main(String[] args) { -// int array[] = new int[100]; -// int key = 50; -// for (int i =0;i < array.length;i++){ -// array[i]=i; -// } -// -// int index = binarySearch( array, key); -// -// if (index==-1){ -// System.out.println("element not found"); -// }else { -// System.out.println("element found at index :"+index); -// } -// -// } - - - - -} diff --git a/src/SearchingAndSortingAlgorithms/binarySearchImpl/RecursiveMethod.java b/src/SearchingAndSortingAlgorithms/binarySearchImpl/RecursiveMethod.java deleted file mode 100644 index e7e085b..0000000 --- a/src/SearchingAndSortingAlgorithms/binarySearchImpl/RecursiveMethod.java +++ /dev/null @@ -1,64 +0,0 @@ -package SearchingAndSortingAlgorithms.binarySearchImpl; - -//Binary Search is a searching algorithm for finding an element's position in a sorted array. -//Binary Search Algorithm can be implemented in two ways which are : -// -// 1) Iterative Method -// 2) Recursive Method (follows the divide and conquer approach) - - -//IT FOLLOWS THE DIVIDE AND CONQUER PARADIGM -// BEST CASE TIME COMPLEXITY IS 0(1) -// WORST CASE TIME COMPLEXITY IS 0(Log n) -// SPACE COMPLEXITY IS 0(1) - -public class RecursiveMethod { - - public int binarySearch(int Array[],int key,int low ,int high){ - - if (high>=low){ - int mid = low +(high-low)/2; - if (Array[mid]==key){ - return mid; - } if (Array[mid]< key){ - return binarySearch(Array,key,mid+1,high); - }if (Array[mid]>key){ - return binarySearch(Array,key,low,mid-1); - } - - }return -1; - } - public static void main(String[] args) { - RecursiveMethod recursiveMethod = new RecursiveMethod(); - int Array[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; - int index = recursiveMethod.binarySearch(Array,97,0, Array.length-1); - if (index==-1){ - System.out.println("element not found"); - }else { - System.out.println("element found at index :"+index); - } - } - -// public static int binarySearch(int[] array,int key,int first,int last){ -// int mid = (first+last)/2; -// if (first<=last){ -// if (key==array[mid]){ -// return mid;} -// if (keyarray[j+1]){ - int temp = array[j]; - array[j] = array[j+1]; - array[j+1] = temp; - } - } - } - } - - public static void main(String[] args) { - - int array[]= {0,9,8,6,5,4,3,2,1,7}; - bubbleSort(array); - for (int i :array){ - System.out.print(i); - } - } -} diff --git a/src/TreeBasedDataStructures/BinarySearchTree/BinarySearchTree.java b/src/TreeBasedDataStructures/BinarySearchTree/BinarySearchTree.java deleted file mode 100644 index 92f073d..0000000 --- a/src/TreeBasedDataStructures/BinarySearchTree/BinarySearchTree.java +++ /dev/null @@ -1,84 +0,0 @@ -package TreeBasedDataStructures.BinarySearchTree; - -//Binary Search Tree Complexities - -//Time Complexity - -//Operation BestCase AverageCase WorstCase -//Search O(log n) O(log n) O(n) -//Insertion O(log n) O(log n) O(n) -//Deletion O(log n) O(log n) O(n) - -//Space Complexity - -//The space complexity for all the operations is O(n). - -//Binary Search Tree Applications - -//1.In multilevel indexing in the database -//2.For dynamic sorting -//3.For managing virtual memory areas in Unix kernel - -// OPERATIONS OS A Binary Search Tree(BST) - -// 1) SEARCH OPERATION -// 2) INSERTION OPERATION -// 3) Deletion Operation -// IF(In the third case, the node to be deleted has two children. -// In such a case follow the steps below: -//*Get the inorder successor of that node. -//*Replace the node with the inorder successor. -//*Remove the inorder successor from its original position.) - -class Node { - char key; - Node left, right; - public Node(char key) { - this.key=key; - } -} - -class BinarySolution{ - Node root; - void insertKey(char key){ - root = insertInTree(root,key); - } - void preordertraversal(Node root){ - if (root !=null){ - System.out.println(root.key+" "); - preordertraversal(root.left); - preordertraversal(root.right); - } - } - Node insertInTree(Node root, char key) { - if (root==null){ - root= new Node(key); - return root; - } - if (key root.key) { - root.right = insertInTree(root.right,key); - - } - return root; - } -public class BinarySearchTree { - - public static void main(String[] args) { - - BinarySolution binarySolution = new BinarySolution(); - binarySolution.insertKey('C'); - binarySolution.insertKey('I'); - binarySolution.insertKey('B'); - binarySolution.insertKey('G'); - binarySolution.insertKey('M'); - binarySolution.insertKey('A'); - binarySolution.preordertraversal(binarySolution.root); - - } -}} - - - - diff --git a/src/TreeBasedDataStructures/BinaryTree/BinaryTree.java b/src/TreeBasedDataStructures/BinaryTree/BinaryTree.java deleted file mode 100644 index 110fe0b..0000000 --- a/src/TreeBasedDataStructures/BinaryTree/BinaryTree.java +++ /dev/null @@ -1,35 +0,0 @@ -package TreeBasedDataStructures.BinaryTree; - -//Binary Tree - -//A binary tree is a tree data structure in which each parent node can have at most two children. Each node of a binary tree consists of three items: -// -//data item -// -//address of left child -// -//address of right child - -//Binary Tree Applications - -//For easy and quick access to data -//In router algorithms -//To implement heap data structure -//Syntax tree - -public class BinaryTree { - public static void main(String[] args) { - TreeTraversal treeTraversal = new TreeTraversal(); - treeTraversal.root = new Node('A'); - treeTraversal.root.left = new Node('B'); - treeTraversal.root.right = new Node('C'); - treeTraversal.root.left.left = new Node('D'); - treeTraversal.root.left.right = new Node('E'); - treeTraversal.preordertraversal(treeTraversal.root ); - System.out.println(); - treeTraversal.postordertraversal( treeTraversal.root ); - System.out.println(); - treeTraversal.inordertraversal(treeTraversal.root ); - } -} - diff --git a/src/TreeBasedDataStructures/BinaryTree/Node.java b/src/TreeBasedDataStructures/BinaryTree/Node.java deleted file mode 100644 index 4d84837..0000000 --- a/src/TreeBasedDataStructures/BinaryTree/Node.java +++ /dev/null @@ -1,8 +0,0 @@ -package TreeBasedDataStructures.BinaryTree; -public class Node { - char key; - Node right,left; - Node(char key){ - this.key=key; - } -} diff --git a/src/TreeBasedDataStructures/BinaryTree/TreeTraversal.java b/src/TreeBasedDataStructures/BinaryTree/TreeTraversal.java deleted file mode 100644 index a2d6b04..0000000 --- a/src/TreeBasedDataStructures/BinaryTree/TreeTraversal.java +++ /dev/null @@ -1,45 +0,0 @@ -package TreeBasedDataStructures.BinaryTree; - -class TreeTraversal { - Node root; - - // PreOrder Traversal - // YOU FIRST TRAVERSE THE ROOT NODE - // THEN LEFT NODE - // AND LASTLY THE RIGHT NODE - void preordertraversal(Node n) { - if (n != null) { - System.out.print(n.key + " "); - preordertraversal(n.left); - preordertraversal(n.right); - - } - } - - // PostOrder Traversal - // YOU FIRST TRAVERSE THE LEFT NODE - // THEN RIGHT NODE - // AND LASTLY THE ROOT NODE - void postordertraversal(Node n) { - if (n != null) { - preordertraversal(n.left); - preordertraversal(n.right); - System.out.print(n.key + " "); - - } - } - - // InOrder Traversal - // YOU FIRST TRAVERSE THE LEFT NODE - // THEN ROOT NODE - // AND LASTLY THE RIGHT NODE - void inordertraversal(Node n) { - if (n != null) { - preordertraversal(n.left); - System.out.print(n.key + " "); - preordertraversal(n.right); - - - } - } -} diff --git a/src/TreeBasedDataStructures/FullBinaryTree/FullBinaryTree.java b/src/TreeBasedDataStructures/FullBinaryTree/FullBinaryTree.java deleted file mode 100644 index d5b9391..0000000 --- a/src/TreeBasedDataStructures/FullBinaryTree/FullBinaryTree.java +++ /dev/null @@ -1,37 +0,0 @@ -package TreeBasedDataStructures.FullBinaryTree; - -// Full Binary Tree -// -// A full Binary tree is a special type of binary tree in which every parent node/internal -// node has either two or no children. - -public class FullBinaryTree { - - Node root; - - boolean isFullBinaryTree(Node node){ - if (node == null){return true;} - if (node.left == null && node.right == null){return true;} - if ((node.left !=null)&& (node.right !=null)){ - return (isFullBinaryTree(node.left)&& isFullBinaryTree(node.right)); - - } - return false; - } - public static void main(String args[]) { - - FullBinaryTree tree = new FullBinaryTree(); - tree.root = new Node(1); - tree.root.left = new Node(2); - tree.root.right = new Node(3); - tree.root.left.left = new Node(4); - tree.root.left.right = new Node(5); - tree.root.right.left = new Node(6); - tree.root.right.right = new Node(7); - - if (tree.isFullBinaryTree(tree.root)) - System.out.print("The tree is a full binary tree"); - else - System.out.print("The tree is not a full binary tree"); - } - } diff --git a/src/TreeBasedDataStructures/FullBinaryTree/Node.java b/src/TreeBasedDataStructures/FullBinaryTree/Node.java deleted file mode 100644 index 7157c81..0000000 --- a/src/TreeBasedDataStructures/FullBinaryTree/Node.java +++ /dev/null @@ -1,10 +0,0 @@ -package TreeBasedDataStructures.FullBinaryTree; - -public class Node { - int data; - Node left,right; - Node(int item){ - data = item; - left = right = null; - } -} diff --git a/src/TreeBasedDataStructures/PerfectBinaryTree/Node.java b/src/TreeBasedDataStructures/PerfectBinaryTree/Node.java deleted file mode 100644 index 7154096..0000000 --- a/src/TreeBasedDataStructures/PerfectBinaryTree/Node.java +++ /dev/null @@ -1,5 +0,0 @@ -package TreeBasedDataStructures.PerfectBinaryTree; - -public class Node { - -} diff --git a/src/TreeBasedDataStructures/PerfectBinaryTree/PerfectBinaryTree.java b/src/TreeBasedDataStructures/PerfectBinaryTree/PerfectBinaryTree.java deleted file mode 100644 index 88a2de0..0000000 --- a/src/TreeBasedDataStructures/PerfectBinaryTree/PerfectBinaryTree.java +++ /dev/null @@ -1,62 +0,0 @@ -package TreeBasedDataStructures.PerfectBinaryTree; - -//Perfect Binary Tree - -//A perfect binary tree is a type of binary tree in which every internal node has exactly two child nodes and all the leaf nodes are at the same level. -public class PerfectBinaryTree { - - static class Node{ - int key; - Node left,right; - } - - - //calculate the depth - static int depth(Node node){ - int d =0; - while (node !=null){ - d++; - node = node.left; - } - return d; - } - - static boolean isPerfect(Node root,int d,int level){ - - //checking if the tree is empty - if (root == null) {return true;} - //checking for children - if (root.left == null && root.right == null){return (d ==level+1);} - if (root.left ==null|| root.right==null){return false;} - return isPerfect(root.left,d,level+1)&&isPerfect(root.right,d,level+1); - } - - //creat a new node - static Node newNode(int k){ - Node node = new Node(); - node.key=k; - node.right = null; - node.left = null; - return node; - - } - static boolean isPerfect(Node root) { - int d = depth(root); - return isPerfect(root, d, 0); - } - - public static void main(String args[]) { - Node root = null; - root = newNode(1); - root.left = newNode(2); - root.right = newNode(3); - root.left.left = newNode(4); - root.left.right = newNode(5); - - if (isPerfect(root) == true) - System.out.println("The tree is a perfect binary tree"); - else - System.out.println("The tree is not a perfect binary tree"); - } -} - diff --git a/src/TreeBasedDataStructures/TreeTerminologies.txt b/src/TreeBasedDataStructures/TreeTerminologies.txt deleted file mode 100644 index b56aaef..0000000 --- a/src/TreeBasedDataStructures/TreeTerminologies.txt +++ /dev/null @@ -1,61 +0,0 @@ -Tree Data Structure - -A tree is a nonlinear hierarchical data structure that consists of nodes connected by edges. - -Tree Terminologies - -Node -A node is an entity that contains a key or value and pointers to its child nodes. - -The last nodes of each path are called leaf nodes or external nodes that do not contain a link/pointer to child nodes. - -The node having at least a child node is called an internal node. - -Edge -It is the link between any two nodes. - -Root -It is the topmost node of a tree. - -Height of a Node -The height of a node is the number of edges from the node to the deepest leaf ( the longest path from the node to a leaf node). - -Degree of a Node -The degree of a node is the total number of branches of that node. - -Tree Applications -Binary Search Trees(BSTs) are used to quickly check whether an element is present in a set or not. -Heap is a kind of tree that is used for heap sort. -A modified version of a tree called Tries is used in modern routers to store routing information. -Most popular databases use B-Trees and T-Trees, which are variants of the tree structure we learned above to store their data -Compilers use a syntax tree to validate the syntax of every program you write. - -Types of Binary Tree - -1. Full Binary Tree -A full Binary tree is a special type of binary tree in which every parent node/internal - node has either two or no children. - -2. Perfect Binary Tree -A perfect binary tree is a type of binary tree in which every internal node has exactly two child nodes and all the - leaf nodes are at the same level. - - 3. Complete Binary Tree - A complete binary tree is just like a full binary tree, but with two major differences - - Every level must be completely filled - All the leaf elements must lean towards the left. - The last leaf element might not have a right sibling i.e. a complete binary - tree doesn't have to be a full binary tree. - - 4. Degenerate or Pathological Tree - A degenerate or pathological tree is the tree having a single child either left or right. - - 5. Skewed Binary Tree - A skewed binary tree is a pathological/degenerate tree in which the tree is either dominated - by the left nodes or the right nodes. Thus, there are two types of skewed binary tree: left-skewed - binary tree and right-skewed binary tree. - - 6. Balanced Binary Tree - It is a type of binary tree in which the difference between the height of the left and the right - subtree for each node is either 0 or 1.