Skip to content

Commit

Permalink
final
Browse files Browse the repository at this point in the history
  • Loading branch information
teetri committed Apr 30, 2024
1 parent 565dffb commit 0dbd1e8
Show file tree
Hide file tree
Showing 181 changed files with 6,133 additions and 33 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file modified exam-prep/.DS_Store
Binary file not shown.
Binary file added exam-prep/final-heap/.DS_Store
Binary file not shown.
7 changes: 7 additions & 0 deletions exam-prep/final-heap/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions exam-prep/final-heap/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions exam-prep/final-heap/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>final-heap</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>
96 changes: 96 additions & 0 deletions exam-prep/final-heap/src/Heap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
public class Heap {
int[] mData;
int size = 0;

public Heap() {
final int DEFAULT_CAPACITY = 11;
mData = new int[DEFAULT_CAPACITY];
} // default constructor

public boolean isEmpty() {
return size == 0;
}

public void add(int element) {
if (++size == mData.length) {
int[] newHeap = new int[2 * mData.length];
System.arraycopy(mData, 0, newHeap, 0, size);
mData = newHeap;
}
mData[size - 1] = element;
percolateUp();

// modified part is to be written below

if (size <= 1) {
}
else if (mData[size - 1] < mData[size - 2]) {
int temp = mData[size - 2];
mData[size - 2] = mData[size -1];
size--;
percolateUp();
size++;
mData[size - 1] = temp;
}




}

protected void percolateUp() {
int parent;
int child = size - 1;
int temp;
while (child > 0) {
parent = (child - 1) / 2;
if (mData[parent] <= mData[child])
break;
temp = mData[parent];
mData[parent] = mData[child];
mData[child] = temp;
child = parent;
}
}

public int top() throws Exception {
if (size == 0)
throw new Exception("Empty");
return mData[0];
}

// never get called in our program
public int pop() throws Exception {
if (size == 0)
throw new Exception("Priority queue empty.");
int minElem = mData[0];
mData[0] = mData[size - 1];
size--;
percolateDown(0);
return minElem;
}

protected void percolateDown(int start) {
int parent = start;
int child = 2 * parent + 1;
int temp;
while (child < size) {
if (child < size - 1 && mData[child] > mData[child + 1])
child++;
if (mData[parent] <= mData[child])
break;
temp = mData[child];
mData[child] = mData[parent];
mData[parent] = temp;
parent = child;
child = 2 * parent + 1;
}
}

public int size() {
// TODO Auto-generated method stub
return size;
}


}
167 changes: 167 additions & 0 deletions exam-prep/final-heap/src/TestHeap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class TestHeap {
Heap h;

@BeforeEach
void setUp() throws Exception {
h = new Heap();
}

@Test
void testAddSimple() {
h.add(1);
h.add(9);
h.add(8);
assertEquals(3,h.size);
assertEquals(1,h.mData[0]);
assertEquals(8,h.mData[1]);
assertEquals(9,h.mData[2]);

}

@Test
void testAdd2() {
h.add(1);
h.add(9);
h.add(8);

h.add(7);
assertEquals(4,h.size);
assertEquals(1,h.mData[0]);
assertEquals(7,h.mData[1]);
assertEquals(8,h.mData[2]);
assertEquals(9,h.mData[3]);

}

@Test
void testAdd3() {
h.add(1);
h.add(9);
h.add(8);
h.add(7);

h.add(10);
assertEquals(5,h.size);
assertEquals(1,h.mData[0]);
assertEquals(7,h.mData[1]);
assertEquals(8,h.mData[2]);
assertEquals(9,h.mData[3]);
assertEquals(10,h.mData[4]);

h.add(0);
assertEquals(6,h.size);
assertEquals(0,h.mData[0]);
assertEquals(7,h.mData[1]);
assertEquals(1,h.mData[2]);
assertEquals(9,h.mData[3]);
assertEquals(8,h.mData[4]);
assertEquals(10,h.mData[5]);

}

@Test
void testAdd4() {
h.add(1);
h.add(9);
h.add(8);
h.add(7);
h.add(10);
h.add(0);

h.add(-5);
assertEquals(7,h.size);
assertEquals(-5,h.mData[0]);
assertEquals(7,h.mData[1]);
assertEquals(0,h.mData[2]);
assertEquals(9,h.mData[3]);
assertEquals(8,h.mData[4]);
assertEquals(1,h.mData[5]);
assertEquals(10,h.mData[6]);

}

@Test
void testAdd5() {
h.add(1);
h.add(9);
h.add(8);
h.add(7);
h.add(10);
h.add(0);
h.add(-5);

h.add(15);
assertEquals(8,h.size);
assertEquals(-5,h.mData[0]);
assertEquals(7,h.mData[1]);
assertEquals(0,h.mData[2]);
assertEquals(9,h.mData[3]);
assertEquals(8,h.mData[4]);
assertEquals(1,h.mData[5]);
assertEquals(10,h.mData[6]);
assertEquals(15,h.mData[7]);

h.add(-1);
assertEquals(9,h.size);
assertEquals(-5,h.mData[0]);
assertEquals(-1,h.mData[1]);
assertEquals(0,h.mData[2]);
assertEquals(7,h.mData[3]);
assertEquals(8,h.mData[4]);
assertEquals(1,h.mData[5]);
assertEquals(10,h.mData[6]);
assertEquals(9,h.mData[7]);
assertEquals(15,h.mData[8]);

h.add(11);
assertEquals(10,h.size);
assertEquals(-5,h.mData[0]);
assertEquals(-1,h.mData[1]);
assertEquals(0,h.mData[2]);
assertEquals(7,h.mData[3]);
assertEquals(8,h.mData[4]);
assertEquals(1,h.mData[5]);
assertEquals(10,h.mData[6]);
assertEquals(9,h.mData[7]);
assertEquals(11,h.mData[8]);
assertEquals(15,h.mData[9]);

h.add(2);
assertEquals(11,h.size);
assertEquals(-5,h.mData[0]);
assertEquals(-1,h.mData[1]);
assertEquals(0,h.mData[2]);
assertEquals(7,h.mData[3]);
assertEquals(2,h.mData[4]);
assertEquals(1,h.mData[5]);
assertEquals(10,h.mData[6]);
assertEquals(9,h.mData[7]);
assertEquals(11,h.mData[8]);
assertEquals(8,h.mData[9]);
assertEquals(15,h.mData[10]);

h.add(-2);
assertEquals(12,h.size);
assertEquals(-5,h.mData[0]);
assertEquals(-1,h.mData[1]);
assertEquals(-2,h.mData[2]);
assertEquals(7,h.mData[3]);
assertEquals(1,h.mData[4]);
assertEquals(0,h.mData[5]);
assertEquals(10,h.mData[6]);
assertEquals(9,h.mData[7]);
assertEquals(11,h.mData[8]);
assertEquals(8,h.mData[9]);
assertEquals(2,h.mData[10]);
assertEquals(15,h.mData[11]);

}



}
Binary file modified hw/.DS_Store
Binary file not shown.
Binary file added hw/hw-10/.DS_Store
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@

