In this commit I have:

Added JavaDoc to every package except for "heaps"
This commit is contained in:
zacharyjones123
2017-04-18 07:57:17 -07:00
parent 94871b7e6a
commit 9411d5be56
29 changed files with 1406 additions and 425 deletions

View File

@@ -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{
/** Data for the node */
public int data;
/** The Node to the left of this one */
public Node left;
/** The Node to the right of this one */
public Node right;
/** The parent of this node */
public Node parent;
/**
* Constructor of Node
*
* @param value Value to put in the node
*/
public Node(int value){
data = value;
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{
/** The root of the Binary Tree */
private Node root;
/**
* Constructor
*/
public Tree(){
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){
Node current = root;
Node last = root;
@@ -37,7 +81,11 @@ class Tree{
return last;
}
//Inserts the given value
/**
* Inserts certain value into the Binary Tree
*
* @param value Value to be inserted
*/
public void put(int value){
Node newNode = new Node(value);
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){
//temp is the node to be deleted
Node temp = find(value);
@@ -84,34 +137,34 @@ class Tree{
//Two children
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
succesor.left = temp.left;
succesor.left.parent = succesor;
successor.left = temp.left;
successor.left.parent = successor;
//If the successor has a right child, the child's grandparent is it's new parent
if(succesor.right != null && succesor.parent != temp){
succesor.right.parent = succesor.parent;
succesor.parent.left = succesor.right;
succesor.right = temp.right;
succesor.right.parent = succesor;
if(successor.right != null && successor.parent != temp){
successor.right.parent = successor.parent;
successor.parent.left = successor.right;
successor.right = temp.right;
successor.right.parent = successor;
}
if(temp == root){
succesor.parent = null;
root = succesor;
successor.parent = null;
root = successor;
return true;
}
//If you're not deleting the root
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
if(temp.parent.data < temp.data)
temp.parent.right = succesor;
temp.parent.right = successor;
else
temp.parent.left = succesor;
temp.parent.left = successor;
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)
return n;
Node current = n.right;
@@ -161,11 +220,20 @@ class Tree{
return parent;
}
/**
* Returns the root of the Binary Tree
*
* @return the root of the Binary Tree
*/
public Node getRoot(){
return root;
}
//Prints leftChild - root - rightChild
/**
* Prints leftChild - root - rightChild
*
* @param localRoot The local root of the binary tree
*/
public void inOrder(Node localRoot){
if(localRoot != null){
inOrder(localRoot.left);
@@ -173,7 +241,12 @@ class Tree{
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){
if(localRoot != null){
System.out.print(localRoot.data + " ");
@@ -181,7 +254,12 @@ class Tree{
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){
if(localRoot != null){
postOrder(localRoot.left);

View File

@@ -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.
* With a linked list you do not need to predetermine it's size as it grows and shrinks as it is edited.
* This is an example of a double ended, doubly linked list.
* Each link references the next link and the previous one.
*/
class LinkedList{
private Link head; //Head refers to the front of the list
private Link tail; //Tail refers to the back of the list
/**
* This class implements a DoublyLinkedList. This is done using the classes
* LinkedList and Link.
*
* 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
* you do not need to predetermine it's size as it grows and shrinks
* as it is edited. This is an example of a double ended, doubly
* 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;
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
if(isEmpty()) //Set the first element added to be the tail
tail = newLink;
@@ -23,6 +42,11 @@ class LinkedList{
head = newLink; // newLink(head) <--> oldhead
}
/**
* Insert an element at the tail
*
* @param x Element to be inserted
*/
public void insertTail(int x){
Link newLink = new Link(x);
newLink.next = null; // currentTail(tail) newlink -->
@@ -31,7 +55,12 @@ class LinkedList{
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;
head = head.next; // oldHead <--> 2ndElement(head)
head.previous = null; // oldHead --> 2ndElement(head) nothing pointing at old head so will be removed
@@ -40,6 +69,11 @@ class LinkedList{
return temp;
}
/**
* Delete the element at the tail
*
* @return The new tail
*/
public Link deleteTail(){
Link temp = tail;
tail = tail.previous; // 2ndLast(tail) <--> oldTail --> null
@@ -47,6 +81,12 @@ class LinkedList{
return temp;
}
/**
* Delete the element from somewhere in the list
*
* @param x element to be deleted
* @return Link deleted
*/
public Link delete(int x){
Link current = head;
@@ -66,6 +106,11 @@ class LinkedList{
return current;
}
/**
* Inserts element and reorders
*
* @param x Element to be added
*/
public void insertOrdered(int x){
Link newLink = new Link(x);
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);
}
/**
* Prints contents of the list
*/
public void display(){ //Prints contents of the list
Link current = head;
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{
/** Value of node */
public int value;
public Link next; //This points to the link in front of the new link
public Link previous; //This points to the link behind the new link
/** This points to the link in front of 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){
this.value = value;
}
/**
* Displays the node
*/
public void displayLink(){
System.out.print(value+" ");
}
}
//Example
public class DoublyLinkedList{
/**
* Main Method
*
* @param args Command line arguments
*/
public static void main(String args[]){
LinkedList myList = new LinkedList();
DoublyLinkedList myList = new DoublyLinkedList();
myList.insertHead(13);
myList.insertHead(7);

View File

@@ -1,21 +1,40 @@
/*
/**
* This class implements a PriorityQueue.
*
* A priority queue adds elements into positions based on their priority.
* 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.
* 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{
/** The max size of the queue */
private int maxSize;
/** The array for the queue */
private int[] queueArray;
/** How many items are in the queue */
private int nItems;
public PriorityQueue(int size){ //Constructor
/**
* Constructor
*
* @param size Size of the queue
*/
public PriorityQueue(int size){
maxSize = size;
queueArray = new int[size];
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){
queueArray[0] = value;
}
@@ -30,28 +49,64 @@ class PriorityQueue{
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];
}
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];
}
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);
}
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);
}
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;
}
}
//Example
/**
* This class implements the PriorityQueue class above.
*
* @author Unknown
*
*/
public class PriorityQueues{
/**
* Main method
*
* @param args Command Line Arguments
*/
public static void main(String args[]){
PriorityQueue myQueue = new PriorityQueue(4);
myQueue.insert(10);

View File

@@ -1,16 +1,31 @@
/*
/**
* This implements Queues by using the class 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.
* 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{
/** Max size of the queue */
private int maxSize;
/** The array representing the queue */
private int[] queueArray;
/** Front of the queue */
private int front;
/** Rear of the queue */
private int rear;
/** How many items are in the queue */
private int nItems;
public Queue(int size){ //Constructor
/**
* Constructor
*
* @param size Size of the new queue
*/
public Queue(int size){
maxSize = size;
queueArray = new int[size];
front = 0;
@@ -18,7 +33,13 @@ class Queue{
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())
return false;
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;
}
/**
* 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
if(isEmpty()){
System.out.println("Queue is empty");
@@ -42,28 +68,64 @@ class Queue{
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];
}
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];
}
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);
}
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);
}
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;
}
}
//Example
/**
* This class is the example for the Queue class
*
* @author Unknown
*
*/
public class Queues{
/**
* Main method
*
* @param args Command line arguments
*/
public static void main(String args[]){
Queue myQueue = new Queue(4);
myQueue.insert(10);

View File

@@ -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.
* With a linked list you do not need to predetermine it's size as it grows 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.
*/
class LinkedList{
private Link head; //Head refers to the front of the list
/**
* This class implements a SinglyLinked List. This is done
* using SinglyLinkedList class and a LinkForLinkedList Class.
*
* A linked list is implar to an array, it hold values.
* However, links in a linked list do not have indexes. With
* 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;
}
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
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
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);
}
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){
current.displayLink();
current = current.next;
}
System.out.println();
}
}
class Link{
public int value;
public Link next; //This is what the link will point to
public Link(int valuein){
value = valuein;
}
public void displayLink(){
System.out.print(value+" ");
}
}
//Example
public class SinglyLinkedList{
/**
* Main method
*
* @param args Command line arguments
*/
public static void main(String args[]){
LinkedList myList = new LinkedList();
SinglyLinkedList myList = new SinglyLinkedList();
System.out.println(myList.isEmpty()); //Will print true
@@ -66,4 +86,36 @@ public class SinglyLinkedList{
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+" ");
}
}

View File

@@ -1,62 +1,131 @@
/*
*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.
*/
import java.util.ArrayList;
/**
* 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{
/** The max size of the Stack */
private int maxSize;
/** The array representation of the Stack */
private int[] stackArray;
/** The top of the stack */
private int top;
public Stack(int size){ //Constructor
/**
* Constructor
*
* @param size Size of the Stack
*/
public Stack(int size){
maxSize = size;
stackArray = new int[maxSize];
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++;
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--];
}
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];
}
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);
}
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);
}
/**
* 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
top = -1; //push method after calling makeEmpty it will overwrite previous values
}
}
/* This is ArrayList Implementation of stack , Where size is not
a problem we can extend the stack as much as we want*/
/**
* This is an ArrayList Implementation of stack, Where size is not
* a problem we can extend the stack as much as we want.
*
* @author Unknown
*
*/
class Stack2{
/** ArrayList representation of the stack */
ArrayList<Integer> stackList;
Stack2(){ //constructor
/**
* Constructor
*/
Stack2(){
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);
}
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
@@ -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())
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);
}
}
//Example
/**
* This class implements the Stack and Stack2 created above
*
* @author Unknown
*
*/
public class Stacks{
/**
* Main method
*
* @param args Command line arguments
*/
public static void main(String args[]){
Stack myStack = new Stack(4); //Declare a stack of maximum size 4
//Populate the stack

View File

@@ -84,7 +84,13 @@ public class MaxHeap implements Heap {
@Override
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");
// The last element in heap replaces the one to be deleted
maxHeap.set(elementIndex - 1, getElement(maxHeap.size()));

View File

@@ -87,7 +87,13 @@ public class MinHeap implements Heap {
@Override
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");
// The last element in heap replaces the one to be deleted
minHeap.set(elementIndex - 1, getElement(minHeap.size()));