From 8ca052d2c72b0dc8c1d6268a8c5e9cace993936d Mon Sep 17 00:00:00 2001 From: Ming Tsai Date: Sat, 17 Apr 2021 21:45:05 -0400 Subject: [PATCH] fix: files naming and remove unncessary index --- algorithms/Java/README.md | 12 +- ...versions.java => counting-inversions.java} | 0 ..._Algorithm.java => kadanes-algorithm.java} | 0 ...{left_rotation.java => left-rotation.java} | 0 ...rnd-pointer.java => clone-linkedlist.java} | 0 ...versal.java => pre-in-post-traversal.java} | 0 arrays/README.md | 7 - arrays/java/Kadanes_Algorithm.java | 30 ---- arrays/java/count-inversions.java | 59 -------- arrays/java/left_rotation.java | 51 ------- .../java/unique-digits-of-large-number.java | 62 -------- graphs/README.md | 3 - graphs/java/Dijkstras.java | 133 ------------------ linked-lists/README.md | 8 -- queues/README.md | 5 - queues/java/circular-queue-linked-list.java | 96 ------------- queues/java/queue-linked-list.java | 78 ---------- scheduling/README.md | 4 - searching/README.md | 6 - searching/java/binary-search.java | 47 ------- searching/java/jump-search.java | 55 -------- searching/java/linear-search.java | 21 --- sorting/README.md | 14 -- sorting/java/bubble-sort.java | 27 ---- sorting/java/counting-sort.java | 74 ---------- sorting/java/heap-sort.java | 69 --------- sorting/java/insertion-sort.java | 29 ---- sorting/java/merge-sort.java | 79 ----------- sorting/java/quick-sort.java | 81 ----------- sorting/java/selection-sort.java | 50 ------- stacks/README.md | 6 - stacks/java/balanced-paranthesis.java | 69 --------- stacks/java/stack.java | 93 ------------ stacks/java/the-stock-span-problem.java | 98 ------------- strings/README.md | 8 -- strings/java/SplitString.java | 35 ----- strings/java/kmp.java | 82 ----------- strings/java/palindrome.java | 56 -------- strings/java/rabin-karp.java | 78 ---------- strings/java/sequence.java | 28 ---- strings/java/tokenizer.java | 96 ------------- trees/README.md | 4 - trees/java/pre_in_post_traversal.java | 76 ---------- 43 files changed, 6 insertions(+), 1823 deletions(-) rename algorithms/Java/arrays/{count-inversions.java => counting-inversions.java} (100%) rename algorithms/Java/arrays/{Kadanes_Algorithm.java => kadanes-algorithm.java} (100%) rename algorithms/Java/arrays/{left_rotation.java => left-rotation.java} (100%) rename algorithms/Java/linked-lists/{clone-linkedlist-with-rnd-pointer.java => clone-linkedlist.java} (100%) rename algorithms/Java/trees/{pre_in_post_traversal.java => pre-in-post-traversal.java} (100%) delete mode 100644 arrays/java/Kadanes_Algorithm.java delete mode 100644 arrays/java/count-inversions.java delete mode 100644 arrays/java/left_rotation.java delete mode 100644 arrays/java/unique-digits-of-large-number.java delete mode 100644 graphs/java/Dijkstras.java delete mode 100644 queues/java/circular-queue-linked-list.java delete mode 100644 queues/java/queue-linked-list.java delete mode 100644 searching/java/binary-search.java delete mode 100644 searching/java/jump-search.java delete mode 100644 searching/java/linear-search.java delete mode 100644 sorting/java/bubble-sort.java delete mode 100644 sorting/java/counting-sort.java delete mode 100644 sorting/java/heap-sort.java delete mode 100644 sorting/java/insertion-sort.java delete mode 100644 sorting/java/merge-sort.java delete mode 100644 sorting/java/quick-sort.java delete mode 100644 sorting/java/selection-sort.java delete mode 100644 stacks/java/balanced-paranthesis.java delete mode 100644 stacks/java/stack.java delete mode 100644 stacks/java/the-stock-span-problem.java delete mode 100644 strings/java/SplitString.java delete mode 100644 strings/java/kmp.java delete mode 100644 strings/java/palindrome.java delete mode 100644 strings/java/rabin-karp.java delete mode 100644 strings/java/sequence.java delete mode 100644 strings/java/tokenizer.java delete mode 100644 trees/java/pre_in_post_traversal.java diff --git a/algorithms/Java/README.md b/algorithms/Java/README.md index a5f3ea7a..53349c99 100644 --- a/algorithms/Java/README.md +++ b/algorithms/Java/README.md @@ -1,17 +1,17 @@ # Python ## Arrays -1. [Count Inversions](arrays/count-inversions.java) -2. [Kadanes Algorithm](arrays/Kadanes_Algorithm.java) -3. [Left Rotation](arrays/left_rotation.java) +1. [Counting Inversions](arrays/counting-inversions.java) +2. [Kadanes Algorithm](arrays/kadanes-algorithm.java) +3. [Left Rotation](arrays/left-rotation.java) 4. [Unique Digits of Large Number](arrays/unique-digits-of-large-number.java) ## Graphs -1. [Dijkstras](graphs/Dijkstras.java) +1. [Dijkstras](graphs/dijkstras.java) ## Linked Lists 1. [Circular](linked-lists/circular.java) -2. [Clone Linked List](linked-lists/clone-linkedlist-with-rnd-pointer.java) +2. [Clone Linked List](linked-lists/clone-linkedlist.java) 3. [Doubly](linked-lists/doubly.java) 4. [Reverse](linked-lists/reverse.java) 5. [Singly](linked-lists/singly.java) @@ -52,4 +52,4 @@ 6. [Tokenizer](strings/tokenizer.java) ## Trees -1. [Pre in Post Traversal](trees/pre_in_post_traversal.java) \ No newline at end of file +1. [Pre in Post Traversal](trees/pre-in-post-traversal.java) \ No newline at end of file diff --git a/algorithms/Java/arrays/count-inversions.java b/algorithms/Java/arrays/counting-inversions.java similarity index 100% rename from algorithms/Java/arrays/count-inversions.java rename to algorithms/Java/arrays/counting-inversions.java diff --git a/algorithms/Java/arrays/Kadanes_Algorithm.java b/algorithms/Java/arrays/kadanes-algorithm.java similarity index 100% rename from algorithms/Java/arrays/Kadanes_Algorithm.java rename to algorithms/Java/arrays/kadanes-algorithm.java diff --git a/algorithms/Java/arrays/left_rotation.java b/algorithms/Java/arrays/left-rotation.java similarity index 100% rename from algorithms/Java/arrays/left_rotation.java rename to algorithms/Java/arrays/left-rotation.java diff --git a/algorithms/Java/linked-lists/clone-linkedlist-with-rnd-pointer.java b/algorithms/Java/linked-lists/clone-linkedlist.java similarity index 100% rename from algorithms/Java/linked-lists/clone-linkedlist-with-rnd-pointer.java rename to algorithms/Java/linked-lists/clone-linkedlist.java diff --git a/algorithms/Java/trees/pre_in_post_traversal.java b/algorithms/Java/trees/pre-in-post-traversal.java similarity index 100% rename from algorithms/Java/trees/pre_in_post_traversal.java rename to algorithms/Java/trees/pre-in-post-traversal.java diff --git a/arrays/README.md b/arrays/README.md index 714eb5fa..a2060995 100644 --- a/arrays/README.md +++ b/arrays/README.md @@ -16,10 +16,3 @@ 10. [Variable declaration](c-or-cpp/variable-declaration.cpp) 11. [Data before and after sorting](c-or-cpp/data-before-sort.cpp) 12. [Even and odd no. ](c-or-cpp/even-and-odd.c) - -### Java - -1. [Counting Inversions](java/count-inversions.java) -2. [Kadane's Algorithm](java/Kadanes_Algorithm.java) -2. [Left Rotation of Array](java/left_rotation.java) -3. [Finding unique digits of large number](java/unique-digits-of-large-number.java) diff --git a/arrays/java/Kadanes_Algorithm.java b/arrays/java/Kadanes_Algorithm.java deleted file mode 100644 index 01021467..00000000 --- a/arrays/java/Kadanes_Algorithm.java +++ /dev/null @@ -1,30 +0,0 @@ -// Java program to print largest contiguous array sum - -import java.io.*; -import java.util.*; - -class Kadane -{ - public static void main (String[] args) - { - int [] a = {-2, -3, 4, -1, -2, 1, 5, -3}; - System.out.println("Maximum contiguous sum is " + - maxSubArraySum(a)); - } - - static int maxSubArraySum(int a[]) - { - int size = a.length; - int max_so_far = Integer.MIN_VALUE, max_ending_here = 0; - - for (int i = 0; i < size; i++) - { - max_ending_here = max_ending_here + a[i]; - if (max_so_far < max_ending_here) - max_so_far = max_ending_here; - if (max_ending_here < 0) - max_ending_here = 0; - } - return max_so_far; - } -} diff --git a/arrays/java/count-inversions.java b/arrays/java/count-inversions.java deleted file mode 100644 index 472a7c3b..00000000 --- a/arrays/java/count-inversions.java +++ /dev/null @@ -1,59 +0,0 @@ -// Algorithm Type: Divide & Conquer -// Time Complexity: O(n*log(n)) - -import java.io.File; -import java.lang.reflect.Array; -import java.util.*; - -public class inversions { - private static long count_split_inv(int[] arr, int[] left, int[] right) { - long split_inv = 0; - int ridx = 0, lidx = 0; - int size = arr.length; - int rsize = right.length; - int lsize = left.length; - for (int i = 0; i < size; i++) { - if (lidx != lsize && ridx != rsize) { - if (right[ridx] <= left[lidx]) { - arr[i] = right[ridx]; - ridx++; - split_inv += lsize - lidx; - } else { - arr[i] = left[lidx]; - lidx++; - } - } else if (lidx == lsize) { - arr[i] = right[ridx]; - ridx++; - } else if (ridx == rsize) { - arr[i] = left[lidx]; - lidx++; - } - } - return split_inv; - } - - private static long count_inversions(int[] arr) { - int size = arr.length; - if (size == 1) { - return 0; - } - int[] left = Arrays.copyOfRange(arr, 0, size / 2); - int[] right = Arrays.copyOfRange(arr, size / 2, size); - long left_inv = count_inversions(left); - long right_inv = count_inversions(right); - long split_inv = count_split_inv(arr, left, right); - - return left_inv + right_inv + split_inv; - } - - public static void main(String[] args) { - try { - int[] arr = {8, 2, 1, 5, 7, 3, 9, 2, 0, 1}; - System.out.println(count_inversions(arr)); - } catch (Exception e) { - System.out.println("Err... "); - } - - } -} diff --git a/arrays/java/left_rotation.java b/arrays/java/left_rotation.java deleted file mode 100644 index aa99464e..00000000 --- a/arrays/java/left_rotation.java +++ /dev/null @@ -1,51 +0,0 @@ -public class left_rotation{ - - public static void rotateToLeft(int [] arr){ - if(arr == null || arr.length == 0){ - System.out.println("no rotation possible"); - return; - } - //take the first element - int firstElement = arr[0]; - //move everything in the left - for(int i = 0; i < arr.length -1; i++){ - arr[i] = arr[i + 1]; - } - //the first element become the last element - arr[arr.length - 1] = firstElement; - } - - public static void print(int [] arr){ - System.out.print("["); - for(int i = 0; i < arr.length; i++) - System.out.print(arr[i] + ", "); - System.out.println("]"); - } - - public static void main(String [] args){ - int [] arr = {1,2,3,4,5,6,7,8,9}; - int n = 3; //number of times to rotate the array - - System.out.print("before: "); - print(arr); - - System.out.println("rotating " + n + " times to left"); - for(int i = 0; i < n; i++) - rotateToLeft(arr); //move to left n times - - System.out.print("after: "); - print(arr); - } -} - -/* - to run the file: - javac left_rotation.java - java left_rotation - - result: - before: [1, 2, 3, 4, 5, 6, 7, 8, 9, ] - rotating 3 times to left - after: [4, 5, 6, 7, 8, 9, 1, 2, 3, ] - -*/ \ No newline at end of file diff --git a/arrays/java/unique-digits-of-large-number.java b/arrays/java/unique-digits-of-large-number.java deleted file mode 100644 index 190a77ea..00000000 --- a/arrays/java/unique-digits-of-large-number.java +++ /dev/null @@ -1,62 +0,0 @@ -import java.util.*; -public class UniqueDigitsOfLargeNumber { - - public static void main(String[] args) { - // TODO Auto-generated method stub - Scanner sc=new Scanner(System.in); - int l,i,j,l1=0; - String s,p=""; - char c,d; - System.out.println("Enter a large number"); - s=sc.nextLine(); - l=s.length(); - for(i=0;i1) - { - for(i=0;ic) - c=p.charAt(i); - } - s=""; - s=s+c; - c--; - for(d=c;d>='0';d--) - { - for(i=0;i -{ - @Override - public int compare(AdjListNode node1, AdjListNode node2) - { - if (node1.weight < node2.weight) - return -1; - if (node1.weight > node2.weight) - return 1; - return 0; - } -} - -class Dijkstras -{ - public int[] dijkstras(List> adj_list,int source) - { - int[] distance = new int[adj_list.size()]; - Set completed = new HashSet(); - for(int i=0;i pq = new PriorityQueue<>(adj_list.size(),new NodeComparator()); - AdjListNode source_node = new AdjListNode(source,0); - for(int i=0;i (distance[current.dest]+n.weight)) - { - distance[n.dest] = distance[current.dest]+n.weight; - pq.add(new AdjListNode(n.dest, distance[n.dest])); - } - } - } - } - return distance; - } - - public static void main(String args[]) - { - /* - SAMPLE INPUT AND OUTPUT - ___________________________ - - Input: - __________ - Please enter the number of nodes N. Vertices will be [0,N-1] - 6 - Please Enter the number of Edges - 9 - Please enter each edge in the sequence - 0 1 1 - 0 2 5 - 1 2 2 - 1 4 1 - 1 3 2 - 2 4 2 - 3 5 1 - 3 4 3 - 4 5 2 - Please enter source vertex - 0 - - Output: - ___________ - Distances from source 0 - Node0--->0 - Node1--->1 - Node2--->3 - Node3--->3 - Node4--->2 - Node5--->4 - */ - - List> adj_list = new ArrayList>(); - System.out.println("Please enter the number of nodes N. Vertices will be [0,N-1]"); - Scanner sc = new Scanner(System.in); - int v = sc.nextInt(); - for(int i=0;i()); - - System.out.println("Please enter the number of edges"); - int e = sc.nextInt(); - System.out.println("Please enter each edge in the sequence "); - // Sample Data: 0 2 5 (edge from 0 to 2 with weight 5) - for(int i=0;i"+"infinity"); - else - System.out.println("Node"+i+"--->"+distances[i]); - } - } -} diff --git a/linked-lists/README.md b/linked-lists/README.md index d29e0be5..cd0052ca 100644 --- a/linked-lists/README.md +++ b/linked-lists/README.md @@ -9,11 +9,3 @@ 5. [Insertion Linked List](c-or-cpp/all-possible-insertion.cpp) 6. [Josephus Problem Using Circular Linked List](c-or-cpp/josephus-problem.c) 7. [Merge Two Singly linked List](c-or-cpp/merge.cpp) - -### Java - -1. [Singly Linked List](java/singly.java) -2. [Doubly Linked List](java/doubly.java) -3. [Circular Linked List](java/circular.java) -4. [Reverse Linked List](java/reverse.java) -5. [Clone Linked List with Random Pointer](java/clone-linkedlist-with-rnd-pointer.java) diff --git a/queues/README.md b/queues/README.md index 0f6db19e..958080a3 100644 --- a/queues/README.md +++ b/queues/README.md @@ -5,8 +5,3 @@ 1. [Queue using linked list](c-or-cpp/queue-linked-list.cpp) 2. [Circular Queue using linked list](c-or-cpp/circular-queue-linked-list.cpp) 3. [Double Ended Queue (using arrays)](c-or-cpp/double-ended-queue-using-array.c) - -### Java - -1. [Queue using linked list](java/queue-linked-list.java) -2. [Circular Queue using linked list](java/circular-queue-linked-list.java) \ No newline at end of file diff --git a/queues/java/circular-queue-linked-list.java b/queues/java/circular-queue-linked-list.java deleted file mode 100644 index 0d182717..00000000 --- a/queues/java/circular-queue-linked-list.java +++ /dev/null @@ -1,96 +0,0 @@ -// Java program for insertion and -// deletion in Circular Queue -import java.util.*; - -class Solution { - - // Structure of a Node - static class Node { - int data; - Node link; - } - - static class Queue { - Node front, rear; - } - - // Function to create Circular queue - static void enQueue(Queue q, int value) - { - Node temp = new Node(); - temp.data = value; - if (q.front == null) - q.front = temp; - else - q.rear.link = temp; - - q.rear = temp; - q.rear.link = q.front; - } - - // Function to delete element from Circular Queue - static int deQueue(Queue q) - { - if (q.front == null) { - System.out.printf("Queue is empty"); - return Integer.MIN_VALUE; - } - - // If this is the last node to be deleted - int value; // Value to be dequeued - if (q.front == q.rear) { - value = q.front.data; - q.front = null; - q.rear = null; - } - else // There are more than one nodes - { - Node temp = q.front; - value = temp.data; - q.front = q.front.link; - q.rear.link = q.front; - } - - return value; - } - - // Function displaying the elements of Circular Queue - static void displayQueue(Queue q) - { - Node temp = q.front; - System.out.printf("\nElements in Circular Queue are: "); - while (temp.link != q.front) { - System.out.printf("%d ", temp.data); - temp = temp.link; - } - System.out.printf("%d", temp.data); - } - - /* Driver of the program */ - public static void main(String args[]) - { - // Create a queue and initialize front and rear - Queue q = new Queue(); - q.front = q.rear = null; - - // Inserting elements in Circular Queue - enQueue(q, 14); - enQueue(q, 22); - enQueue(q, 6); - - // Display elements present in Circular Queue - displayQueue(q); - - // Deleting elements from Circular Queue - System.out.printf("\nDeleted value = %d", deQueue(q)); - System.out.printf("\nDeleted value = %d", deQueue(q)); - - // Remaining elements in Circular Queue - displayQueue(q); - - enQueue(q, 9); - enQueue(q, 20); - displayQueue(q); - } -} - \ No newline at end of file diff --git a/queues/java/queue-linked-list.java b/queues/java/queue-linked-list.java deleted file mode 100644 index 560eb22c..00000000 --- a/queues/java/queue-linked-list.java +++ /dev/null @@ -1,78 +0,0 @@ -// Java program for linked-list implementation of queue - -// A linked list (LL) node to store a queue entry -class QNode { - int key; - QNode next; - - // constructor to create a new linked list node - public QNode(int key) - { - this.key = key; - this.next = null; - } -} - -// A class to represent a queue -// The queue, front stores the front node of LL and rear stores the -// last node of LL -class Queue { - QNode front, rear; - - public Queue() - { - this.front = this.rear = null; - } - - // Method to add an key to the queue. - void enqueue(int key) - { - - // Create a new LL node - QNode temp = new QNode(key); - - // If queue is empty, then new node is front and rear both - if (this.rear == null) { - this.front = this.rear = temp; - return; - } - - // Add the new node at the end of queue and change rear - this.rear.next = temp; - this.rear = temp; - } - - // Method to remove an key from queue. - void dequeue() - { - // If queue is empty, return NULL. - if (this.front == null) - return; - - // Store previous front and move front one node ahead - QNode temp = this.front; - this.front = this.front.next; - - // If front becomes NULL, then change rear also as NULL - if (this.front == null) - this.rear = null; - } -} - -// Driver class -public class Test { - public static void main(String[] args) - { - Queue q = new Queue(); - q.enqueue(10); - q.enqueue(20); - q.dequeue(); - q.dequeue(); - q.enqueue(30); - q.enqueue(40); - q.enqueue(50); - q.dequeue(); - System.out.println("Queue Front : " + q.front.key); - System.out.println("Queue Rear : " + q.rear.key); - } -} \ No newline at end of file diff --git a/scheduling/README.md b/scheduling/README.md index b9e9d715..2ad227ad 100644 --- a/scheduling/README.md +++ b/scheduling/README.md @@ -7,7 +7,3 @@ ### Golang 1. [Interval Scheduling](go/interval-scheduling.go) -### Java - -1. [Multi-Level Queue Scheduling](java/multi-level-queue-scheduling.java) -2. [Round Robin Scheduling](java/round-robin.java) diff --git a/searching/README.md b/searching/README.md index 70c1f678..7490bf4c 100644 --- a/searching/README.md +++ b/searching/README.md @@ -8,12 +8,6 @@ 4. [finding squareroot using binary search](c-or-cpp/sqrt-monotonic-binary-search.cpp) 5. [Interpolation Search](c-or-cpp/interpolation-search.cpp) -### Java - -1. [Linear Search](java/linear-search.java) -2. [Binary Search](java/binary-search.java) -3. [Jump Search](java/jump-search.java) - ### Golang 1. [Linear Search](go/linear-search.go) diff --git a/searching/java/binary-search.java b/searching/java/binary-search.java deleted file mode 100644 index 519448af..00000000 --- a/searching/java/binary-search.java +++ /dev/null @@ -1,47 +0,0 @@ -// Java implementation of recursive Binary Search -class BinarySearch { - // Returns index of x if it is present in arr[l..r], - // else return -1 - int binarySearch(int arr[], int l, int r, int x) - { - if (r >= l) { - int mid = l + (r - l) / 2; - - // If the element is present at the - // middle itself - if (arr[mid] == x) - return mid; - - // If element is smaller than mid, then - // it can only be present in left subarray - if (arr[mid] > x) - return binarySearch(arr, l, mid - 1, x); - - // Else the element can only be present - // in right subarray - return binarySearch(arr, mid + 1, r, x); - } - - // We reach here when element is not present - // in array - return -1; - } - - // Driver method to test above - public static void main(String args[]) - { - BinarySearch ob = new BinarySearch(); - int arr[] = { 2, 3, 4, 10, 40 }; - int n = arr.length; - int x = 10; - int result = ob.binarySearch(arr, 0, n - 1, x); - if (result == -1) - System.out.println("Element not present"); - else - System.out.println("Element found at index " + result); - } -} - -// For running in terminal rename this file to BinarySearch.java -//then run the commands followed by -//It will generate and a BinarySearch.class file which is a file containing java bytecode that is executed by JVM. diff --git a/searching/java/jump-search.java b/searching/java/jump-search.java deleted file mode 100644 index d90cdd6b..00000000 --- a/searching/java/jump-search.java +++ /dev/null @@ -1,55 +0,0 @@ -// Java program to implement Jump Search. -public class JumpSearch -{ - public static int jumpSearch(int[] arr, int x) - { - int n = arr.length; - - // Finding block size to be jumped - int step = (int)Math.floor(Math.sqrt(n)); - - // Finding the block where element is - // present (if it is present) - int prev = 0; - while (arr[Math.min(step, n)-1] < x) - { - prev = step; - step += (int)Math.floor(Math.sqrt(n)); - if (prev >= n) - return -1; - } - - // Doing a linear search for x in block - // beginning with prev. - while (arr[prev] < x) - { - prev++; - - // If we reached next block or end of - // array, element is not present. - if (prev == Math.min(step, n)) - return -1; - } - - // If element is found - if (arr[prev] == x) - return prev; - - return -1; - } - - // Driver program to test function - public static void main(String [ ] args) - { - int arr[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, - 34, 55, 89, 144, 233, 377, 610}; - int x = 55; - - // Find the index of 'x' using Jump Search - int index = jumpSearch(arr, x); - - // Print the index where 'x' is located - System.out.println("\nNumber " + x + - " is at index " + index); - } -} \ No newline at end of file diff --git a/searching/java/linear-search.java b/searching/java/linear-search.java deleted file mode 100644 index 4c8e2d3b..00000000 --- a/searching/java/linear-search.java +++ /dev/null @@ -1,21 +0,0 @@ -//Linear search to check for a given key in an array - -public class MyLinearSearch{ -public static int linearSearch(int[] arr, int key){ - for(int i=0;i followed by -//It will generate and a MyLinearSearch.class file which is a file containing java bytecode that is executed by JVM. diff --git a/sorting/README.md b/sorting/README.md index 970a16b9..74853dfd 100644 --- a/sorting/README.md +++ b/sorting/README.md @@ -15,20 +15,6 @@ 11. [Comb Sort](c-or-cpp/comb-sort.cpp) 12. [3 Way Quick Sort](c-or-cpp/3way_quick_sort.cpp) -### Java - - -1. [Bubble Sort](java/bubble-sort.java) -2. [Insertion Sort](java/insertion-sort.java) -3. [Selection Sort](java/selection-sort.java) -4. [Merge Sort](java/merge-sort.java) -5. [Quick Sort](java/quick-sort.java) -6. [Counting Sort](java/counting-sort.java) -7. [Bubble Sort algo](java/bubble-sort.java) -8. [Heap Sort Algo](java/heap-sort.java) -9. [Insertion Sort algo](java/insertion-sort.java) -10. [Selection Sort algo](java/selection-sort.java) - ### Golang 1. [Insertion Sort](go/insertion-sort.go) 2. [Quick Sort](go/quick-sort.go) diff --git a/sorting/java/bubble-sort.java b/sorting/java/bubble-sort.java deleted file mode 100644 index 51ebadc9..00000000 --- a/sorting/java/bubble-sort.java +++ /dev/null @@ -1,27 +0,0 @@ - -import java.util.Arrays; - -public class BubbleSort { - static int temp = 0; - static int[] nums = { 13, 34, 12, 45, 56, 32, 20 }; - - public static void main(String[] args) { - - System.out.println("Before Sorting : "+Arrays.toString(nums)); - sort(); - - } - //create method sorting array - public static void sort() { - for (int i = 1; i < nums.length; i++) { - for (int j = 0; j < i; j++) { - if (nums[j] > nums[i]) { - temp = nums[j]; - nums[j] = nums[i]; - nums[i] = temp; - } - } - } - System.out.println(Arrays.toString(nums)); - } -} \ No newline at end of file diff --git a/sorting/java/counting-sort.java b/sorting/java/counting-sort.java deleted file mode 100644 index 36894d57..00000000 --- a/sorting/java/counting-sort.java +++ /dev/null @@ -1,74 +0,0 @@ -public class CountingSort { - - /** - * Public method to use the counting sort algorithm - * @param arr - the array in input - */ - public void sort(int[] arr){ - - int[] sortedArray = new int[arr.length + 1]; - - // Search max element - int max = arr[0]; - for(int i = 0; i < arr.length; i++){ - if(arr[i] > max){ - max = arr[i]; - } - } - - // Create counting array - int[] countArray = new int[max + 1]; - for(int i = 0; i < countArray.length; i++){ - countArray[i] = 0; - } - - // Count elements in array - for(int i = 0; i < arr.length; i++){ - countArray[arr[i]]++; - } - - - for(int i = 1; i < countArray.length; i++){ - countArray[i] += countArray[i - 1]; - } - - for (int i = arr.length - 1; i >= 0; i--) { - sortedArray[countArray[arr[i]] - 1] = arr[i]; - countArray[arr[i]]--; - } - - System.arraycopy(sortedArray, 0, arr, 0, arr.length); - } - - /** - * Main method for testing - * @param args - */ - public static void main(String[] args) throws Exception { - CountingSort count = new CountingSort(); - - int[] test = {1,9,8,7,6,5,4,3,2,12,546,23,123,5768,689,45,6342,0,76,856,34,412,12,32,353,46,568,456,234,3456,467,345345,345,345,}; - - count.sort(test); - - for(int i = 1; i < test.length; i++){ - if(test[i] < test[i - 1]){ - throw new Exception("[ERROR] Ops! Counting sort not work!"); - } - } - - printArray(test); - } - - /** - * Print an array - * @param arr - */ - public static void printArray(int[] arr){ - - System.out.println("Sorted array: "); - for(int i = 0; i < arr.length; i++){ - System.out.println(" " + i); - } - } -} diff --git a/sorting/java/heap-sort.java b/sorting/java/heap-sort.java deleted file mode 100644 index 63f52a19..00000000 --- a/sorting/java/heap-sort.java +++ /dev/null @@ -1,69 +0,0 @@ - -import java.util.Scanner; -public class HeapSort -{ - public void sort(int array[]) - { - - int n = array.length; - for (int i = n / 2 - 1; i >= 0; i--) - heapify(array, n, i); - for (int i = n-1; i >= 0; i--) - { - - int temp = array[0]; - array[0] = array[i]; - array[i] = temp; - - - heapify(array, i, 0); - } - } - - - void heapify(int array[], int n, int i) - { - int largest = i; - int l = 2*i + 1; - int r = 2*i + 2; - - if (l < n && array[l] > array[largest]) - largest = l; -if (r < n && array[r] > array[largest]) - largest = r; - - - if (largest != i) - { - int swap = array[i]; - array[i] = array[largest]; - array[largest] = swap; - - - heapify(array, n, largest); - } - } - public static void main(String args[]) - { - - System.out.println("enter the array size"); - Scanner inputobj = new Scanner(System.in); - int s = inputobj.nextInt(); - int array[] = new int[s]; - System.out.println("enter " + s + "elements of array"); - for(int k=0;k 0 && nums[j - 1] > temp) { - nums[j] = nums[j - 1]; - j--; - } - nums[j] = temp; - i++; - } - System.out.println("After Sorting " + Arrays.toString(nums)); - - } -} \ No newline at end of file diff --git a/sorting/java/merge-sort.java b/sorting/java/merge-sort.java deleted file mode 100644 index 3b8f6645..00000000 --- a/sorting/java/merge-sort.java +++ /dev/null @@ -1,79 +0,0 @@ -//Java program for implementation of Merge Sort -import java.util.*; - -class MergeSortApp -{ - private int[] arr; - public MergeSortApp() - { - arr = new int[]{12,54,222,54622,10,169,30}; //Array to sort - } - - //Function to display/Print Array - public void printArray() - { - for (int i=0;i0 && arr[--rightptr]>pivot); //Find smaller item - - if (leftptr>=rightptr) //if index numbers cross,partition is done - break; - else - swap(leftptr,rightptr); //swap elements - - } - swap(leftptr,right); //restore pivot element - return leftptr; //return pivot location - } - - //Recursive quicksort function - public void recQuickSort(int left,int right) - { - if (right-left <= 0) // already sorted if size is 1 - return; - else - { - int pivot = arr[right]; //choosing pivot as rightmost(end) element of Array - int partition = partitionFn(left,right,pivot); - recQuickSort(left,partition-1); //Sort left - recQuickSort(partition+1,right); //Sort right - - } - } - - //function called in main - public void quickSort() - { - recQuickSort(0,arr.length-1); - } -} - -class QuickSort -{ - //Driver code to test above - public static void main(String[] args) { - - QuickSortBack ob = new QuickSortBack(); - ob.displayArray(); //display array before sort - ob.quickSort(); //call quickSort - ob.displayArray(); - } -} diff --git a/sorting/java/selection-sort.java b/sorting/java/selection-sort.java deleted file mode 100644 index bfe4ba80..00000000 --- a/sorting/java/selection-sort.java +++ /dev/null @@ -1,50 +0,0 @@ -class Selection -{ - - int minIndex(int Array[] , int start, int end) - { - int minIndex = start; - - for (int i = start+1; i < end; i++) - { - if ( Array[i] < Array[minIndex] ) - { - minIndex = i; - } - } - return minIndex; - } - int[] sorting(int Array[],int length) - { - for (int i = 0; i < length-1; i++) - { - int minI = minIndex(Array, i, length); - int temp = Array[minI]; - Array[minI] = Array[i]; - Array[i] = temp; - } - return Array; - } -} - -/** - * SelectionSort - */ -public class SelectionSort { - - public static void main(String[] args) { - - int Array[] = {1,2,3,4,5,6,7,8,9}; - - Selection s1 = new Selection(); - long startTime = System.nanoTime(); - int sortedArray[] = s1.sorting(Array, 9); - long endTime = System.nanoTime(); - - for (int i = 0; i < 9; i++) { - System.out.println(sortedArray[i]); - } - System.out.println("Total Time in Neno Second: "+ (endTime-startTime)); - - } -} diff --git a/stacks/README.md b/stacks/README.md index 5c6d4d0b..34cd53b8 100644 --- a/stacks/README.md +++ b/stacks/README.md @@ -3,9 +3,3 @@ ### C or C++ 1. [Balanced Parenthesis](c-or-cpp/balanced-parenthesis.cpp) - -### Java - -1. [Stack Implementation](java/stack.java) -2. [Balanced Parenthesis](java/balanced-paranthesis.java) -3. [Stock Span Problem](java/the-stock-span-problem.java) diff --git a/stacks/java/balanced-paranthesis.java b/stacks/java/balanced-paranthesis.java deleted file mode 100644 index 386c9d70..00000000 --- a/stacks/java/balanced-paranthesis.java +++ /dev/null @@ -1,69 +0,0 @@ - - -import java.util.Iterator; -import java.util.Stack; -import java.util.Vector; - -class Balanced_Paranthesis { - - public void problem(String input_string){ - - Vector output = new Vector<>(); - - Stack sta = new Stack<>(); - - for(int i = 0;i < input_string.length();i++){ - for (int j = 0; j <= i; j++){ - String sub_s = input_string.substring(j,i+1); - char[] sub = sub_s.toCharArray(); - int no_of_left_paranthesis = 0; - int no_of_right_paranthesis = 0; - for (int k = 0;k < sub_s.length();k++){ - if(sub[k] == '('){ - no_of_left_paranthesis = no_of_left_paranthesis + 1; - sta.push('('); - } - if(sub[k] == ')'){ - no_of_right_paranthesis = no_of_right_paranthesis + 1; - sta.pop(); - } - - if (sub_s.length() % 2 == 0 && no_of_left_paranthesis == no_of_right_paranthesis && sta.isEmpty()){ - output.add(sub_s); - } - else{ - while (!sta.isEmpty()){ - sta.pop(); - } - } - } - } - - int length = 0; - if (output.size() == 0){ - System.out.println(length + " "); - } - else{ - int max_len = 0; - String long_Str = ""; - - for(int s = 0;s < output.size();s++){ - if (output.get(s).length() > max_len){ - max_len = output.get(s).length(); - long_Str = output.get(s); - } - } - System.out.println("Maximum length of string : " + max_len + "and the string is : " + long_Str); - } - } - } -} - -class Main { - - public static void main(String[] args){ - Balanced_Paranthesis bal = new Balanced_Paranthesis(); - bal.problem("((()))"); - } -} - diff --git a/stacks/java/stack.java b/stacks/java/stack.java deleted file mode 100644 index bf884e76..00000000 --- a/stacks/java/stack.java +++ /dev/null @@ -1,93 +0,0 @@ - - - class stack { - public int Max; - public int Top; - public int[] stack; - - public stack(int Max){ - this.Max = Max; - stack = new int[Max]; - Top = -1; - } - - /** - * if the stack is empty then always the top will be -1 - * so this should return a boolean if value of top < 1 then returns true which means - * stack is empty. - */ - public boolean isEmpty(){ - return (Top < 0); - } - - public int size(){ - return (Top+1); - } - - public void push(int x){ - if (size() >= Max){ - System.out.println("Stack Overflow"); - } - else{ - Top = Top + 1; - stack[Top] = x; - } - } - - /** - * pop function pops out the top element in the queue - * returns the element that is popped out of the queue - */ - public int pop(){ - if (size() == 0){ - System.out.println("Stack Empty Exception"); - } - else { - /** - * the top element in the stack will be popped out from the stack - */ - int popped_element = stack[Top]; - Top = Top - 1; - return popped_element; - } - return -1; - } - - public int top(){ - if(isEmpty()){ - System.out.println("Stack Empty Exception"); - } - else{ - return stack[Top]; - } - return -1; - } - - public void PrintStack(){ - if (isEmpty()){ - System.out.println("Empty"); - } - else{ - for(int i = 0;i < size();i++){ - System.out.print(stack[i] + " "); - } - System.out.println(); - } - } - -} -class Main { - - public static void main(String[] args){ - max = 1000; - stack object = new stack(max); - object.push(1); - object.push(2); - object.push(3); - object.pop(); - object.top(); - object.PrintStack(); - } -} - - diff --git a/stacks/java/the-stock-span-problem.java b/stacks/java/the-stock-span-problem.java deleted file mode 100644 index 7ee5eba0..00000000 --- a/stacks/java/the-stock-span-problem.java +++ /dev/null @@ -1,98 +0,0 @@ - - -import java.lang.reflect.Array; -import java.util.Arrays; -import java.util.Stack ; - -/** - * Problem statement : - * The stock span problem is a financial problem where we have a series of n daily price quotes for a stock - * and we need to calculate span of stock’s price for all n days. - * The span Si of the stock’s price on a given day i is defined as the maximum number of consecutive days just before the given day, - * for which the price of the stock on - * the current day is less than or equal to its price on the given day. - * For example, if an array of 7 days prices is given as {100, 80, 60, 70, 60, 75, 85}, then the span values for corresponding - * 7 days are {1, 1, 1, 2, 1, 4, 6} - */ -class The_Stock_Span_Problem extends stack{ - - public The_Stock_Span_Problem(int Max) { - super(Max); - } - - /** - * this problem is being solved with stack abstract data type. - */ - public void problem(int[] price,int n,int[] span){ - stack st = new stack(Max); - st.push(0); - - //span of the first day is always going to be 1 - span[0] = 1; - - - for (int i = 1;i < n;i ++){ - span[i] = 1; - st.PrintStack(); - while(!st.isEmpty() && price[st.top()] <= price[i]){ - st.pop(); - } - - if (st.isEmpty()){ - span[i] = i + 1; - } - else{ - span[i] = i - st.top(); - } - st.push(i); - } - printArray(span); - - } - - public void printArray(int[] span){ - System.out.println("The span of the stock array :" + Arrays.toString(span)); - } - - /** - * This is a different approach that has slightly higher time complexity than the approach used above - * Both the methods are awesome and star struck but...xD - */ - public void alternate_approach(int[] price,int n,int[] span){ - - Stack sta = new Stack<>(); - - span[0] = 1; - - for (int i = 1;i < n;i++){ - for (int j = 0;j < i;j++){ - sta.push(j); - } - - while(!sta.isEmpty() && price[sta.peek()] <= price[i]){ - sta.pop(); - } - if (sta.isEmpty()){ - span[i] = i + 1; - } - else{ - span[i] = i - sta.peek(); - } - sta.clear(); - } - printArray(span); - } -} - -class Main { - - //to test the span stock problem - public static void main(String[] args){ - int[] price = { 10, 4, 5, 90, 120, 80 }; - int n = price.length; - int[] span = new int[n]; - - The_Stock_Span_Problem prob = new The_Stock_Span_Problem(1000); - prob.alternate_approach(price,n,span); - } -} diff --git a/strings/README.md b/strings/README.md index b2f0a8b2..f4eee73e 100644 --- a/strings/README.md +++ b/strings/README.md @@ -11,14 +11,6 @@ 7. [Permutation of String](c-or-cpp/Permutation-of-String.c) 8. [Count Words](c-or-cpp/count-words.c) -### Java - -1. [Palindrome Check](java/palindrome.java) -2. [All subsequences](java/sequence.java) -3. [KMP String Searching](java/kmp.cpp) -4. [Rabin Karp String Searching](java/rabin-karp.cpp) -5. [String Tokenizer](java/tokenizer.java) - ### Rust 1. [Palindrome Check](rust/palindrome/README.md) diff --git a/strings/java/SplitString.java b/strings/java/SplitString.java deleted file mode 100644 index 24b67cf4..00000000 --- a/strings/java/SplitString.java +++ /dev/null @@ -1,35 +0,0 @@ -//Separate the strings such as in other group same letter does not repeat. -//Example abcdaef will give 5,1,1 from first 'a' to last 'a' one group and then remaining 2 letters from 1-1 group -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Scanner; - -public class SplitString { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("Enter the string"); - String s = sc.next(); - List a = new ArrayList(); - Map last = new HashMap(); - for (int i = 0; i < s.length(); i++) { - last.put(s.charAt(i), i); - } - int p = 0, ctr = 0; - for (int i = 0; i < s.length(); i++) { - if (last.get(s.charAt(i)) > p) - p = last.get(s.charAt(i)); - ++ctr; - if (i == p) { - a.add(ctr); - ctr = 0; - } - - } - for (Integer i : a) { - System.out.println(i); - } - sc.close(); - } -} \ No newline at end of file diff --git a/strings/java/kmp.java b/strings/java/kmp.java deleted file mode 100644 index d720a3f2..00000000 --- a/strings/java/kmp.java +++ /dev/null @@ -1,82 +0,0 @@ -class KMP_String_Matching { - void KMPSearch(String pat, String txt) - { - int M = pat.length(); - int N = txt.length(); - - // create lps[] that will hold the longest - // prefix suffix values for pattern - int lps[] = new int[M]; - int j = 0; // index for pat[] - - // Preprocess the pattern (calculate lps[] - // array) - computeLPSArray(pat, M, lps); - - int i = 0; // index for txt[] - while (i < N) { - if (pat.charAt(j) == txt.charAt(i)) { - j++; - i++; - } - if (j == M) { - System.out.println("Found pattern " - + "at index " + (i - j)); - j = lps[j - 1]; - } - - // mismatch after j matches - else if (i < N && pat.charAt(j) != txt.charAt(i)) { - // Do not match lps[0..lps[j-1]] characters, - // they will match anyway - if (j != 0) - j = lps[j - 1]; - else - i = i + 1; - } - } - } - - void computeLPSArray(String pat, int M, int lps[]) - { - // length of the previous longest prefix suffix - int len = 0; - int i = 1; - lps[0] = 0; // lps[0] is always 0 - - // the loop calculates lps[i] for i = 1 to M-1 - while (i < M) { - if (pat.charAt(i) == pat.charAt(len)) { - len++; - lps[i] = len; - i++; - } - else // (pat[i] != pat[len]) - { - // This is tricky. Consider the example. - // AAACAAAA and i = 7. The idea is similar - // to search step. - if (len != 0) { - len = lps[len - 1]; - - // Also, note that we do not increment - // i here - } - else // if (len == 0) - { - lps[i] = len; - i++; - } - } - } - } - - // Driver program to test above function - public static void main(String args[]) - { - String txt = "ABABDABACDABABCABAB"; - String pat = "ABABCABAB"; - new KMP_String_Matching().KMPSearch(pat, txt); - } -} -// This code has been contributed by Amit Khandelwal. diff --git a/strings/java/palindrome.java b/strings/java/palindrome.java deleted file mode 100644 index eafe646d..00000000 --- a/strings/java/palindrome.java +++ /dev/null @@ -1,56 +0,0 @@ -public class palindrome { - - // Function that returns true if - // str is a palindrome - static boolean isPalindrome(String str) - { - - // Pointers pointing to the beginning - // and the end of the string - int i = 0, j = str.length() - 1; - - // While there are characters toc compare - while (i < j) { - - // If there is a mismatch - if (str.charAt(i) != str.charAt(j)) - return false; - - // Increment first pointer and - // decrement the other - i++; - j--; - } - - // Given string is a palindrome - return true; - } - - //new approach with case sensitive - public static boolean isPalindrome(String str, boolean caseSensitive){ - // lowercase if not case sensitive - if(!caseSensitive) str = str.toLowerCase(); - - for(int i = 0; i < str.length() / 2; i++) - if(str.charAt(i) != str.charAt(str.length() - 1 - i)) - return false; - - return true; - } - - // Driver code - public static void main(String[] args) - { - String str = "Dad"; - - if (isPalindrome(str)) - System.out.println("Yes"); - else - System.out.println("No"); - - if (isPalindrome(str, false)) - System.out.println("Yes"); - else - System.out.println("No"); - } -} diff --git a/strings/java/rabin-karp.java b/strings/java/rabin-karp.java deleted file mode 100644 index 376fc0c6..00000000 --- a/strings/java/rabin-karp.java +++ /dev/null @@ -1,78 +0,0 @@ -public class Main -{ - // d is the number of characters in the input alphabet - public final static int d = 256; - - /* pat -> pattern - txt -> text - q -> A prime number - */ - static void search(String pat, String txt, int q) - { - int M = pat.length(); - int N = txt.length(); - int i, j; - int p = 0; // hash value for pattern - int t = 0; // hash value for txt - int h = 1; - - // The value of h would be "pow(d, M-1)%q" - for (i = 0; i < M-1; i++) - h = (h*d)%q; - - // Calculate the hash value of pattern and first - // window of text - for (i = 0; i < M; i++) - { - p = (d*p + pat.charAt(i))%q; - t = (d*t + txt.charAt(i))%q; - } - - // Slide the pattern over text one by one - for (i = 0; i <= N - M; i++) - { - - // Check the hash values of current window of text - // and pattern. If the hash values match then only - // check for characters on by one - if ( p == t ) - { - /* Check for characters one by one */ - for (j = 0; j < M; j++) - { - if (txt.charAt(i+j) != pat.charAt(j)) - break; - } - - // if p == t and pat[0...M-1] = txt[i, i+1, ...i+M-1] - if (j == M) - System.out.println("Pattern found at index " + i); - } - - // Calculate hash value for next window of text: Remove - // leading digit, add trailing digit - if ( i < N-M ) - { - t = (d*(t - txt.charAt(i)*h) + txt.charAt(i+M))%q; - - // We might get negative value of t, converting it - // to positive - if (t < 0) - t = (t + q); - } - } - } - - /* Driver Code */ - public static void main(String[] args) - { - String txt = "ABCAMCABAMMAM"; - String pat = "AM"; - - // A prime number - int q = 101; - - // Function Call - search(pat, txt, q); - } -} \ No newline at end of file diff --git a/strings/java/sequence.java b/strings/java/sequence.java deleted file mode 100644 index 65df0c5c..00000000 --- a/strings/java/sequence.java +++ /dev/null @@ -1,28 +0,0 @@ -import java.util.*; -class sequence { - - - static List al = new ArrayList<>(); - - - public static void main(String[] args) - { - String s = "abc"; - findsubsequences(s, ""); - System.out.println(al); - } - - private static void findsubsequences(String s, - String ans) - { - if (s.length() == 0) { - al.add(ans); - return; - } - - - findsubsequences(s.substring(1), ans + s.charAt(0)); - - findsubsequences(s.substring(1), ans); - } -} \ No newline at end of file diff --git a/strings/java/tokenizer.java b/strings/java/tokenizer.java deleted file mode 100644 index ce6c4dc6..00000000 --- a/strings/java/tokenizer.java +++ /dev/null @@ -1,96 +0,0 @@ -import java.util.ArrayList; -import java.util.List; - -public class tokenizer{ - - public static boolean charIsDelimiter(char c, char [] delimiters){ - //verrify if a character is a delimiter - for(char d: delimiters) - if(c == d) return true; - - return false; - } - - public static List tokenize(String str, char... delimiters){ - //by default the delimiter is white space ' ' - if(delimiters.length <= 0) delimiters = new char [] {' '}; - - List tokens = new ArrayList(); - String token = ""; - - for(int i = 0; i < str.length(); i++) { - char pos = str.charAt(i); - - if(!charIsDelimiter(pos, delimiters)) { - //if the character is not a delimiter add it into the current token - token += pos; - }else { - //avoid an empty token before adding to the list - if(!token.equals("")) - tokens.add(token); - - token = ""; - } - } - - //add the last token to the list - tokens.add(token); - - return tokens; - } - - public static void printTokens(List tokens){ - if(tokens == null) return; - - System.out.print("[ "); - for(String token : tokens){ - System.out.print("'" + token + "', "); - } - System.out.println("]"); - } - - public static void main(String [] args){ - String myString = "Hello I like pasta & pizza--hut"; - - System.out.println("myString = '" + myString + "'"); - - System.out.print("\ntokenize(myString) = "); - printTokens(tokenize(myString)); - - System.out.print("\ntokenize(myString, ' ', 'z') = "); - printTokens(tokenize(myString, ' ', 'z')); - - System.out.print("\ntokenize(myString, 'p','l', 'u') = "); - printTokens(tokenize(myString, 'p','l', 'u')); - - System.out.print("\ntokenize(myString, ' ', '&', '-') = "); - printTokens(tokenize(myString, ' ', '&', '-')); - } -} - -/* - to call the function: - tokenize(str, delimiters) - str is a text - delimiters is a list of char which is by default white space : ' ' - example: - tokenize("hello world") = tokenize("hello world", ' ') = [ 'hello', 'world' ] - tokenize("hello world", ' ', 'l') = [ 'he', 'o', 'wor', 'd' ] - - - to run this file: - javac tokenizer.java - java tokenizer - - result: - myString = 'Hello I like pasta & pizza--hut' - - tokenize(myString) = [ 'Hello', 'I', 'like', 'pasta', '&', 'pizza--hut', ] - - tokenize(myString, ' ', 'z') = [ 'Hello', 'I', 'like', 'pasta', '&', 'pi', 'a--hut', ] - - tokenize(myString, 'p','l', 'u') = [ 'He', 'o I ', 'ike ', 'asta & ', 'izza--h', 't', ] - - tokenize(myString, ' ', '&', '-') = [ 'Hello', 'I', 'like', 'pasta', 'pizza', 'hut', ] - -*/ diff --git a/trees/README.md b/trees/README.md index e64b6268..b5bf728e 100644 --- a/trees/README.md +++ b/trees/README.md @@ -7,7 +7,3 @@ 3. [Build Binary Tree using Pre,In & Post Order](c-or-cpp/build-binary-tree.cpp) 4. [Count and find the Sum of nodes in a Binary Tree](c-or-cpp/count-and-sum-of-nodes-in-binary-tree.cpp) -### Java - -1. [Pre, In & Post Order Traversals](java/pre_in_post_traversal.java) - diff --git a/trees/java/pre_in_post_traversal.java b/trees/java/pre_in_post_traversal.java deleted file mode 100644 index 8997e3e3..00000000 --- a/trees/java/pre_in_post_traversal.java +++ /dev/null @@ -1,76 +0,0 @@ -class Node{ - int value; - Node left; - Node right; - - public Node(int value) { - this.value = value; - } -} - -public class pre_in_post_traversal{ - - public static void preOrder(Node node){ - if (node == null) return; - - System.out.print(node.value + ", "); - preOrder(node.left); - preOrder(node.right); - } - - public static void inOrder(Node node){ - if(node == null) return; - - inOrder(node.left); - System.out.print(node.value + ", "); - inOrder(node.right); - } - - public static void postOrder(Node node){ - if(node == null) return; - postOrder(node.left); - postOrder(node.right); - System.out.print(node.value + ", "); - } - - - public static void main(String [] args){ - Node root = new Node(4); - root.left = new Node(2); - root.right = new Node(7); - root.left.right = new Node(6); - root.left.left = new Node(1); - root.right.right = new Node(9); - - System.out.print("pre order: "); - preOrder(root); - - System.out.println("\n"); - System.out.print("in order: "); - inOrder(root); - - System.out.print("\n\npost order: "); - postOrder(root); - } -} - -/* -tree structure: - 4 - / \ - 2 7 - / \ \ -1 6 9 - -to run the file: - javac .\pre_in_post_traversal.java - java pre_in_post_traversal - -results: - pre order: 4, 2, 1, 6, 7, 9, - - in order: 1, 2, 6, 4, 7, 9, - - post order: 1, 6, 2, 9, 7, 4, - -*/