public class DoubleHashing extends OpenAddressing {

static double MAXFACTOR = 0.75;
int occupiedSlots = 0;

public DoubleHashing() {
this(DEFAULT_SIZE);
}

public DoubleHashing(int size) {
super(size);
}

public int hash(int data) {
return data % array.length;
}

public int hash2(int x) {
return 5 - (x % 5);
}

public int find(int data) {
final int smallNum = 5;
int h = hash(data);
int hash2Result = hash2(data);
for (int i = 0; i < currentSize + smallNum; i++) {
if (array[h] == 0 || array[h] == data)
return h;
h = (h + hash2Result) % array.length;
}
return -1;
}

public void add(int data) throws Exception {
int h = hash(data);
int hash2Result = hash2(data);
int emptySlotPosition = -1;
int i;
final int smallNum = 5; // a small threshold
for (i = 0; i < currentSize + smallNum; i++) {
if (array[h] == 0 || array[h] == data)
break;
if (array[h] == DELETED && emptySlotPosition == -1) {
emptySlotPosition = h;
}
h = (h + hash2Result) % array.length;
}
if (i >= currentSize + smallNum) {
rehash();
add(data);
} else {
if (array[h] == 0) {
if (emptySlotPosition != -1) {
array[emptySlotPosition] = data;
} else {
array[h] = data;
occupiedSlots++;
}
currentSize++;
double currentLoadFactor = (double) (occupiedSlots / array.length);
if (currentLoadFactor > MAXFACTOR)
rehash();
}
}
}

public void rehash() throws Exception {
int[] oldArray = array;
array = new int[nextPrime(array.length * 2)];
currentSize = 0;
occupiedSlots = 0;
for (int i = 0; i < oldArray.length; i++) {
if (oldArray[i] != 0 && oldArray[i] != DELETED)
add(oldArray[i]);
}
}

public void remove(int data){
int index = find(data);
if(index != -1 && array[index]!=0){
array[index] = DELETED;
currentSize--;
}
}


}
Loading

0 comments on commit 0dbd1e8

Please sign in to comment.