mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-15 01:40:49 +08:00
Merge pull request #1325 from mariaRoxana94/fix-error
Fixed 8XCorrectness_Bugs + 10XBad_Practice_Bugs + 14XDodgy_Code_Bugs
This commit is contained in:
@ -41,7 +41,7 @@ public class DynamicArray<E> implements Iterable<E> {
|
||||
}
|
||||
|
||||
public void put(final int index, E element) {
|
||||
Objects.checkIndex(index, this.size);
|
||||
// Objects.checkIndex(index, this.size);
|
||||
|
||||
this.elements[index] = element;
|
||||
}
|
||||
@ -79,7 +79,7 @@ public class DynamicArray<E> implements Iterable<E> {
|
||||
}
|
||||
|
||||
private E getElement(final int index) {
|
||||
Objects.checkIndex(index, this.size);
|
||||
// Objects.checkIndex(index, this.size);
|
||||
return (E) this.elements[index];
|
||||
}
|
||||
|
||||
|
@ -23,7 +23,7 @@ start vertex, end vertes and weights. Vertices should be labelled with a number
|
||||
* @param v End vertex
|
||||
* @param c Weight
|
||||
*/
|
||||
Edge(int a,int b,int c)
|
||||
public Edge(int a,int b,int c)
|
||||
{
|
||||
u=a;
|
||||
v=b;
|
||||
|
@ -127,8 +127,7 @@ class AdjacencyMatrixGraph {
|
||||
* @return returns a string describing this graph
|
||||
*/
|
||||
public String toString() {
|
||||
String s = new String();
|
||||
s = " ";
|
||||
String s = " ";
|
||||
for (int i = 0; i < this.numberOfVertices(); i++) {
|
||||
s = s + String.valueOf(i) + " ";
|
||||
}
|
||||
|
@ -117,7 +117,21 @@ public class HeapElement {
|
||||
* @return true if the keys on both elements are identical and the additional info objects
|
||||
* are identical.
|
||||
*/
|
||||
public boolean equals(HeapElement otherHeapElement) {
|
||||
return (this.key == otherHeapElement.key) && (this.additionalInfo.equals(otherHeapElement.additionalInfo));
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o != null) {
|
||||
if (!(o instanceof HeapElement)) return false;
|
||||
HeapElement otherHeapElement = (HeapElement) o;
|
||||
return (this.key == otherHeapElement.key) && (this.additionalInfo.equals(otherHeapElement.additionalInfo));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = 0;
|
||||
result = 31*result + (int) key;
|
||||
result = 31*result + (additionalInfo != null ? additionalInfo.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -49,9 +49,9 @@ public class MaxHeap implements Heap {
|
||||
// Toggle an element up to its right place as long as its key is lower than its parent's
|
||||
private void toggleUp(int elementIndex) {
|
||||
double key = maxHeap.get(elementIndex - 1).getKey();
|
||||
while (getElementKey((int) Math.floor(elementIndex / 2)) < key) {
|
||||
swap(elementIndex, (int) Math.floor(elementIndex / 2));
|
||||
elementIndex = (int) Math.floor(elementIndex / 2);
|
||||
while (getElementKey((int) Math.floor(elementIndex / 2.0)) < key) {
|
||||
swap(elementIndex, (int) Math.floor(elementIndex / 2.0));
|
||||
elementIndex = (int) Math.floor(elementIndex / 2.0);
|
||||
}
|
||||
}
|
||||
|
||||
@ -101,7 +101,7 @@ public class MaxHeap implements Heap {
|
||||
maxHeap.set(elementIndex - 1, getElement(maxHeap.size()));
|
||||
maxHeap.remove(maxHeap.size());
|
||||
// Shall the new element be moved up...
|
||||
if (getElementKey(elementIndex) > getElementKey((int) Math.floor(elementIndex / 2))) toggleUp(elementIndex);
|
||||
if (getElementKey(elementIndex) > getElementKey((int) Math.floor(elementIndex / 2.0))) toggleUp(elementIndex);
|
||||
// ... or down ?
|
||||
else if (((2 * elementIndex <= maxHeap.size()) && (getElementKey(elementIndex) < getElementKey(elementIndex * 2))) ||
|
||||
((2 * elementIndex < maxHeap.size()) && (getElementKey(elementIndex) < getElementKey(elementIndex * 2))))
|
||||
|
@ -44,9 +44,9 @@ public class MinHeap implements Heap {
|
||||
// Toggle an element up to its right place as long as its key is lower than its parent's
|
||||
private void toggleUp(int elementIndex) {
|
||||
double key = minHeap.get(elementIndex - 1).getKey();
|
||||
while (getElementKey((int) Math.floor(elementIndex / 2)) > key) {
|
||||
swap(elementIndex, (int) Math.floor(elementIndex / 2));
|
||||
elementIndex = (int) Math.floor(elementIndex / 2);
|
||||
while (getElementKey((int) Math.floor(elementIndex / 2.0)) > key) {
|
||||
swap(elementIndex, (int) Math.floor(elementIndex / 2.0));
|
||||
elementIndex = (int) Math.floor(elementIndex / 2.0);
|
||||
}
|
||||
}
|
||||
|
||||
@ -96,7 +96,7 @@ public class MinHeap implements Heap {
|
||||
minHeap.set(elementIndex - 1, getElement(minHeap.size()));
|
||||
minHeap.remove(minHeap.size());
|
||||
// Shall the new element be moved up...
|
||||
if (getElementKey(elementIndex) < getElementKey((int) Math.floor(elementIndex / 2))) toggleUp(elementIndex);
|
||||
if (getElementKey(elementIndex) < getElementKey((int)Math.floor(elementIndex / 2.0))) toggleUp(elementIndex);
|
||||
// ... or down ?
|
||||
else if (((2 * elementIndex <= minHeap.size()) && (getElementKey(elementIndex) > getElementKey(elementIndex * 2))) ||
|
||||
((2 * elementIndex < minHeap.size()) && (getElementKey(elementIndex) > getElementKey(elementIndex * 2))))
|
||||
|
@ -14,7 +14,7 @@ public class CircleLinkedList<E> {
|
||||
//For better O.O design this should be private allows for better black box design
|
||||
private int size;
|
||||
//this will point to dummy node;
|
||||
private Node<E> head;
|
||||
private Node<E> head = null;
|
||||
|
||||
//constructer for class.. here we will make a dummy node for circly linked list implementation with reduced error catching as our list will never be empty;
|
||||
public CircleLinkedList() {
|
||||
|
@ -86,9 +86,12 @@ public class DoublyLinkedList {
|
||||
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
|
||||
if (head == null)
|
||||
|
||||
if (head == null) {
|
||||
tail = null;
|
||||
} else {
|
||||
head.previous = null; // oldHead --> 2ndElement(head) nothing pointing at old head so will be removed
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
@ -100,10 +103,13 @@ public class DoublyLinkedList {
|
||||
public Link deleteTail() {
|
||||
Link temp = tail;
|
||||
tail = tail.previous; // 2ndLast(tail) <--> oldTail --> null
|
||||
tail.next = null; // 2ndLast(tail) --> null
|
||||
|
||||
if (tail == null) {
|
||||
head = null;
|
||||
} else{
|
||||
tail.next = null; // 2ndLast(tail) --> null
|
||||
}
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
|
@ -74,7 +74,7 @@ public class NodeStack<Item> {
|
||||
} else {
|
||||
newNs.setPrevious(NodeStack.head);
|
||||
NodeStack.head.setNext(newNs);
|
||||
NodeStack.head = newNs;
|
||||
NodeStack.head.setHead(newNs);
|
||||
}
|
||||
|
||||
NodeStack.setSize(NodeStack.getSize() + 1);
|
||||
@ -89,7 +89,7 @@ public class NodeStack<Item> {
|
||||
|
||||
Item item = (Item) NodeStack.head.getData();
|
||||
|
||||
NodeStack.head = NodeStack.head.getPrevious();
|
||||
NodeStack.head.setHead(NodeStack.head.getPrevious());
|
||||
NodeStack.head.setNext(null);
|
||||
|
||||
NodeStack.setSize(NodeStack.getSize() - 1);
|
||||
|
@ -15,8 +15,8 @@ public class LevelOrderTraversal {
|
||||
// Root of the Binary Tree
|
||||
Node root;
|
||||
|
||||
public LevelOrderTraversal() {
|
||||
root = null;
|
||||
public LevelOrderTraversal( Node root) {
|
||||
this.root = root;
|
||||
}
|
||||
|
||||
/* function to print level order traversal of tree*/
|
||||
|
@ -19,11 +19,9 @@ public class LevelOrderTraversalQueue {
|
||||
}
|
||||
}
|
||||
|
||||
Node root;
|
||||
|
||||
/* Given a binary tree. Print its nodes in level order
|
||||
using array for implementing queue */
|
||||
void printLevelOrder() {
|
||||
void printLevelOrder(Node root) {
|
||||
Queue<Node> queue = new LinkedList<Node>();
|
||||
queue.add(root);
|
||||
while (!queue.isEmpty()) {
|
||||
|
@ -13,14 +13,13 @@ public class ValidBSTOrNot {
|
||||
}
|
||||
|
||||
//Root of the Binary Tree
|
||||
Node root;
|
||||
|
||||
/* can give min and max value according to your code or
|
||||
can write a function to find min and max value of tree. */
|
||||
|
||||
/* returns true if given search tree is binary
|
||||
search tree (efficient version) */
|
||||
boolean isBST() {
|
||||
boolean isBST(Node root) {
|
||||
return isBSTUtil(root, Integer.MIN_VALUE,
|
||||
Integer.MAX_VALUE);
|
||||
}
|
||||
|
Reference in New Issue
Block a user