In this commit I have:
Added JavaDoc to every package except for "heaps"
This commit is contained in:
parent
94871b7e6a
commit
9411d5be56
7
.classpath
Normal file
7
.classpath
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry excluding="data_structures/" kind="src" path=""/>
|
||||||
|
<classpathentry kind="src" path="data_structures"/>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||||
|
<classpathentry kind="output" path="bin"/>
|
||||||
|
</classpath>
|
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/bin/
|
17
.project
Normal file
17
.project
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>Java</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
74
BinarySearch.java
Normal file
74
BinarySearch.java
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements a Binary Search in Java
|
||||||
|
*
|
||||||
|
* @author unknown
|
||||||
|
*/
|
||||||
|
class BinarySearch
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* This method implements the Binary Search
|
||||||
|
*
|
||||||
|
* @param array The array to make the binary search
|
||||||
|
* @param key The number you are looking for
|
||||||
|
* @param lb The lower bound
|
||||||
|
* @param up The upper bound
|
||||||
|
* @return the location of the key
|
||||||
|
**/
|
||||||
|
public static int BS(int array[], int key, int lb, int ub)
|
||||||
|
{ if (lb>ub)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mid=(lb+ub)/2;
|
||||||
|
|
||||||
|
if (key<array[mid])
|
||||||
|
{
|
||||||
|
return (BS(array, key, lb, mid-1));
|
||||||
|
}
|
||||||
|
else if (key>array[mid])
|
||||||
|
{
|
||||||
|
return (BS(array, key, mid+1, ub));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return mid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is the main method of Binary Search
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
* @param args Command line parameters
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
Scanner input=new Scanner(System.in);
|
||||||
|
int array[]=new int[10] ;
|
||||||
|
int key;
|
||||||
|
|
||||||
|
//Input
|
||||||
|
System.out.println("Enter an array of 10 numbers : ");
|
||||||
|
for (int i=0; i<10 ;i++ )
|
||||||
|
{
|
||||||
|
array[i]=input.nextInt();
|
||||||
|
}
|
||||||
|
System.out.println("Enter the number to be searched : ");
|
||||||
|
key=input.nextInt();
|
||||||
|
|
||||||
|
int index=BS(array, key, 0, 9);
|
||||||
|
if (index!=-1)
|
||||||
|
{
|
||||||
|
System.out.println("Number found at index number : " + index);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
System.out.println("Not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
32
BinaryToDecimal.java
Normal file
32
BinaryToDecimal.java
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class converts a Binary number to a Decimal number
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class BinaryToDecimal
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Main Method
|
||||||
|
*
|
||||||
|
* @param args Command line arguments
|
||||||
|
*/
|
||||||
|
public static void main(String args[])
|
||||||
|
{
|
||||||
|
Scanner sc=new Scanner(System.in);
|
||||||
|
int n,k,d,s=0,c=0;
|
||||||
|
n=sc.nextInt();
|
||||||
|
k=n;
|
||||||
|
while(k!=0)
|
||||||
|
{
|
||||||
|
d=k%10;
|
||||||
|
s+=d*(int)Math.pow(2,c++);
|
||||||
|
k/=10;
|
||||||
|
}
|
||||||
|
System.out.println("Binary number:"+n);
|
||||||
|
System.out.println("Decimal equivalent:"+s);
|
||||||
|
}
|
||||||
|
}
|
50
BubbleSort.java
Normal file
50
BubbleSort.java
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class implements BubbleSort
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
class BubbleSort
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Main Method
|
||||||
|
*
|
||||||
|
* @param args Command line arguments
|
||||||
|
*/
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
int array[]=new int[6];
|
||||||
|
Scanner input=new Scanner(System.in);
|
||||||
|
|
||||||
|
//Input
|
||||||
|
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
|
||||||
|
for(int i=0; i<6; i++)
|
||||||
|
{
|
||||||
|
array[i]=input.nextInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
//Sorting
|
||||||
|
for(int i=0; i<5; i++)
|
||||||
|
{
|
||||||
|
for(int j=i+1; j<6; j++)
|
||||||
|
{
|
||||||
|
if(array[j]>array[i])
|
||||||
|
{
|
||||||
|
int temp=array[j];
|
||||||
|
array[j]=array[i];
|
||||||
|
array[i]=temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Output
|
||||||
|
for(int i=0; i<6; i++)
|
||||||
|
{
|
||||||
|
System.out.print(array[i]+"\t");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
31
DecimalToBinary.java
Normal file
31
DecimalToBinary.java
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class converts a Decimal number to a Binary number
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class DecimalToBinary
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Main Method
|
||||||
|
*
|
||||||
|
* @param args Command Line Arguments
|
||||||
|
*/
|
||||||
|
public static void main(String args[])
|
||||||
|
{
|
||||||
|
Scanner sc=new Scanner(System.in);
|
||||||
|
int n,k,s=0,c=0,d;
|
||||||
|
n=sc.nextInt();
|
||||||
|
k=n;
|
||||||
|
while(k!=0)
|
||||||
|
{
|
||||||
|
d=k%2;
|
||||||
|
s=s+d*(int)Math.pow(10,c++);
|
||||||
|
k/=2;
|
||||||
|
}//converting decimal to binary
|
||||||
|
System.out.println("Decimal number:"+n);
|
||||||
|
System.out.println("Binary equivalent:"+s);
|
||||||
|
}
|
||||||
|
}
|
31
DecimalToOctal.java
Normal file
31
DecimalToOctal.java
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class converts Decimal numbers to Octal Numbers
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class Decimal_Octal
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Main Method
|
||||||
|
*
|
||||||
|
* @param args Command line Arguments
|
||||||
|
*/
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
Scanner sc=new Scanner(System.in);
|
||||||
|
int n,k,d,s=0,c=0;
|
||||||
|
n=sc.nextInt();
|
||||||
|
k=n;
|
||||||
|
while(k!=0)
|
||||||
|
{
|
||||||
|
d=k%8;
|
||||||
|
s+=d*(int)Math.pow(10,c++);
|
||||||
|
k/=8;
|
||||||
|
}
|
||||||
|
System.out.println("Decimal number:"+n);
|
||||||
|
System.out.println("Octal equivalent:"+s);
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,19 @@
|
|||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This program will print out the factorial of any non-negative
|
||||||
|
* number that you input into it.
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class Factorial{
|
public class Factorial{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The main method
|
||||||
|
*
|
||||||
|
* @param args Command line arguments
|
||||||
|
*/
|
||||||
public static void main(String[] args){
|
public static void main(String[] args){
|
||||||
Scanner input = new Scanner(System.in);
|
Scanner input = new Scanner(System.in);
|
||||||
//Prompt user to enter integer
|
//Prompt user to enter integer
|
||||||
@ -20,7 +32,12 @@ public class Factorial{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Factorial method
|
/**
|
||||||
|
* Recursive Factorial Method
|
||||||
|
*
|
||||||
|
* @param n The number to factorial
|
||||||
|
* @return The factorial of the number
|
||||||
|
*/
|
||||||
public static long factorial(int n){
|
public static long factorial(int n){
|
||||||
|
|
||||||
if (n==0){
|
if (n==0){
|
||||||
|
@ -1,12 +1,27 @@
|
|||||||
/*
|
/**
|
||||||
* The Sieve of Eratosthenes is an algorithm used to find prime numbers, up to a given value.
|
* The Sieve of Eratosthenes is an algorithm use to find prime numbers,
|
||||||
|
* up to a given value.
|
||||||
* Illustration: https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif
|
* Illustration: https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif
|
||||||
*/
|
* (This illustration is also in the github repository)
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class FindingPrimes{
|
public class FindingPrimes{
|
||||||
|
/**
|
||||||
|
* The Main method
|
||||||
|
*
|
||||||
|
* @param args Command line arguments
|
||||||
|
*/
|
||||||
public static void main(String args[]){
|
public static void main(String args[]){
|
||||||
SOE(20); //Example: Finds all the primes up to 20
|
SOE(20); //Example: Finds all the primes up to 20
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The method implementing the Sieve of Eratosthenes
|
||||||
|
*
|
||||||
|
* @param n Number to perform SOE on
|
||||||
|
*/
|
||||||
public static void SOE(int n){
|
public static void SOE(int n){
|
||||||
boolean sieve[] = new boolean[n];
|
boolean sieve[] = new boolean[n];
|
||||||
|
|
||||||
|
359
HeapSort.java
359
HeapSort.java
@ -1,185 +1,174 @@
|
|||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Heap Sort Algorithm. Implements MinHeap
|
* Heap Sort Algorithm
|
||||||
*
|
* Implements MinHeap
|
||||||
*/
|
*
|
||||||
public class HeapSort {
|
* @author Unknown
|
||||||
/**
|
*
|
||||||
* array to store heap.
|
*/
|
||||||
*/
|
public class HeapSort {
|
||||||
private int[] heap;
|
/** Array to store heap */
|
||||||
/**
|
private int[] heap;
|
||||||
* size of heap.
|
/** The size of the heap */
|
||||||
*/
|
private int size;
|
||||||
private int size;
|
|
||||||
|
/**
|
||||||
/**
|
* Constructor
|
||||||
* Constructor.
|
*
|
||||||
*
|
* @param heap array of unordered integers
|
||||||
* @param heap
|
*/
|
||||||
* array of unordered integers
|
public HeapSort(int[] heap) {
|
||||||
*/
|
this.setHeap(heap);
|
||||||
public HeapSort(int[] heap) {
|
this.setSize(heap.length);
|
||||||
this.setHeap(heap);
|
}
|
||||||
this.setSize(heap.length);
|
|
||||||
}
|
/**
|
||||||
|
* Setter for variable size
|
||||||
/**
|
*
|
||||||
* Sets this.size with {@code length).
|
* @param length new size
|
||||||
*
|
*/
|
||||||
* @param length
|
private void setSize(int length) {
|
||||||
* integer length of heap
|
this.size = length;
|
||||||
*/
|
}
|
||||||
private void setSize(int length) {
|
|
||||||
this.size = length;
|
/**
|
||||||
}
|
* Setter for variable heap
|
||||||
|
*
|
||||||
/**
|
* @param heap array of unordered elements
|
||||||
* Sets Heap with {@code heap}.
|
*/
|
||||||
*
|
private void setHeap(int[] heap) {
|
||||||
* @param heap
|
this.heap = heap;
|
||||||
* array of unordered elements
|
}
|
||||||
*/
|
|
||||||
private void setHeap(int[] heap) {
|
/**
|
||||||
this.heap = heap;
|
* Swaps index of first with second
|
||||||
}
|
*
|
||||||
|
* @param first First index to switch
|
||||||
/**
|
* @param second Second index to switch
|
||||||
* Swaps index of {@code first} with {@code second}.
|
*/
|
||||||
*
|
private void swap(int first, int second) {
|
||||||
* @param first
|
int temp = this.heap[first];
|
||||||
* index to swap {@code second} with
|
this.heap[first] = this.heap[second];
|
||||||
* @param second
|
this.heap[second] = temp;
|
||||||
* index to swap {@code first} with
|
}
|
||||||
*/
|
|
||||||
private void swap(int first, int second) {
|
/**
|
||||||
int temp = this.heap[first];
|
* Heapifies subtree from top as root to last as last child
|
||||||
this.heap[first] = this.heap[second];
|
*
|
||||||
this.heap[second] = temp;
|
* @param rootIndex index of root
|
||||||
}
|
* @param lastChild index of last child
|
||||||
|
*/
|
||||||
/**
|
private void heapSubtree(int rootIndex, int lastChild) {
|
||||||
* Heapifies subtree from {@code top} as root to {@code last} as last child.
|
int leftIndex = rootIndex * 2 + 1;
|
||||||
*
|
int rightIndex = rootIndex * 2 + 2;
|
||||||
* @param rootIndex
|
int root = this.heap[rootIndex];
|
||||||
* index of root
|
if (rightIndex <= lastChild) { // if has right and left children
|
||||||
* @param lastChild
|
int left = this.heap[leftIndex];
|
||||||
* index of last child
|
int right = this.heap[rightIndex];
|
||||||
*/
|
if (left < right && left < root) {
|
||||||
private void heapSubtree(int rootIndex, int lastChild) {
|
this.swap(leftIndex, rootIndex);
|
||||||
int leftIndex = rootIndex * 2 + 1;
|
this.heapSubtree(leftIndex, lastChild);
|
||||||
int rightIndex = rootIndex * 2 + 2;
|
} else if (right < root) {
|
||||||
int root = this.heap[rootIndex];
|
this.swap(rightIndex, rootIndex);
|
||||||
if (rightIndex <= lastChild) { // if has right and left children
|
this.heapSubtree(rightIndex, lastChild);
|
||||||
int left = this.heap[leftIndex];
|
}
|
||||||
int right = this.heap[rightIndex];
|
} else if (leftIndex <= lastChild) { // if no right child, but has left child
|
||||||
if (left < right && left < root) {
|
int left = this.heap[leftIndex];
|
||||||
this.swap(leftIndex, rootIndex);
|
if (left < root) {
|
||||||
this.heapSubtree(leftIndex, lastChild);
|
this.swap(leftIndex, rootIndex);
|
||||||
} else if (right < root) {
|
this.heapSubtree(leftIndex, lastChild);
|
||||||
this.swap(rightIndex, rootIndex);
|
}
|
||||||
this.heapSubtree(rightIndex, lastChild);
|
}
|
||||||
}
|
}
|
||||||
} else if (leftIndex <= lastChild) { // if no right child, but has left child
|
|
||||||
int left = this.heap[leftIndex];
|
/**
|
||||||
if (left < root) {
|
* Makes heap with root as root
|
||||||
this.swap(leftIndex, rootIndex);
|
*
|
||||||
this.heapSubtree(leftIndex, lastChild);
|
* @param root index of root of heap
|
||||||
}
|
*/
|
||||||
}
|
private void makeMinHeap(int root) {
|
||||||
}
|
int leftIndex = root * 2 + 1;
|
||||||
|
int rightIndex = root * 2 + 2;
|
||||||
/**
|
boolean hasLeftChild = leftIndex < this.heap.length;
|
||||||
* Makes heap with {@code root} as root.
|
boolean hasRightChild = rightIndex < this.heap.length;
|
||||||
*
|
if (hasRightChild) { //if has left and right
|
||||||
* @param root
|
this.makeMinHeap(leftIndex);
|
||||||
* index of root of heap
|
this.makeMinHeap(rightIndex);
|
||||||
*/
|
this.heapSubtree(root, this.heap.length - 1);
|
||||||
private void makeMinHeap(int root) {
|
} else if (hasLeftChild) {
|
||||||
int leftIndex = root * 2 + 1;
|
this.heapSubtree(root, this.heap.length - 1);
|
||||||
int rightIndex = root * 2 + 2;
|
}
|
||||||
boolean hasLeftChild = leftIndex < this.heap.length;
|
}
|
||||||
boolean hasRightChild = rightIndex < this.heap.length;
|
|
||||||
if (hasRightChild) { //if has left and right
|
/**
|
||||||
this.makeMinHeap(leftIndex);
|
* Gets the root of heap
|
||||||
this.makeMinHeap(rightIndex);
|
*
|
||||||
this.heapSubtree(root, this.heap.length - 1);
|
* @return root of heap
|
||||||
} else if (hasLeftChild) {
|
*/
|
||||||
this.heapSubtree(root, this.heap.length - 1);
|
private int getRoot() {
|
||||||
}
|
this.swap(0, this.size - 1);
|
||||||
}
|
this.size--;
|
||||||
|
this.heapSubtree(0, this.size - 1);
|
||||||
/**
|
return this.heap[this.size]; // return old root
|
||||||
* Gets the root of this.heap.
|
}
|
||||||
*
|
|
||||||
* @return root of this.heap
|
/**
|
||||||
*/
|
* Sorts heap with heap sort; displays ordered elements to console.
|
||||||
private int getRoot() {
|
*
|
||||||
this.swap(0, this.size - 1);
|
* @return sorted array of sorted elements
|
||||||
this.size--;
|
*/
|
||||||
this.heapSubtree(0, this.size - 1);
|
public final int[] sort() {
|
||||||
return this.heap[this.size]; // return old root
|
this.makeMinHeap(0); // make min heap using index 0 as root.
|
||||||
}
|
int[] sorted = new int[this.size];
|
||||||
|
int index = 0;
|
||||||
/**
|
while (this.size > 0) {
|
||||||
* Sorts this.heap with heap sort; displays ordered elements to console.
|
int min = this.getRoot();
|
||||||
*
|
sorted[index] = min;
|
||||||
* @return {@code sorted} array of sorted elements
|
index++;
|
||||||
*/
|
}
|
||||||
public final int[] sort() {
|
return sorted;
|
||||||
this.makeMinHeap(0); // make min heap using index 0 as root.
|
}
|
||||||
int[] sorted = new int[this.size];
|
|
||||||
int index = 0;
|
/**
|
||||||
while (this.size > 0) {
|
* Gets input to sort
|
||||||
int min = this.getRoot();
|
*
|
||||||
sorted[index] = min;
|
* @return unsorted array of integers to sort
|
||||||
index++;
|
*/
|
||||||
}
|
public static int[] getInput() {
|
||||||
return sorted;
|
final int numElements = 6;
|
||||||
}
|
int[] unsorted = new int[numElements];
|
||||||
|
Scanner input = new Scanner(System.in);
|
||||||
/**
|
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
|
||||||
* Gets input to sort.
|
for (int i = 0; i < numElements; i++) {
|
||||||
*
|
unsorted[i] = input.nextInt();
|
||||||
* @return unsorted array of integers to sort
|
}
|
||||||
*/
|
input.close();
|
||||||
public static int[] getInput() {
|
return unsorted;
|
||||||
final int numElements = 6;
|
}
|
||||||
int[] unsorted = new int[numElements];
|
|
||||||
Scanner input = new Scanner(System.in);
|
/**
|
||||||
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
|
* Prints elements in heap
|
||||||
for (int i = 0; i < numElements; i++) {
|
*
|
||||||
unsorted[i] = input.nextInt();
|
* @param heap array representing heap
|
||||||
}
|
*/
|
||||||
input.close();
|
public static void printData(int[] heap) {
|
||||||
return unsorted;
|
System.out.println("Sorted Elements:");
|
||||||
}
|
for (int i = 0; i < heap.length; i++) {
|
||||||
|
System.out.print(" " + heap[i] + " ");
|
||||||
/**
|
}
|
||||||
* Prints elements in heap.
|
}
|
||||||
*
|
|
||||||
* @param heap
|
/**
|
||||||
* array representing heap
|
* Main method
|
||||||
*/
|
*
|
||||||
public static void printData(int[] heap) {
|
* @param args the command line arguments
|
||||||
System.out.println("Sorted Elements:");
|
*/
|
||||||
for (int i = 0; i < heap.length; i++) {
|
public static void main(String[] args) {
|
||||||
System.out.print(" " + heap[i] + " ");
|
int[] heap = getInput();
|
||||||
}
|
HeapSort data = new HeapSort(heap);
|
||||||
}
|
int[] sorted = data.sort();
|
||||||
|
printData(sorted);
|
||||||
/**
|
}
|
||||||
* Main method.
|
|
||||||
*
|
}
|
||||||
* @param args
|
|
||||||
* the command line arguments
|
|
||||||
*/
|
|
||||||
public static void main(String[] args) {
|
|
||||||
int[] heap = getInput();
|
|
||||||
HeapSort data = new HeapSort(heap);
|
|
||||||
int[] sorted = data.sort();
|
|
||||||
printData(sorted);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
49
InsertionSort.java
Normal file
49
InsertionSort.java
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class implements Insertion Sort
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class InsertionSort
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Main Method
|
||||||
|
*
|
||||||
|
* @param args Command line arguments
|
||||||
|
*/
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
int array[]=new int[6];
|
||||||
|
Scanner input=new Scanner(System.in);
|
||||||
|
|
||||||
|
//Input
|
||||||
|
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
|
||||||
|
for(int i=0; i<6; i++)
|
||||||
|
{
|
||||||
|
array[i]=input.nextInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
//Sorting
|
||||||
|
for(int i=0; i<6; i++)
|
||||||
|
{
|
||||||
|
int temp=array[i];
|
||||||
|
int j=i-1;
|
||||||
|
while(j>=0 && temp<array[j] )
|
||||||
|
{
|
||||||
|
array[j+1]=array[j];
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
|
||||||
|
array[j+1]=temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Output
|
||||||
|
for(int i=0; i<6; i++)
|
||||||
|
{
|
||||||
|
System.out.print(array[i]+"\t");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
55
InsertionSortInteger.java
Normal file
55
InsertionSortInteger.java
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
/**
|
||||||
|
* This is my implementation of an insertion sort.
|
||||||
|
*
|
||||||
|
* I decided to do this when i didn't feel comfortable enough about implementing
|
||||||
|
* different types of sorts, so this is my trial and error to try and get myself to implement
|
||||||
|
* the various sorts correctly.
|
||||||
|
*
|
||||||
|
* @author Kody C. Kendall
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class InsertionSortInteger {
|
||||||
|
|
||||||
|
|
||||||
|
public int[] insertionIntArraySort(int[] initialArray){
|
||||||
|
|
||||||
|
int[] sortedArray = new int[initialArray.length];
|
||||||
|
|
||||||
|
//Marking first element as sorted.
|
||||||
|
sortedArray[0] = initialArray[0];
|
||||||
|
|
||||||
|
//For each element in the initialArray
|
||||||
|
for (int index = 1; index < initialArray.length; index++){
|
||||||
|
|
||||||
|
//Finding the last index that was sorted
|
||||||
|
int lastSortedIndex = index;
|
||||||
|
|
||||||
|
//Extracting the next element to be compared w/ other sorted elements from initial array
|
||||||
|
int extractedElement = initialArray[index];
|
||||||
|
|
||||||
|
//Compare the values of the sorted index to the current extractedElement
|
||||||
|
for (lastSortedIndex = index; lastSortedIndex > 0; lastSortedIndex--){
|
||||||
|
|
||||||
|
//If our extracted element is smaller than element to the right, switch them.
|
||||||
|
if (sortedArray[lastSortedIndex-1] > extractedElement){
|
||||||
|
//move the element to the left of extractedElement to the right in sortedArray
|
||||||
|
sortedArray[lastSortedIndex] = sortedArray[lastSortedIndex-1];
|
||||||
|
//And move the current extractedElement to the left one (since it's smaller).
|
||||||
|
sortedArray[lastSortedIndex-1] = extractedElement;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
//insert element where it is.
|
||||||
|
sortedArray[lastSortedIndex] = extractedElement;
|
||||||
|
//Terminating loop since element is in the right spot.
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return sortedArray;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,7 +1,10 @@
|
|||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class LinearSearch{
|
public class LinearSearch{
|
||||||
//Main method
|
/**
|
||||||
|
* The main method
|
||||||
|
* @param args Command line arguments
|
||||||
|
*/
|
||||||
public static void main(String[] args){
|
public static void main(String[] args){
|
||||||
|
|
||||||
Scanner input = new Scanner(System.in);
|
Scanner input = new Scanner(System.in);
|
||||||
@ -27,7 +30,13 @@ public class LinearSearch{
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Linear search method
|
/**
|
||||||
|
* Linear search method
|
||||||
|
*
|
||||||
|
* @param list List to be searched
|
||||||
|
* @param key Key being searched for
|
||||||
|
* @return Location of the key
|
||||||
|
*/
|
||||||
public static int linearsearch(int[] list, int key){
|
public static int linearsearch(int[] list, int key){
|
||||||
|
|
||||||
for (int i = 0; i< list.length; i++){
|
for (int i = 0; i< list.length; i++){
|
||||||
@ -37,7 +46,11 @@ public class LinearSearch{
|
|||||||
|
|
||||||
} return -1;
|
} return -1;
|
||||||
}
|
}
|
||||||
//Helper method
|
/**
|
||||||
|
* Helper Method
|
||||||
|
*
|
||||||
|
* @param a array to be printed
|
||||||
|
*/
|
||||||
public static void printarray(int[] a){
|
public static void printarray(int[] a){
|
||||||
System.out.print("The array is: ");
|
System.out.print("The array is: ");
|
||||||
for( int d: a){
|
for( int d: a){
|
||||||
|
216
MergeSort.java
216
MergeSort.java
@ -1,107 +1,109 @@
|
|||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Merge Sort
|
* This class implements MergeSort
|
||||||
*
|
* @author Unknown
|
||||||
*/
|
*
|
||||||
public class MergeSort {
|
*/
|
||||||
private int[] array;
|
public class MergeSort {
|
||||||
private int[] tempMergArr;
|
/** Array for mergeSort*/
|
||||||
private int length;
|
private int[] array;
|
||||||
|
/** Temp Merge Array*/
|
||||||
/**
|
private int[] tempMergArr;
|
||||||
* Sorts {@code inputArr} with merge sort algorithm.
|
/** Length of the array*/
|
||||||
*
|
private int length;
|
||||||
* @param inputArr
|
|
||||||
*/
|
/**
|
||||||
public final void sort(int inputArr[]) {
|
* Sorts inputArr with merge sort algorithm
|
||||||
this.array = inputArr;
|
*
|
||||||
this.length = inputArr.length;
|
* @param inputArr Array to be sorted
|
||||||
this.tempMergArr = new int[this.length];
|
*/
|
||||||
this.mergeSort(0, this.length - 1);
|
public final void sort(int inputArr[]) {
|
||||||
}
|
this.array = inputArr;
|
||||||
|
this.length = inputArr.length;
|
||||||
/**
|
this.tempMergArr = new int[this.length];
|
||||||
* Partitions Array into recursively smaller pieces.
|
this.mergeSort(0, this.length - 1);
|
||||||
*
|
}
|
||||||
* @param lowerIndex
|
|
||||||
* lower bound to include in the first partition
|
/**
|
||||||
* @param higherIndex
|
* Partitions Array into recursively smaller pieces
|
||||||
* upper bound to include in the third partition
|
*
|
||||||
*/
|
* @param lowerIndex lower bound to include in the first partition
|
||||||
private void mergeSort(int lowerIndex, int higherIndex) {
|
* @param higherIndex upper bound to include in the third partition
|
||||||
if (lowerIndex < higherIndex) {
|
*/
|
||||||
int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
|
private void mergeSort(int lowerIndex, int higherIndex) {
|
||||||
// Below step sorts the left side of the array
|
if (lowerIndex < higherIndex) {
|
||||||
this.mergeSort(lowerIndex, middle);
|
int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
|
||||||
// Below step sorts the right side of the array
|
// Below step sorts the left side of the array
|
||||||
this.mergeSort(middle + 1, higherIndex);
|
this.mergeSort(lowerIndex, middle);
|
||||||
// Now merge both sides
|
// Below step sorts the right side of the array
|
||||||
this.mergeParts(lowerIndex, middle, higherIndex);
|
this.mergeSort(middle + 1, higherIndex);
|
||||||
}
|
// Now merge both sides
|
||||||
}
|
this.mergeParts(lowerIndex, middle, higherIndex);
|
||||||
|
}
|
||||||
/**
|
}
|
||||||
* Merges partitions.
|
|
||||||
*
|
/**
|
||||||
* @param lowerIndex
|
* Merges partitions
|
||||||
* @param middle
|
*
|
||||||
* @param higherIndex
|
* @param lowerIndex The lower index
|
||||||
*/
|
* @param middle The middle index
|
||||||
private void mergeParts(int lowerIndex, int middle, int higherIndex) {
|
* @param higherIndex The higher index
|
||||||
for (int i = lowerIndex; i <= higherIndex; i++) {
|
*/
|
||||||
this.tempMergArr[i] = this.array[i];
|
private void mergeParts(int lowerIndex, int middle, int higherIndex) {
|
||||||
}
|
for (int i = lowerIndex; i <= higherIndex; i++) {
|
||||||
int i = lowerIndex;
|
this.tempMergArr[i] = this.array[i];
|
||||||
int j = middle + 1;
|
}
|
||||||
int k = lowerIndex;
|
int i = lowerIndex;
|
||||||
while (i <= middle && j <= higherIndex) {
|
int j = middle + 1;
|
||||||
if (this.tempMergArr[i] <= this.tempMergArr[j]) {
|
int k = lowerIndex;
|
||||||
this.array[k] = this.tempMergArr[i];
|
while (i <= middle && j <= higherIndex) {
|
||||||
i++;
|
if (this.tempMergArr[i] <= this.tempMergArr[j]) {
|
||||||
} else {
|
this.array[k] = this.tempMergArr[i];
|
||||||
this.array[k] = this.tempMergArr[j];
|
i++;
|
||||||
j++;
|
} else {
|
||||||
}
|
this.array[k] = this.tempMergArr[j];
|
||||||
k++;
|
j++;
|
||||||
}
|
}
|
||||||
while (i <= middle) {
|
k++;
|
||||||
this.array[k] = this.tempMergArr[i];
|
}
|
||||||
k++;
|
while (i <= middle) {
|
||||||
i++;
|
this.array[k] = this.tempMergArr[i];
|
||||||
}
|
k++;
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
}
|
||||||
* Gets input to sort.
|
|
||||||
*
|
/**
|
||||||
* @return unsorted array of integers to sort
|
* Gets input to sort
|
||||||
*/
|
*
|
||||||
public static int[] getInput() {
|
* @return unsorted array of integers to sort
|
||||||
final int numElements = 6;
|
*/
|
||||||
int[] unsorted = new int[numElements];
|
public static int[] getInput() {
|
||||||
Scanner input = new Scanner(System.in);
|
final int numElements = 6;
|
||||||
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
|
int[] unsorted = new int[numElements];
|
||||||
for (int i = 0; i < numElements; i++) {
|
Scanner input = new Scanner(System.in);
|
||||||
unsorted[i] = input.nextInt();
|
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
|
||||||
}
|
for (int i = 0; i < numElements; i++) {
|
||||||
input.close();
|
unsorted[i] = input.nextInt();
|
||||||
return unsorted;
|
}
|
||||||
}
|
input.close();
|
||||||
|
return unsorted;
|
||||||
/**
|
}
|
||||||
* Main Method.
|
|
||||||
*
|
/**
|
||||||
* @param args
|
* Main Method
|
||||||
*/
|
*
|
||||||
public static void main(String args[]) {
|
* @param args Command line arguments
|
||||||
int[] inputArr = getInput();
|
*/
|
||||||
MergeSort mergeSort = new MergeSort();
|
public static void main(String args[]) {
|
||||||
mergeSort.sort(inputArr);
|
int[] inputArr = getInput();
|
||||||
for (int i : inputArr) {
|
MergeSort mergeSort = new MergeSort();
|
||||||
System.out.print(i);
|
mergeSort.sort(inputArr);
|
||||||
System.out.print(" ");
|
for (int i : inputArr) {
|
||||||
}
|
System.out.print(i);
|
||||||
}
|
System.out.print(" ");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,7 +1,19 @@
|
|||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implementation of QuickSort
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
public class Quicksort{
|
public class Quicksort{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The main method
|
||||||
|
*
|
||||||
|
* @param args Command line arguments
|
||||||
|
*/
|
||||||
public static void main(String[] args){
|
public static void main(String[] args){
|
||||||
Scanner input = new Scanner(System.in);
|
Scanner input = new Scanner(System.in);
|
||||||
int[] array;
|
int[] array;
|
||||||
@ -28,7 +40,13 @@ public class Quicksort{
|
|||||||
System.out.println();
|
System.out.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
//Quicksort Method
|
/**
|
||||||
|
* QuickSort method
|
||||||
|
*
|
||||||
|
* @param ar Array to perform QuickSort
|
||||||
|
* @param start Start of the array
|
||||||
|
* @param end End of the array
|
||||||
|
*/
|
||||||
public static void quicksort(int[] ar, int start, int end){
|
public static void quicksort(int[] ar, int start, int end){
|
||||||
int[] array;
|
int[] array;
|
||||||
|
|
||||||
@ -54,7 +72,13 @@ public class Quicksort{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Helper methods
|
/**
|
||||||
|
* Helper Method 1 - Swaps elements of an array
|
||||||
|
*
|
||||||
|
* @param ar Array to be used
|
||||||
|
* @param index1 Index 1 to be switched with Index 2
|
||||||
|
* @param index2 Index 2 to be switched with Index 1
|
||||||
|
*/
|
||||||
public static void swap(int[] ar, int index1, int index2){
|
public static void swap(int[] ar, int index1, int index2){
|
||||||
|
|
||||||
int temp = ar[index1];
|
int temp = ar[index1];
|
||||||
@ -62,6 +86,11 @@ public class Quicksort{
|
|||||||
ar[index2] = temp;
|
ar[index2] = temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper Method 2 - Prints the elements of an array
|
||||||
|
*
|
||||||
|
* @param array Array to be printed
|
||||||
|
*/
|
||||||
public static void printarray(int[] array){
|
public static void printarray(int[] array){
|
||||||
|
|
||||||
for (int data : array){
|
for (int data : array){
|
||||||
|
47
ReverseString.java
Normal file
47
ReverseString.java
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method produces a reversed version of a string
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class ReverseString
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method reverses the string str and returns it
|
||||||
|
* @param str String to be reversed
|
||||||
|
* @return Reversed string
|
||||||
|
*/
|
||||||
|
static String reverseString(String str)
|
||||||
|
{
|
||||||
|
String reverse=" ";
|
||||||
|
if(str.length()==1)
|
||||||
|
{
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
reverse=reverse+str.charAt(str.length()-1)+reverseString(str.substring(0,str.length()-1));
|
||||||
|
return reverse;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Main Method
|
||||||
|
*
|
||||||
|
* @param args Command line arguments
|
||||||
|
* @throws IOException Exception thrown because of BufferedReader
|
||||||
|
*/
|
||||||
|
public static void main(String args[]) throws IOException
|
||||||
|
{
|
||||||
|
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
|
||||||
|
System.out.println("Enter the string");
|
||||||
|
String srr=br.readLine();
|
||||||
|
System.out.println("Reverse="+reverseString(srr));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
52
SelectionSort.java
Normal file
52
SelectionSort.java
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class implements Selection Sort
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
class SelectionSort
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Main method
|
||||||
|
*
|
||||||
|
* @param args Command line arguments
|
||||||
|
*/
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
int array[]=new int[6];
|
||||||
|
Scanner input=new Scanner(System.in);
|
||||||
|
|
||||||
|
//Input
|
||||||
|
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
|
||||||
|
for(int i=0; i<6; i++)
|
||||||
|
{
|
||||||
|
array[i]=input.nextInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
//Sorting
|
||||||
|
for(int i=0; i<6; i++)
|
||||||
|
{
|
||||||
|
int min=i;
|
||||||
|
for(int j=i+1; j<6; j++)
|
||||||
|
{
|
||||||
|
if(array[j]<array[min])
|
||||||
|
{
|
||||||
|
min=j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int temp=array[i];
|
||||||
|
array[i]=array[min];
|
||||||
|
array[min]=temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Output
|
||||||
|
for(int i=0; i<6; i++)
|
||||||
|
{
|
||||||
|
System.out.print(array[i]+"\t");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
22
bfs.java
22
bfs.java
@ -1,7 +1,20 @@
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class App{
|
/**
|
||||||
|
* Implementation of a Breadth First Search
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class bfs{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The BFS implemented in code to use.
|
||||||
|
*
|
||||||
|
* @param a Structure to perform the search on
|
||||||
|
* @param vertices The vertices to use
|
||||||
|
* @param source The Source
|
||||||
|
*/
|
||||||
public static void bfs(byte [][] a,int vertices,int source){ //passing adjacency matrix and no of vertices
|
public static void bfs(byte [][] a,int vertices,int source){ //passing adjacency matrix and no of vertices
|
||||||
byte []b=new byte[vertices]; //flag container containing status of each vertices
|
byte []b=new byte[vertices]; //flag container containing status of each vertices
|
||||||
Arrays.fill(b,(byte)-1); //status initialization
|
Arrays.fill(b,(byte)-1); //status initialization
|
||||||
@ -10,7 +23,7 @@ public class App{
|
|||||||
0 = waiting
|
0 = waiting
|
||||||
1 = processed */
|
1 = processed */
|
||||||
|
|
||||||
Queue <Integer> st=new LinkedList<>(); //operational stack
|
Queue <Integer> st=new Queue<>(); //operational stack
|
||||||
st.add(source); //assigning source
|
st.add(source); //assigning source
|
||||||
while(!st.isEmpty()){
|
while(!st.isEmpty()){
|
||||||
b[st.peek()]=(byte)0; //assigning waiting status
|
b[st.peek()]=(byte)0; //assigning waiting status
|
||||||
@ -26,6 +39,11 @@ public class App{
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The main method
|
||||||
|
*
|
||||||
|
* @param args Command line arguments
|
||||||
|
*/
|
||||||
public static void main(String args[]){
|
public static void main(String args[]){
|
||||||
Scanner in=new Scanner(System.in);
|
Scanner in=new Scanner(System.in);
|
||||||
int vertices=in.nextInt(),source=in.nextInt();
|
int vertices=in.nextInt(),source=in.nextInt();
|
||||||
|
@ -1,6 +1,19 @@
|
|||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* You enter a string into this program, and it will return how
|
||||||
|
* many words were in that particular string
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
class CountTheWords
|
class CountTheWords
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* The main method
|
||||||
|
*
|
||||||
|
* @param args Command line arguments
|
||||||
|
*/
|
||||||
public static void main(String args[])
|
public static void main(String args[])
|
||||||
{
|
{
|
||||||
System.out.println("Enter the string");
|
System.out.println("Enter the string");
|
||||||
|
@ -1,11 +1,35 @@
|
|||||||
//A binary tree is a data structure in which an element has two successors(children)
|
/**
|
||||||
//The left child is usually smaller than the parent, and the right child is usually bigger
|
* This entire class is used to build a Binary Tree data structure.
|
||||||
|
* There is the Node Class and the Tree Class, both explained below.
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class implements the nodes that will go on the Binary Tree.
|
||||||
|
* They consist of the data in them, the node to the left, the node
|
||||||
|
* to the right, and the parent from which they came from.
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
class Node{
|
class Node{
|
||||||
|
/** Data for the node */
|
||||||
public int data;
|
public int data;
|
||||||
|
/** The Node to the left of this one */
|
||||||
public Node left;
|
public Node left;
|
||||||
|
/** The Node to the right of this one */
|
||||||
public Node right;
|
public Node right;
|
||||||
|
/** The parent of this node */
|
||||||
public Node parent;
|
public Node parent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor of Node
|
||||||
|
*
|
||||||
|
* @param value Value to put in the node
|
||||||
|
*/
|
||||||
public Node(int value){
|
public Node(int value){
|
||||||
data = value;
|
data = value;
|
||||||
left = null;
|
left = null;
|
||||||
@ -14,13 +38,33 @@ class Node{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A binary tree is a data structure in which an element
|
||||||
|
* has two successors(children). The left child is usually
|
||||||
|
* smaller than the parent, and the right child is usually
|
||||||
|
* bigger.
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
class Tree{
|
class Tree{
|
||||||
|
/** The root of the Binary Tree */
|
||||||
private Node root;
|
private Node root;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
public Tree(){
|
public Tree(){
|
||||||
root = null;
|
root = null;
|
||||||
}
|
}
|
||||||
//Returns the node if it finds it, otherwise returns the parent
|
|
||||||
|
/**
|
||||||
|
* Method to find a Node with a certain value
|
||||||
|
*
|
||||||
|
* @param key Value being looked for
|
||||||
|
* @return The node if it finds it, otherwise returns the parent
|
||||||
|
*/
|
||||||
public Node find(int key){
|
public Node find(int key){
|
||||||
Node current = root;
|
Node current = root;
|
||||||
Node last = root;
|
Node last = root;
|
||||||
@ -37,7 +81,11 @@ class Tree{
|
|||||||
return last;
|
return last;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Inserts the given value
|
/**
|
||||||
|
* Inserts certain value into the Binary Tree
|
||||||
|
*
|
||||||
|
* @param value Value to be inserted
|
||||||
|
*/
|
||||||
public void put(int value){
|
public void put(int value){
|
||||||
Node newNode = new Node(value);
|
Node newNode = new Node(value);
|
||||||
if(root == null)
|
if(root == null)
|
||||||
@ -60,7 +108,12 @@ class Tree{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Deletes the given value
|
/**
|
||||||
|
* Deletes a given value from the Binary Tree
|
||||||
|
*
|
||||||
|
* @param value Value to be deleted
|
||||||
|
* @return If the value was deleted
|
||||||
|
*/
|
||||||
public boolean remove(int value){
|
public boolean remove(int value){
|
||||||
//temp is the node to be deleted
|
//temp is the node to be deleted
|
||||||
Node temp = find(value);
|
Node temp = find(value);
|
||||||
@ -84,34 +137,34 @@ class Tree{
|
|||||||
|
|
||||||
//Two children
|
//Two children
|
||||||
else if(temp.left != null && temp.right != null){
|
else if(temp.left != null && temp.right != null){
|
||||||
Node succesor = findSuccesor(temp);
|
Node successor = findSuccessor(temp);
|
||||||
|
|
||||||
//The left tree of temp is made the left tree of the successor
|
//The left tree of temp is made the left tree of the successor
|
||||||
succesor.left = temp.left;
|
successor.left = temp.left;
|
||||||
succesor.left.parent = succesor;
|
successor.left.parent = successor;
|
||||||
|
|
||||||
//If the successor has a right child, the child's grandparent is it's new parent
|
//If the successor has a right child, the child's grandparent is it's new parent
|
||||||
if(succesor.right != null && succesor.parent != temp){
|
if(successor.right != null && successor.parent != temp){
|
||||||
succesor.right.parent = succesor.parent;
|
successor.right.parent = successor.parent;
|
||||||
succesor.parent.left = succesor.right;
|
successor.parent.left = successor.right;
|
||||||
succesor.right = temp.right;
|
successor.right = temp.right;
|
||||||
succesor.right.parent = succesor;
|
successor.right.parent = successor;
|
||||||
}
|
}
|
||||||
if(temp == root){
|
if(temp == root){
|
||||||
succesor.parent = null;
|
successor.parent = null;
|
||||||
root = succesor;
|
root = successor;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//If you're not deleting the root
|
//If you're not deleting the root
|
||||||
else{
|
else{
|
||||||
succesor.parent = temp.parent;
|
successor.parent = temp.parent;
|
||||||
|
|
||||||
//This if/else assigns the new node to be either the left or right child of the parent
|
//This if/else assigns the new node to be either the left or right child of the parent
|
||||||
if(temp.parent.data < temp.data)
|
if(temp.parent.data < temp.data)
|
||||||
temp.parent.right = succesor;
|
temp.parent.right = successor;
|
||||||
else
|
else
|
||||||
temp.parent.left = succesor;
|
temp.parent.left = successor;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -148,8 +201,14 @@ class Tree{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Move right once and go left down the tree as far as you can
|
/**
|
||||||
public Node findSuccesor(Node n){
|
* This method finds the Successor to the Node given.
|
||||||
|
* Move right once and go left down the tree as far as you can
|
||||||
|
*
|
||||||
|
* @param n Node that you want to find the Successor of
|
||||||
|
* @return The Successor of the node
|
||||||
|
*/
|
||||||
|
public Node findSuccessor(Node n){
|
||||||
if(n.right == null)
|
if(n.right == null)
|
||||||
return n;
|
return n;
|
||||||
Node current = n.right;
|
Node current = n.right;
|
||||||
@ -161,11 +220,20 @@ class Tree{
|
|||||||
return parent;
|
return parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the root of the Binary Tree
|
||||||
|
*
|
||||||
|
* @return the root of the Binary Tree
|
||||||
|
*/
|
||||||
public Node getRoot(){
|
public Node getRoot(){
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Prints leftChild - root - rightChild
|
/**
|
||||||
|
* Prints leftChild - root - rightChild
|
||||||
|
*
|
||||||
|
* @param localRoot The local root of the binary tree
|
||||||
|
*/
|
||||||
public void inOrder(Node localRoot){
|
public void inOrder(Node localRoot){
|
||||||
if(localRoot != null){
|
if(localRoot != null){
|
||||||
inOrder(localRoot.left);
|
inOrder(localRoot.left);
|
||||||
@ -173,7 +241,12 @@ class Tree{
|
|||||||
inOrder(localRoot.right);
|
inOrder(localRoot.right);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Prints root - leftChild - rightChild
|
|
||||||
|
/**
|
||||||
|
* Prints root - leftChild - rightChild
|
||||||
|
*
|
||||||
|
* @param localRoot The local root of the binary tree
|
||||||
|
*/
|
||||||
public void preOrder(Node localRoot){
|
public void preOrder(Node localRoot){
|
||||||
if(localRoot != null){
|
if(localRoot != null){
|
||||||
System.out.print(localRoot.data + " ");
|
System.out.print(localRoot.data + " ");
|
||||||
@ -181,7 +254,12 @@ class Tree{
|
|||||||
preOrder(localRoot.right);
|
preOrder(localRoot.right);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Prints rightChild - leftChild - root
|
|
||||||
|
/**
|
||||||
|
* Prints rightChild - leftChild - root
|
||||||
|
*
|
||||||
|
* @param localRoot The local root of the binary tree
|
||||||
|
*/
|
||||||
public void postOrder(Node localRoot){
|
public void postOrder(Node localRoot){
|
||||||
if(localRoot != null){
|
if(localRoot != null){
|
||||||
postOrder(localRoot.left);
|
postOrder(localRoot.left);
|
||||||
|
@ -1,19 +1,38 @@
|
|||||||
/*
|
/**
|
||||||
* A linked list is similar to an array, it holds values. However, links in a linked list do not have indexes.
|
* This class implements a DoublyLinkedList. This is done using the classes
|
||||||
* With a linked list you do not need to predetermine it's size as it grows and shrinks as it is edited.
|
* LinkedList and Link.
|
||||||
* This is an example of a double ended, doubly linked list.
|
*
|
||||||
* Each link references the next link and the previous one.
|
* A linked list is simplar to an array, it holds values. However,
|
||||||
*/
|
* links in a linked list do not have indees. With a linked list
|
||||||
class LinkedList{
|
* you do not need to predetermine it's size as it grows and shrinks
|
||||||
private Link head; //Head refers to the front of the list
|
* as it is edited. This is an example of a double ended, doubly
|
||||||
private Link tail; //Tail refers to the back of the list
|
* linked list. Each link references the next link and the previous
|
||||||
|
* one.
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
public LinkedList(){
|
class DoublyLinkedList{
|
||||||
|
/** Head refers to the front of the list */
|
||||||
|
private Link head;
|
||||||
|
/** Tail refers to the back of the list */
|
||||||
|
private Link tail;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
public DoublyLinkedList(){
|
||||||
head = null;
|
head = null;
|
||||||
tail = null;
|
tail = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void insertHead(int x){ //Insert an element at the head
|
/**
|
||||||
|
* Insert an element at the head
|
||||||
|
*
|
||||||
|
* @param x Element to be inserted
|
||||||
|
*/
|
||||||
|
public void insertHead(int x){
|
||||||
Link newLink = new Link(x); //Create a new link with a value attached to it
|
Link newLink = new Link(x); //Create a new link with a value attached to it
|
||||||
if(isEmpty()) //Set the first element added to be the tail
|
if(isEmpty()) //Set the first element added to be the tail
|
||||||
tail = newLink;
|
tail = newLink;
|
||||||
@ -23,6 +42,11 @@ class LinkedList{
|
|||||||
head = newLink; // newLink(head) <--> oldhead
|
head = newLink; // newLink(head) <--> oldhead
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Insert an element at the tail
|
||||||
|
*
|
||||||
|
* @param x Element to be inserted
|
||||||
|
*/
|
||||||
public void insertTail(int x){
|
public void insertTail(int x){
|
||||||
Link newLink = new Link(x);
|
Link newLink = new Link(x);
|
||||||
newLink.next = null; // currentTail(tail) newlink -->
|
newLink.next = null; // currentTail(tail) newlink -->
|
||||||
@ -31,7 +55,12 @@ class LinkedList{
|
|||||||
tail = newLink; // oldTail <--> newLink(tail) -->
|
tail = newLink; // oldTail <--> newLink(tail) -->
|
||||||
}
|
}
|
||||||
|
|
||||||
public Link deleteHead(){ //Delete the element at the head
|
/**
|
||||||
|
* Delete the element at the head
|
||||||
|
*
|
||||||
|
* @return The new head
|
||||||
|
*/
|
||||||
|
public Link deleteHead(){
|
||||||
Link temp = head;
|
Link temp = head;
|
||||||
head = head.next; // oldHead <--> 2ndElement(head)
|
head = head.next; // oldHead <--> 2ndElement(head)
|
||||||
head.previous = null; // oldHead --> 2ndElement(head) nothing pointing at old head so will be removed
|
head.previous = null; // oldHead --> 2ndElement(head) nothing pointing at old head so will be removed
|
||||||
@ -40,6 +69,11 @@ class LinkedList{
|
|||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete the element at the tail
|
||||||
|
*
|
||||||
|
* @return The new tail
|
||||||
|
*/
|
||||||
public Link deleteTail(){
|
public Link deleteTail(){
|
||||||
Link temp = tail;
|
Link temp = tail;
|
||||||
tail = tail.previous; // 2ndLast(tail) <--> oldTail --> null
|
tail = tail.previous; // 2ndLast(tail) <--> oldTail --> null
|
||||||
@ -47,6 +81,12 @@ class LinkedList{
|
|||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete the element from somewhere in the list
|
||||||
|
*
|
||||||
|
* @param x element to be deleted
|
||||||
|
* @return Link deleted
|
||||||
|
*/
|
||||||
public Link delete(int x){
|
public Link delete(int x){
|
||||||
Link current = head;
|
Link current = head;
|
||||||
|
|
||||||
@ -66,6 +106,11 @@ class LinkedList{
|
|||||||
return current;
|
return current;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inserts element and reorders
|
||||||
|
*
|
||||||
|
* @param x Element to be added
|
||||||
|
*/
|
||||||
public void insertOrdered(int x){
|
public void insertOrdered(int x){
|
||||||
Link newLink = new Link(x);
|
Link newLink = new Link(x);
|
||||||
Link current = head;
|
Link current = head;
|
||||||
@ -86,10 +131,18 @@ class LinkedList{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isEmpty(){ //Returns true if list is empty
|
/**
|
||||||
|
* Returns true if list is empty
|
||||||
|
*
|
||||||
|
* @return true if list is empty
|
||||||
|
*/
|
||||||
|
public boolean isEmpty(){
|
||||||
return(head == null);
|
return(head == null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints contents of the list
|
||||||
|
*/
|
||||||
public void display(){ //Prints contents of the list
|
public void display(){ //Prints contents of the list
|
||||||
Link current = head;
|
Link current = head;
|
||||||
while(current!=null){
|
while(current!=null){
|
||||||
@ -100,24 +153,44 @@ class LinkedList{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class is used to implement the nodes of the
|
||||||
|
* linked list.
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
class Link{
|
class Link{
|
||||||
|
/** Value of node */
|
||||||
public int value;
|
public int value;
|
||||||
public Link next; //This points to the link in front of the new link
|
/** This points to the link in front of the new link */
|
||||||
public Link previous; //This points to the link behind the new link
|
public Link next;
|
||||||
|
/** This points to the link behind the new link */
|
||||||
|
public Link previous;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param value Value of node
|
||||||
|
*/
|
||||||
public Link(int value){
|
public Link(int value){
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Displays the node
|
||||||
|
*/
|
||||||
public void displayLink(){
|
public void displayLink(){
|
||||||
System.out.print(value+" ");
|
System.out.print(value+" ");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
//Example
|
/**
|
||||||
public class DoublyLinkedList{
|
* Main Method
|
||||||
|
*
|
||||||
|
* @param args Command line arguments
|
||||||
|
*/
|
||||||
public static void main(String args[]){
|
public static void main(String args[]){
|
||||||
LinkedList myList = new LinkedList();
|
DoublyLinkedList myList = new DoublyLinkedList();
|
||||||
|
|
||||||
myList.insertHead(13);
|
myList.insertHead(13);
|
||||||
myList.insertHead(7);
|
myList.insertHead(7);
|
||||||
|
@ -1,21 +1,40 @@
|
|||||||
/*
|
/**
|
||||||
|
* This class implements a PriorityQueue.
|
||||||
|
*
|
||||||
* A priority queue adds elements into positions based on their priority.
|
* A priority queue adds elements into positions based on their priority.
|
||||||
* So the most important elements are placed at the front/on the top.
|
* So the most important elements are placed at the front/on the top.
|
||||||
* In this example I give numbers that are bigger, a higher priority.
|
* In this example I give numbers that are bigger, a higher priority.
|
||||||
* Queues in theory have no fixed size but when using an array implementation it does.
|
* Queues in theory have no fixed size but when using an array
|
||||||
|
* implementation it does.
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
class PriorityQueue{
|
class PriorityQueue{
|
||||||
|
/** The max size of the queue */
|
||||||
private int maxSize;
|
private int maxSize;
|
||||||
|
/** The array for the queue */
|
||||||
private int[] queueArray;
|
private int[] queueArray;
|
||||||
|
/** How many items are in the queue */
|
||||||
private int nItems;
|
private int nItems;
|
||||||
|
|
||||||
public PriorityQueue(int size){ //Constructor
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param size Size of the queue
|
||||||
|
*/
|
||||||
|
public PriorityQueue(int size){
|
||||||
maxSize = size;
|
maxSize = size;
|
||||||
queueArray = new int[size];
|
queueArray = new int[size];
|
||||||
nItems = 0;
|
nItems = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void insert(int value){ //Inserts an element in it's appropriate place
|
/**
|
||||||
|
* Inserts an element in it's appropriate place
|
||||||
|
*
|
||||||
|
* @param value Value to be inserted
|
||||||
|
*/
|
||||||
|
public void insert(int value){
|
||||||
if(nItems == 0){
|
if(nItems == 0){
|
||||||
queueArray[0] = value;
|
queueArray[0] = value;
|
||||||
}
|
}
|
||||||
@ -30,28 +49,64 @@ class PriorityQueue{
|
|||||||
nItems++;
|
nItems++;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int remove(){ //Remove the element from the front of the queue
|
/**
|
||||||
|
* Remove the element from the front of the queue
|
||||||
|
*
|
||||||
|
* @return The element removed
|
||||||
|
*/
|
||||||
|
public int remove(){
|
||||||
return queueArray[--nItems];
|
return queueArray[--nItems];
|
||||||
}
|
}
|
||||||
|
|
||||||
public int peek(){ //Checks what's at the front of the queue
|
/**
|
||||||
|
* Checks what's at the front of the queue
|
||||||
|
*
|
||||||
|
* @return element at the front of the queue
|
||||||
|
*/
|
||||||
|
public int peek(){
|
||||||
return queueArray[nItems-1];
|
return queueArray[nItems-1];
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isEmpty(){ //Returns true is the queue is empty
|
/**
|
||||||
|
* Returns true if the queue is empty
|
||||||
|
*
|
||||||
|
* @return true if the queue is empty
|
||||||
|
*/
|
||||||
|
public boolean isEmpty(){
|
||||||
return(nItems == 0);
|
return(nItems == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isFull(){ //Returns true is the queue is full
|
/**
|
||||||
|
* Returns true if the queue is full
|
||||||
|
*
|
||||||
|
* @return true if the queue is full
|
||||||
|
*/
|
||||||
|
public boolean isFull(){
|
||||||
return(nItems == maxSize);
|
return(nItems == maxSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSize(){ //Returns the number of elements in the queue
|
/**
|
||||||
|
* Returns the number of elements in the queue
|
||||||
|
*
|
||||||
|
* @return number of elements in the queue
|
||||||
|
*/
|
||||||
|
public int getSize(){
|
||||||
return nItems;
|
return nItems;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Example
|
|
||||||
|
/**
|
||||||
|
* This class implements the PriorityQueue class above.
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class PriorityQueues{
|
public class PriorityQueues{
|
||||||
|
/**
|
||||||
|
* Main method
|
||||||
|
*
|
||||||
|
* @param args Command Line Arguments
|
||||||
|
*/
|
||||||
public static void main(String args[]){
|
public static void main(String args[]){
|
||||||
PriorityQueue myQueue = new PriorityQueue(4);
|
PriorityQueue myQueue = new PriorityQueue(4);
|
||||||
myQueue.insert(10);
|
myQueue.insert(10);
|
||||||
|
@ -1,16 +1,31 @@
|
|||||||
/*
|
/**
|
||||||
|
* This implements Queues by using the class Queue.
|
||||||
|
*
|
||||||
* A queue data structure functions the same as a real world queue.
|
* A queue data structure functions the same as a real world queue.
|
||||||
* The elements that are added first are the first to be removed.
|
* The elements that are added first are the first to be removed.
|
||||||
* New elements are added to the back/rear of the queue.
|
* New elements are added to the back/rear of the queue.
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
class Queue{
|
class Queue{
|
||||||
|
/** Max size of the queue */
|
||||||
private int maxSize;
|
private int maxSize;
|
||||||
|
/** The array representing the queue */
|
||||||
private int[] queueArray;
|
private int[] queueArray;
|
||||||
|
/** Front of the queue */
|
||||||
private int front;
|
private int front;
|
||||||
|
/** Rear of the queue */
|
||||||
private int rear;
|
private int rear;
|
||||||
|
/** How many items are in the queue */
|
||||||
private int nItems;
|
private int nItems;
|
||||||
|
|
||||||
public Queue(int size){ //Constructor
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param size Size of the new queue
|
||||||
|
*/
|
||||||
|
public Queue(int size){
|
||||||
maxSize = size;
|
maxSize = size;
|
||||||
queueArray = new int[size];
|
queueArray = new int[size];
|
||||||
front = 0;
|
front = 0;
|
||||||
@ -18,7 +33,13 @@ class Queue{
|
|||||||
nItems = 0;
|
nItems = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean insert(int x){ //Inserts an element at the rear of the queue
|
/**
|
||||||
|
* Inserts an element at the rear of the queue
|
||||||
|
*
|
||||||
|
* @param x element to be added
|
||||||
|
* @return True if the element was added successfully
|
||||||
|
*/
|
||||||
|
public boolean insert(int x){
|
||||||
if(isFull())
|
if(isFull())
|
||||||
return false;
|
return false;
|
||||||
if(rear == maxSize-1) //If the back of the queue is the end of the array wrap around to the front
|
if(rear == maxSize-1) //If the back of the queue is the end of the array wrap around to the front
|
||||||
@ -29,6 +50,11 @@ class Queue{
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove an element from the front of the queue
|
||||||
|
*
|
||||||
|
* @return the new front of the queue
|
||||||
|
*/
|
||||||
public int remove(){ //Remove an element from the front of the queue
|
public int remove(){ //Remove an element from the front of the queue
|
||||||
if(isEmpty()){
|
if(isEmpty()){
|
||||||
System.out.println("Queue is empty");
|
System.out.println("Queue is empty");
|
||||||
@ -42,28 +68,64 @@ class Queue{
|
|||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int peekFront(){ //Checks what's at the front of the queue
|
/**
|
||||||
|
* Checks what's at the front of the queue
|
||||||
|
*
|
||||||
|
* @return element at the front of the queue
|
||||||
|
*/
|
||||||
|
public int peekFront(){
|
||||||
return queueArray[front];
|
return queueArray[front];
|
||||||
}
|
}
|
||||||
|
|
||||||
public int peekRear(){ //Checks what's at the rear of the queue
|
/**
|
||||||
|
* Checks what's at the rear of the queue
|
||||||
|
*
|
||||||
|
* @return element at the rear of the queue
|
||||||
|
*/
|
||||||
|
public int peekRear(){
|
||||||
return queueArray[rear];
|
return queueArray[rear];
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isEmpty(){ //Returns true is the queue is empty
|
/**
|
||||||
|
* Returns true if the queue is empty
|
||||||
|
*
|
||||||
|
* @return true if the queue is empty
|
||||||
|
*/
|
||||||
|
public boolean isEmpty(){
|
||||||
return(nItems == 0);
|
return(nItems == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isFull(){ //Returns true is the queue is full
|
/**
|
||||||
|
* Returns true if the queue is full
|
||||||
|
*
|
||||||
|
* @return true if the queue is full
|
||||||
|
*/
|
||||||
|
public boolean isFull(){
|
||||||
return(nItems == maxSize);
|
return(nItems == maxSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSize(){ //Returns the number of elements in the queue
|
/**
|
||||||
|
* Returns the number of elements in the queue
|
||||||
|
*
|
||||||
|
* @return number of elements in the queue
|
||||||
|
*/
|
||||||
|
public int getSize(){
|
||||||
return nItems;
|
return nItems;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Example
|
|
||||||
|
/**
|
||||||
|
* This class is the example for the Queue class
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class Queues{
|
public class Queues{
|
||||||
|
/**
|
||||||
|
* Main method
|
||||||
|
*
|
||||||
|
* @param args Command line arguments
|
||||||
|
*/
|
||||||
public static void main(String args[]){
|
public static void main(String args[]){
|
||||||
Queue myQueue = new Queue(4);
|
Queue myQueue = new Queue(4);
|
||||||
myQueue.insert(10);
|
myQueue.insert(10);
|
||||||
|
@ -1,58 +1,78 @@
|
|||||||
/*
|
/**
|
||||||
* A linked list is similar to an array, it holds values. However, links in a linked list do not have indexes.
|
* This class implements a SinglyLinked List. This is done
|
||||||
* With a linked list you do not need to predetermine it's size as it grows and shrinks as it is edited.
|
* using SinglyLinkedList class and a LinkForLinkedList Class.
|
||||||
* This is an example of a singly linked list. Elements can only be added/removed at the head/front of the list.
|
*
|
||||||
*/
|
* A linked list is implar to an array, it hold values.
|
||||||
class LinkedList{
|
* However, links in a linked list do not have indexes. With
|
||||||
private Link head; //Head refers to the front of the list
|
* a linked list you do not need to predetermine it's size as
|
||||||
|
* it gorws and shrinks as it is edited. This is an example of
|
||||||
|
* a singly linked list. Elements can only be added/removed
|
||||||
|
* at the head/front of the list.
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class SinglyLinkedList{
|
||||||
|
/**Head refered to the front of the list */
|
||||||
|
private LinkForLinkedList head;
|
||||||
|
|
||||||
public LinkedList(){
|
/**
|
||||||
|
* Constructor of SinglyLinkedList
|
||||||
|
*/
|
||||||
|
public SinglyLinkedList(){
|
||||||
head = null;
|
head = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void insertHead(int x){ //Insert an element at the head
|
/**
|
||||||
Link newLink = new Link(x); //Create a new link with a value attached to it
|
* This method inserts an element at the head
|
||||||
|
*
|
||||||
|
* @param x Element to be added
|
||||||
|
*/
|
||||||
|
public void insertHead(int x){
|
||||||
|
LinkForLinkedList newLink = new LinkForLinkedList(x); //Create a new link with a value attached to it
|
||||||
newLink.next = head; //Set the new link to point to the current head
|
newLink.next = head; //Set the new link to point to the current head
|
||||||
head = newLink; //Now set the new link to be the head
|
head = newLink; //Now set the new link to be the head
|
||||||
}
|
}
|
||||||
|
|
||||||
public Link deleteHead(){ //Delete the element at the head
|
/**
|
||||||
Link temp = head;
|
* This method deletes an element at the head
|
||||||
|
*
|
||||||
|
* @return The element deleted
|
||||||
|
*/
|
||||||
|
public LinkForLinkedList deleteHead(){
|
||||||
|
LinkForLinkedList temp = head;
|
||||||
head = head.next; //Make the second element in the list the new head, the Java garbage collector will later remove the old head
|
head = head.next; //Make the second element in the list the new head, the Java garbage collector will later remove the old head
|
||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isEmpty(){ //Returns true if list is empty
|
/**
|
||||||
|
* Checks if the list is empty
|
||||||
|
*
|
||||||
|
* @return true is list is empty
|
||||||
|
*/
|
||||||
|
public boolean isEmpty(){
|
||||||
return(head == null);
|
return(head == null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void display(){ //Prints contents of the list
|
/**
|
||||||
Link current = head;
|
* Prints contents of the list
|
||||||
|
*/
|
||||||
|
public void display(){
|
||||||
|
LinkForLinkedList current = head;
|
||||||
while(current!=null){
|
while(current!=null){
|
||||||
current.displayLink();
|
current.displayLink();
|
||||||
current = current.next;
|
current = current.next;
|
||||||
}
|
}
|
||||||
System.out.println();
|
System.out.println();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
class Link{
|
* Main method
|
||||||
public int value;
|
*
|
||||||
public Link next; //This is what the link will point to
|
* @param args Command line arguments
|
||||||
|
*/
|
||||||
public Link(int valuein){
|
|
||||||
value = valuein;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void displayLink(){
|
|
||||||
System.out.print(value+" ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Example
|
|
||||||
public class SinglyLinkedList{
|
|
||||||
public static void main(String args[]){
|
public static void main(String args[]){
|
||||||
LinkedList myList = new LinkedList();
|
SinglyLinkedList myList = new SinglyLinkedList();
|
||||||
|
|
||||||
System.out.println(myList.isEmpty()); //Will print true
|
System.out.println(myList.isEmpty()); //Will print true
|
||||||
|
|
||||||
@ -66,4 +86,36 @@ public class SinglyLinkedList{
|
|||||||
|
|
||||||
myList.display(); // 7(head) --> 5
|
myList.display(); // 7(head) --> 5
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class is the nodes of the SinglyLinked List.
|
||||||
|
* They consist of a vlue and a pointer to the node
|
||||||
|
* after them.
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class LinkForLinkedList{
|
||||||
|
/** The value of the node */
|
||||||
|
public int value;
|
||||||
|
/** Point to the next node */
|
||||||
|
public LinkForLinkedList next; //This is what the link will point to
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param valuein Value to be put in the node
|
||||||
|
*/
|
||||||
|
public LinkForLinkedList(int valuein){
|
||||||
|
value = valuein;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints out the value of the node
|
||||||
|
*/
|
||||||
|
public void displayLink(){
|
||||||
|
System.out.print(value+" ");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,62 +1,131 @@
|
|||||||
/*
|
import java.util.ArrayList;
|
||||||
*A stack is exactly what it sounds like. An element gets added to top of the stack and only the element on the top may be removed.
|
|
||||||
*This is an example of an array implementation of a Stack. So an element can only be added/removed from the end of the array.
|
/**
|
||||||
*In theory stacks have no fixed size, but with an array implementation it does.
|
* This class implements a Stack using two different implementations.
|
||||||
*/
|
* Stack is used with a regular array and Stack2 uses an ArrayList.
|
||||||
|
*
|
||||||
|
* A stack is exactly what it sounds like. An element gets added to the top of
|
||||||
|
* the stack and only the element on the top may be removed. This is an example
|
||||||
|
* of an array implementation of a Stack. So an element can only be added/removed
|
||||||
|
* from the end of the array. In theory stack have no fixed size, but with an
|
||||||
|
* array implementation it does.
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
class Stack{
|
class Stack{
|
||||||
|
/** The max size of the Stack */
|
||||||
private int maxSize;
|
private int maxSize;
|
||||||
|
/** The array representation of the Stack */
|
||||||
private int[] stackArray;
|
private int[] stackArray;
|
||||||
|
/** The top of the stack */
|
||||||
private int top;
|
private int top;
|
||||||
|
|
||||||
public Stack(int size){ //Constructor
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param size Size of the Stack
|
||||||
|
*/
|
||||||
|
public Stack(int size){
|
||||||
maxSize = size;
|
maxSize = size;
|
||||||
stackArray = new int[maxSize];
|
stackArray = new int[maxSize];
|
||||||
top = -1;
|
top = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void push(int value){ //Adds an element to the top of the stack
|
/**
|
||||||
|
* Adds an element to the top of the stack
|
||||||
|
*
|
||||||
|
* @param value The element added
|
||||||
|
*/
|
||||||
|
public void push(int value){
|
||||||
top++;
|
top++;
|
||||||
stackArray[top] = value;
|
stackArray[top] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int pop(){ //Removes the top element of the stack and returns the value you've removed
|
/**
|
||||||
|
* Removes the top element of the stack and returns the value you've removed
|
||||||
|
*
|
||||||
|
* @return value popped off the Stack
|
||||||
|
*/
|
||||||
|
public int pop(){
|
||||||
return stackArray[top--];
|
return stackArray[top--];
|
||||||
}
|
}
|
||||||
|
|
||||||
public int peek(){ //Returns the element at the top of the stack
|
/**
|
||||||
|
* Returns the element at the top of the stack
|
||||||
|
*
|
||||||
|
* @return element at the top of the stack
|
||||||
|
*/
|
||||||
|
public int peek(){
|
||||||
return stackArray[top];
|
return stackArray[top];
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isEmpty(){ //Returns true if the stack is empty
|
/**
|
||||||
|
* Returns true if the stack is empty
|
||||||
|
*
|
||||||
|
* @return true if the stack is empty
|
||||||
|
*/
|
||||||
|
public boolean isEmpty(){
|
||||||
return(top == -1);
|
return(top == -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isFull(){ //Returns true if the stack is full
|
/**
|
||||||
|
* Returns true if the stack is full
|
||||||
|
*
|
||||||
|
* @return true if the stack is full
|
||||||
|
*/
|
||||||
|
public boolean isFull(){
|
||||||
return(top+1 == maxSize);
|
return(top+1 == maxSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes everything in the Stack
|
||||||
|
*
|
||||||
|
* Doesn't delete elements in the array
|
||||||
|
* but if you call push method after calling
|
||||||
|
* makeEmpty it will overwrite previous
|
||||||
|
* values
|
||||||
|
*/
|
||||||
public void makeEmpty(){ //Doesn't delete elements in the array but if you call
|
public void makeEmpty(){ //Doesn't delete elements in the array but if you call
|
||||||
top = -1; //push method after calling makeEmpty it will overwrite previous values
|
top = -1; //push method after calling makeEmpty it will overwrite previous values
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
/* This is ArrayList Implementation of stack , Where size is not
|
* This is an ArrayList Implementation of stack, Where size is not
|
||||||
a problem we can extend the stack as much as we want*/
|
* a problem we can extend the stack as much as we want.
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
class Stack2{
|
class Stack2{
|
||||||
|
/** ArrayList representation of the stack */
|
||||||
|
|
||||||
ArrayList<Integer> stackList;
|
ArrayList<Integer> stackList;
|
||||||
|
|
||||||
Stack2(){ //constructor
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
Stack2(){
|
||||||
stackList=new ArrayList<>();
|
stackList=new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
void push(int value){ //adds value to the end of list which is the top for stack
|
* Adds value to the end of list which
|
||||||
|
* is the top for stack
|
||||||
|
*
|
||||||
|
* @param value value to be added
|
||||||
|
*/
|
||||||
|
void push(int value){
|
||||||
stackList.add(value);
|
stackList.add(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
int pop(){ //pops last element of list which is indeed the top for Stack
|
/**
|
||||||
|
* Pops last element of list which is indeed
|
||||||
|
* the top for Stack
|
||||||
|
*
|
||||||
|
* @return Element popped
|
||||||
|
*/
|
||||||
|
int pop(){
|
||||||
|
|
||||||
if(!isEmpty()){ // checks for an empty Stack
|
if(!isEmpty()){ // checks for an empty Stack
|
||||||
|
|
||||||
@ -71,7 +140,12 @@ class Stack2{
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean isEmpty(){ //checks for empty Stack
|
/**
|
||||||
|
* Checks for empty Stack
|
||||||
|
*
|
||||||
|
* @return true if stack is empty
|
||||||
|
*/
|
||||||
|
boolean isEmpty(){
|
||||||
if(stackList.isEmpty())
|
if(stackList.isEmpty())
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
@ -79,13 +153,28 @@ class Stack2{
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int peek(){ //top element of stack
|
/**
|
||||||
|
* Top element of stack
|
||||||
|
*
|
||||||
|
* @return top element of stack
|
||||||
|
*/
|
||||||
|
int peek(){
|
||||||
return stackList.get(stackList.size()-1);
|
return stackList.get(stackList.size()-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Example
|
/**
|
||||||
|
* This class implements the Stack and Stack2 created above
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class Stacks{
|
public class Stacks{
|
||||||
|
/**
|
||||||
|
* Main method
|
||||||
|
*
|
||||||
|
* @param args Command line arguments
|
||||||
|
*/
|
||||||
public static void main(String args[]){
|
public static void main(String args[]){
|
||||||
Stack myStack = new Stack(4); //Declare a stack of maximum size 4
|
Stack myStack = new Stack(4); //Declare a stack of maximum size 4
|
||||||
//Populate the stack
|
//Populate the stack
|
||||||
|
@ -84,7 +84,13 @@ public class MaxHeap implements Heap {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deleteElement(int elementIndex) {
|
public void deleteElement(int elementIndex) {
|
||||||
if (isempty(maxHeap)) throw new EmptyHeapException("Attempt to delete an element from an empty heap");
|
if (maxHeap.isEmpty())
|
||||||
|
try {
|
||||||
|
throw new EmptyHeapException("Attempt to delete an element from an empty heap");
|
||||||
|
} catch (EmptyHeapException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
if ((elementIndex > maxHeap.size()) && (elementIndex <= 0)) throw new IndexOutOfBoundsException("Index out of heap range");
|
if ((elementIndex > maxHeap.size()) && (elementIndex <= 0)) throw new IndexOutOfBoundsException("Index out of heap range");
|
||||||
// The last element in heap replaces the one to be deleted
|
// The last element in heap replaces the one to be deleted
|
||||||
maxHeap.set(elementIndex - 1, getElement(maxHeap.size()));
|
maxHeap.set(elementIndex - 1, getElement(maxHeap.size()));
|
||||||
|
@ -87,7 +87,13 @@ public class MinHeap implements Heap {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deleteElement(int elementIndex) {
|
public void deleteElement(int elementIndex) {
|
||||||
if (isempty(maxHeap)) throw new EmptyHeapException("Attempt to delete an element from an empty heap");
|
if (minHeap.isEmpty())
|
||||||
|
try {
|
||||||
|
throw new EmptyHeapException("Attempt to delete an element from an empty heap");
|
||||||
|
} catch (EmptyHeapException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
if ((elementIndex > minHeap.size()) && (elementIndex <= 0)) throw new IndexOutOfBoundsException("Index out of heap range");
|
if ((elementIndex > minHeap.size()) && (elementIndex <= 0)) throw new IndexOutOfBoundsException("Index out of heap range");
|
||||||
// The last element in heap replaces the one to be deleted
|
// The last element in heap replaces the one to be deleted
|
||||||
minHeap.set(elementIndex - 1, getElement(minHeap.size()));
|
minHeap.set(elementIndex - 1, getElement(minHeap.size()));
|
||||||
|
22
dfs.java
22
dfs.java
@ -1,8 +1,21 @@
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class App{
|
/**
|
||||||
|
* Implementation of a Depth First Search
|
||||||
|
*
|
||||||
|
* @author Unknown
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class dfs{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implementation in code of a DFS
|
||||||
|
*
|
||||||
|
* @param a structure to be DFS'ed
|
||||||
|
* @param vertices The vertices
|
||||||
|
* @param source The source
|
||||||
|
*/
|
||||||
public static void dfs(byte [][] a,int vertices,int source){ //passing adjacency matrix and no of vertices
|
public static void dfs(byte [][] a,int vertices,int source){ //passing adjacency matrix and no of vertices
|
||||||
byte []b=new byte[vertices]; //flag container containing status of each vertices
|
byte []b=new byte[vertices]; //flag container containing status of each vertices
|
||||||
Arrays.fill(b,(byte)-1); //status initialization
|
Arrays.fill(b,(byte)-1); //status initialization
|
||||||
@ -27,6 +40,11 @@ public class App{
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The main method
|
||||||
|
*
|
||||||
|
* @param args Command line arguments
|
||||||
|
*/
|
||||||
public static void main(String args[]){
|
public static void main(String args[]){
|
||||||
Scanner in=new Scanner(System.in);
|
Scanner in=new Scanner(System.in);
|
||||||
int vertices=in.nextInt(),source=in.nextInt();
|
int vertices=in.nextInt(),source=in.nextInt();
|
||||||
|
Loading…
Reference in New Issue
Block a user