diff --git a/.DS_Store b/.DS_Store
index d3c43b0..bea6557 100644
Binary files a/.DS_Store and b/.DS_Store differ
diff --git a/exam-prep/.DS_Store b/exam-prep/.DS_Store
index 67ebdf5..eb266e1 100644
Binary files a/exam-prep/.DS_Store and b/exam-prep/.DS_Store differ
diff --git a/exam-prep/final-heap/.DS_Store b/exam-prep/final-heap/.DS_Store
new file mode 100644
index 0000000..f2e99a9
Binary files /dev/null and b/exam-prep/final-heap/.DS_Store differ
diff --git a/exam-prep/final-heap/.classpath b/exam-prep/final-heap/.classpath
new file mode 100644
index 0000000..6220d0d
--- /dev/null
+++ b/exam-prep/final-heap/.classpath
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
diff --git a/exam-prep/final-heap/.gitignore b/exam-prep/final-heap/.gitignore
new file mode 100644
index 0000000..ae3c172
--- /dev/null
+++ b/exam-prep/final-heap/.gitignore
@@ -0,0 +1 @@
+/bin/
diff --git a/exam-prep/final-heap/.project b/exam-prep/final-heap/.project
new file mode 100644
index 0000000..5f06afd
--- /dev/null
+++ b/exam-prep/final-heap/.project
@@ -0,0 +1,17 @@
+
+
+ final-heap
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/exam-prep/final-heap/src/Heap.java b/exam-prep/final-heap/src/Heap.java
new file mode 100644
index 0000000..cee9c60
--- /dev/null
+++ b/exam-prep/final-heap/src/Heap.java
@@ -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;
+ }
+
+
+}
diff --git a/exam-prep/final-heap/src/TestHeap.java b/exam-prep/final-heap/src/TestHeap.java
new file mode 100644
index 0000000..c65aeea
--- /dev/null
+++ b/exam-prep/final-heap/src/TestHeap.java
@@ -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]);
+
+ }
+
+
+
+}
diff --git a/hw/.DS_Store b/hw/.DS_Store
index b785519..5da61d6 100644
Binary files a/hw/.DS_Store and b/hw/.DS_Store differ
diff --git a/hw/hw-10/.DS_Store b/hw/hw-10/.DS_Store
new file mode 100644
index 0000000..b9d481c
Binary files /dev/null and b/hw/hw-10/.DS_Store differ
diff --git a/hw/hw-10/HW10_Hash_toStudent_65f951214bb82/.DS_Store b/hw/hw-10/HW10_Hash_toStudent_65f951214bb82/.DS_Store
new file mode 100644
index 0000000..12cb557
Binary files /dev/null and b/hw/hw-10/HW10_Hash_toStudent_65f951214bb82/.DS_Store differ
diff --git a/hw/hw-10/HW10_Hash_toStudent_65f951214bb82/HW10_Hash.docx b/hw/hw-10/HW10_Hash_toStudent_65f951214bb82/HW10_Hash.docx
new file mode 100644
index 0000000..f370574
Binary files /dev/null and b/hw/hw-10/HW10_Hash_toStudent_65f951214bb82/HW10_Hash.docx differ
diff --git a/hw/hw-10/HW10_Hash_toStudent_65f951214bb82/HW10_Hash.pdf b/hw/hw-10/HW10_Hash_toStudent_65f951214bb82/HW10_Hash.pdf
new file mode 100644
index 0000000..14ce94f
Binary files /dev/null and b/hw/hw-10/HW10_Hash_toStudent_65f951214bb82/HW10_Hash.pdf differ
diff --git a/hw/hw-10/HW10_Hash_toStudent_65f951214bb82/HW_10_Student/DoubleHashing.java b/hw/hw-10/HW10_Hash_toStudent_65f951214bb82/HW_10_Student/DoubleHashing.java
new file mode 100644
index 0000000..7e05269
--- /dev/null
+++ b/hw/hw-10/HW10_Hash_toStudent_65f951214bb82/HW_10_Student/DoubleHashing.java
@@ -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--;
+ }
+ }
+
+
+}
diff --git a/hw/hw-10/HW10_Hash_toStudent_65f951214bb82/HW_10_Student/HashIterator.java b/hw/hw-10/HW10_Hash_toStudent_65f951214bb82/HW_10_Student/HashIterator.java
new file mode 100644
index 0000000..55b5895
--- /dev/null
+++ b/hw/hw-10/HW10_Hash_toStudent_65f951214bb82/HW_10_Student/HashIterator.java
@@ -0,0 +1,58 @@
+
+public class HashIterator implements Iterator {
+
+ OpenAddressing h; // the associated hash table
+ int currentPos; // position in the table's array that is currently marked.
+
+
+ //Create an iterator that marks the leftmost actual data in the hash table.
+ //Assume actual data are not 0 and DELETED.
+ //If there are no actual data in the table, set currentPos to -1.
+ public HashIterator(OpenAddressing o) {
+ h = o;
+ int i = 0;
+ for (; i < o.array.length; i++) {
+ if (o.array[i] != 0 && o.array[i] != OpenAddressing.DELETED) {
+ currentPos = i;
+ break;
+ }
+ }
+ if (i >= o.array.length) {
+ currentPos = -1;
+ }
+ }
+
+ @Override
+ public boolean hasNext() {
+
+
+
+ }
+
+ @Override
+ public boolean hasPrevious() {
+
+
+ }
+
+ @Override
+ public int next() throws Exception {
+
+
+
+ }
+
+ @Override
+ public int previous() throws Exception {
+
+
+ }
+
+ @Override
+ public void set(int value) {
+ // does not do anything,
+ //because it will break hash table definition
+
+ }
+
+}
diff --git a/hw/hw-10/HW10_Hash_toStudent_65f951214bb82/HW_10_Student/Iterator.java b/hw/hw-10/HW10_Hash_toStudent_65f951214bb82/HW_10_Student/Iterator.java
new file mode 100644
index 0000000..98ff400
--- /dev/null
+++ b/hw/hw-10/HW10_Hash_toStudent_65f951214bb82/HW_10_Student/Iterator.java
@@ -0,0 +1,19 @@
+public interface Iterator {
+
+ public boolean hasNext();
+
+ public boolean hasPrevious();
+
+ // Move iterator to the next position,
+ // then returns the value at that position.
+ //throw exception if it cannot go to that position.
+ public int next() throws Exception;
+
+ // Return the value at current position,
+ // then move the iterator back one position.
+ //throw exception if it cannot go to that position.
+ public int previous() throws Exception;
+
+ public void set(int value);
+
+}
diff --git a/hw/hw-10/HW10_Hash_toStudent_65f951214bb82/HW_10_Student/OpenAddressing.java b/hw/hw-10/HW10_Hash_toStudent_65f951214bb82/HW_10_Student/OpenAddressing.java
new file mode 100644
index 0000000..574d522
--- /dev/null
+++ b/hw/hw-10/HW10_Hash_toStudent_65f951214bb82/HW_10_Student/OpenAddressing.java
@@ -0,0 +1,38 @@
+
+public class OpenAddressing {
+ protected static int DEFAULT_SIZE = 11;
+ protected static final int DELETED = -9999;
+ protected static double MAXFACTOR = 0.5;
+ protected int currentSize = 0;
+ protected int[] array;
+
+ protected static boolean isPrime(int n) {
+ if (n == 2 || n == 3)
+ return true;
+ if (n == 1 || n % 2 == 0)
+ return false;
+ for (int i = 3; i * i <= n; i += 2)
+ if (n % i == 0)
+ return false;
+ return true;
+ }
+
+ protected static int nextPrime(int n) {
+ if (n % 2 == 0)
+ n++;
+ for (; !isPrime(n); n += 2) {
+ }
+ return n;
+ }
+
+ public OpenAddressing() {
+ this(DEFAULT_SIZE);
+ }
+
+ public OpenAddressing(int size) {
+ int nextPrimeSize = nextPrime(size);
+ array = new int[nextPrimeSize];
+ }
+
+
+}
diff --git a/hw/hw-10/HW10_Hash_toStudent_65f951214bb82/HW_10_Student/TestHash.java b/hw/hw-10/HW10_Hash_toStudent_65f951214bb82/HW_10_Student/TestHash.java
new file mode 100644
index 0000000..71a3e3e
--- /dev/null
+++ b/hw/hw-10/HW10_Hash_toStudent_65f951214bb82/HW_10_Student/TestHash.java
@@ -0,0 +1,103 @@
+import static org.junit.jupiter.api.Assertions.*;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class TestHash {
+
+ DoubleHashing hTable;
+
+ @BeforeEach
+ void setUp() throws Exception {
+ hTable = new DoubleHashing();
+
+ }
+
+ @Test
+ void testNextAndHasNextEmptyTable() throws Exception { //1 point
+ HashIterator i = new HashIterator(hTable);
+ assertFalse(i.hasNext());
+ Assertions.assertThrows(Exception.class, () -> {
+ i.next();
+ });
+ }
+
+ @Test
+ void testPreviousAndHasPreviousEmptyTable() throws Exception { //1 point
+ HashIterator i = new HashIterator(hTable);
+ assertFalse(i.hasPrevious());
+ Assertions.assertThrows(Exception.class, () -> {
+ i.previous();
+ });
+ }
+
+ @Test
+ void testNextAndHasNext() throws Exception { //4 points
+ hTable.add(5);
+ hTable.add(16);
+ hTable.add(27);
+ hTable.add(9);
+ hTable.remove(16);
+ hTable.add(20);
+ hTable.add(16);
+
+ HashIterator i = new HashIterator(hTable);
+ assertEquals(16, hTable.array[i.currentPos]);
+
+ assertTrue(i.hasNext());
+ assertEquals(5, i.next());
+
+ assertTrue(i.hasNext());
+ assertEquals(27, i.next());
+
+ assertTrue(i.hasNext());
+ assertEquals(20, i.next());
+
+ assertTrue(i.hasNext());
+ assertEquals(9, i.next());
+
+ assertFalse(i.hasNext());
+ Assertions.assertThrows(Exception.class, () -> {
+ i.next();
+ });
+
+ }
+
+ @Test
+ void testPreviousAndHasPrevious() throws Exception { //4 points
+ hTable.add(5);
+ hTable.add(16);
+ hTable.add(27);
+ hTable.add(9);
+ hTable.remove(16);
+ hTable.add(20);
+ hTable.add(16);
+
+ HashIterator i = new HashIterator(hTable);
+ i.next();
+ i.next();
+ i.next();
+ i.next();
+
+ assertEquals(9, hTable.array[i.currentPos]);
+ assertTrue(i.hasPrevious());
+ assertEquals(9, i.previous());
+
+ assertTrue(i.hasPrevious());
+ assertEquals(20, i.previous());
+
+ assertTrue(i.hasPrevious());
+ assertEquals(27, i.previous());
+
+ assertTrue(i.hasPrevious());
+ assertEquals(5, i.previous());
+
+ assertFalse(i.hasPrevious());
+ Assertions.assertThrows(Exception.class, () -> {
+ i.previous();
+ });
+
+ }
+
+}
diff --git a/hw/hw-10/hw-10/.DS_Store b/hw/hw-10/hw-10/.DS_Store
new file mode 100644
index 0000000..118af64
Binary files /dev/null and b/hw/hw-10/hw-10/.DS_Store differ
diff --git a/hw/hw-10/hw-10/.classpath b/hw/hw-10/hw-10/.classpath
new file mode 100644
index 0000000..86473c8
--- /dev/null
+++ b/hw/hw-10/hw-10/.classpath
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/hw/hw-10/hw-10/.gitignore b/hw/hw-10/hw-10/.gitignore
new file mode 100644
index 0000000..ae3c172
--- /dev/null
+++ b/hw/hw-10/hw-10/.gitignore
@@ -0,0 +1 @@
+/bin/
diff --git a/hw/hw-10/hw-10/.project b/hw/hw-10/hw-10/.project
new file mode 100644
index 0000000..de869bb
--- /dev/null
+++ b/hw/hw-10/hw-10/.project
@@ -0,0 +1,17 @@
+
+
+ hw-10
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/hw/hw-10/hw-10/.settings/org.eclipse.core.resources.prefs b/hw/hw-10/hw-10/.settings/org.eclipse.core.resources.prefs
new file mode 100644
index 0000000..99f26c0
--- /dev/null
+++ b/hw/hw-10/hw-10/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,2 @@
+eclipse.preferences.version=1
+encoding/=UTF-8
diff --git a/hw/hw-10/hw-10/.settings/org.eclipse.jdt.core.prefs b/hw/hw-10/hw-10/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..ccfd559
--- /dev/null
+++ b/hw/hw-10/hw-10/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,14 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=20
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=20
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
+org.eclipse.jdt.core.compiler.release=enabled
+org.eclipse.jdt.core.compiler.source=20
diff --git a/hw/hw-10/hw-10/src/DoubleHashing.java b/hw/hw-10/hw-10/src/DoubleHashing.java
new file mode 100644
index 0000000..7e05269
--- /dev/null
+++ b/hw/hw-10/hw-10/src/DoubleHashing.java
@@ -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--;
+ }
+ }
+
+
+}
diff --git a/hw/hw-10/hw-10/src/HashIterator.java b/hw/hw-10/hw-10/src/HashIterator.java
new file mode 100644
index 0000000..dca1fb7
--- /dev/null
+++ b/hw/hw-10/hw-10/src/HashIterator.java
@@ -0,0 +1,88 @@
+
+public class HashIterator implements Iterator {
+
+ OpenAddressing h; // the associated hash table
+ int currentPos; // position in the table's array that is currently marked.
+
+
+ //Create an iterator that marks the leftmost actual data in the hash table.
+ //Assume actual data are not 0 and DELETED.
+ //If there are no actual data in the table, set currentPos to -1.
+ public HashIterator(OpenAddressing o) {
+ h = o;
+ int i = 0;
+ for (; i < o.array.length; i++) {
+ if (o.array[i] != 0 && o.array[i] != OpenAddressing.DELETED) {
+ currentPos = i;
+ break;
+ }
+ }
+ if (i >= o.array.length) {
+ currentPos = -1;
+ }
+ }
+
+ @Override
+ public boolean hasNext() {
+ int i = currentPos+1;
+ for (; i < h.array.length; i++) {
+ if (h.array[i] != 0 && h.array[i] != OpenAddressing.DELETED) {
+ return true;
+ }
+ }
+ if (i >= h.array.length) {
+ return false;
+ }
+ return false;
+ }
+
+ @Override
+ public boolean hasPrevious() {
+ int i = currentPos-1;
+ for (; i >= 0; i--) {
+ if (h.array[i] != 0 && h.array[i] != OpenAddressing.DELETED) {
+ return true;
+ }
+ }
+ if (i < 0) {
+ return false;
+ }
+ return false;
+ }
+
+ @Override
+ public int next() throws Exception {
+ if(!hasNext()) throw new Exception();
+ int i = currentPos+1;
+ for (; i < h.array.length; i++) {
+ if (h.array[i] != 0 && h.array[i] != OpenAddressing.DELETED) {
+ currentPos = i;
+ return h.array[currentPos];
+ }
+ }
+ return -1;
+
+ }
+
+ @Override
+ public int previous() throws Exception {
+ if(!hasPrevious()) throw new Exception();
+ int i = currentPos-1;
+ for (; i >= 0; i--) {
+ if (h.array[i] != 0 && h.array[i] != OpenAddressing.DELETED) {
+ int ret = h.array[currentPos];
+ currentPos = i;
+ return ret;
+ }
+ }
+ return -1;
+ }
+
+ @Override
+ public void set(int value) {
+ // does not do anything,
+ //because it will break hash table definition
+
+ }
+
+}
diff --git a/hw/hw-10/hw-10/src/Iterator.java b/hw/hw-10/hw-10/src/Iterator.java
new file mode 100644
index 0000000..98ff400
--- /dev/null
+++ b/hw/hw-10/hw-10/src/Iterator.java
@@ -0,0 +1,19 @@
+public interface Iterator {
+
+ public boolean hasNext();
+
+ public boolean hasPrevious();
+
+ // Move iterator to the next position,
+ // then returns the value at that position.
+ //throw exception if it cannot go to that position.
+ public int next() throws Exception;
+
+ // Return the value at current position,
+ // then move the iterator back one position.
+ //throw exception if it cannot go to that position.
+ public int previous() throws Exception;
+
+ public void set(int value);
+
+}
diff --git a/hw/hw-10/hw-10/src/OpenAddressing.java b/hw/hw-10/hw-10/src/OpenAddressing.java
new file mode 100644
index 0000000..574d522
--- /dev/null
+++ b/hw/hw-10/hw-10/src/OpenAddressing.java
@@ -0,0 +1,38 @@
+
+public class OpenAddressing {
+ protected static int DEFAULT_SIZE = 11;
+ protected static final int DELETED = -9999;
+ protected static double MAXFACTOR = 0.5;
+ protected int currentSize = 0;
+ protected int[] array;
+
+ protected static boolean isPrime(int n) {
+ if (n == 2 || n == 3)
+ return true;
+ if (n == 1 || n % 2 == 0)
+ return false;
+ for (int i = 3; i * i <= n; i += 2)
+ if (n % i == 0)
+ return false;
+ return true;
+ }
+
+ protected static int nextPrime(int n) {
+ if (n % 2 == 0)
+ n++;
+ for (; !isPrime(n); n += 2) {
+ }
+ return n;
+ }
+
+ public OpenAddressing() {
+ this(DEFAULT_SIZE);
+ }
+
+ public OpenAddressing(int size) {
+ int nextPrimeSize = nextPrime(size);
+ array = new int[nextPrimeSize];
+ }
+
+
+}
diff --git a/hw/hw-10/hw-10/src/TestHash.java b/hw/hw-10/hw-10/src/TestHash.java
new file mode 100644
index 0000000..71a3e3e
--- /dev/null
+++ b/hw/hw-10/hw-10/src/TestHash.java
@@ -0,0 +1,103 @@
+import static org.junit.jupiter.api.Assertions.*;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class TestHash {
+
+ DoubleHashing hTable;
+
+ @BeforeEach
+ void setUp() throws Exception {
+ hTable = new DoubleHashing();
+
+ }
+
+ @Test
+ void testNextAndHasNextEmptyTable() throws Exception { //1 point
+ HashIterator i = new HashIterator(hTable);
+ assertFalse(i.hasNext());
+ Assertions.assertThrows(Exception.class, () -> {
+ i.next();
+ });
+ }
+
+ @Test
+ void testPreviousAndHasPreviousEmptyTable() throws Exception { //1 point
+ HashIterator i = new HashIterator(hTable);
+ assertFalse(i.hasPrevious());
+ Assertions.assertThrows(Exception.class, () -> {
+ i.previous();
+ });
+ }
+
+ @Test
+ void testNextAndHasNext() throws Exception { //4 points
+ hTable.add(5);
+ hTable.add(16);
+ hTable.add(27);
+ hTable.add(9);
+ hTable.remove(16);
+ hTable.add(20);
+ hTable.add(16);
+
+ HashIterator i = new HashIterator(hTable);
+ assertEquals(16, hTable.array[i.currentPos]);
+
+ assertTrue(i.hasNext());
+ assertEquals(5, i.next());
+
+ assertTrue(i.hasNext());
+ assertEquals(27, i.next());
+
+ assertTrue(i.hasNext());
+ assertEquals(20, i.next());
+
+ assertTrue(i.hasNext());
+ assertEquals(9, i.next());
+
+ assertFalse(i.hasNext());
+ Assertions.assertThrows(Exception.class, () -> {
+ i.next();
+ });
+
+ }
+
+ @Test
+ void testPreviousAndHasPrevious() throws Exception { //4 points
+ hTable.add(5);
+ hTable.add(16);
+ hTable.add(27);
+ hTable.add(9);
+ hTable.remove(16);
+ hTable.add(20);
+ hTable.add(16);
+
+ HashIterator i = new HashIterator(hTable);
+ i.next();
+ i.next();
+ i.next();
+ i.next();
+
+ assertEquals(9, hTable.array[i.currentPos]);
+ assertTrue(i.hasPrevious());
+ assertEquals(9, i.previous());
+
+ assertTrue(i.hasPrevious());
+ assertEquals(20, i.previous());
+
+ assertTrue(i.hasPrevious());
+ assertEquals(27, i.previous());
+
+ assertTrue(i.hasPrevious());
+ assertEquals(5, i.previous());
+
+ assertFalse(i.hasPrevious());
+ Assertions.assertThrows(Exception.class, () -> {
+ i.previous();
+ });
+
+ }
+
+}
diff --git a/hw/hw-11/.DS_Store b/hw/hw-11/.DS_Store
new file mode 100644
index 0000000..e9538aa
Binary files /dev/null and b/hw/hw-11/.DS_Store differ
diff --git a/hw/hw-11/hw-11/.DS_Store b/hw/hw-11/hw-11/.DS_Store
new file mode 100644
index 0000000..0e67fd6
Binary files /dev/null and b/hw/hw-11/hw-11/.DS_Store differ
diff --git a/hw/hw-11/hw-11/.classpath b/hw/hw-11/hw-11/.classpath
new file mode 100644
index 0000000..86473c8
--- /dev/null
+++ b/hw/hw-11/hw-11/.classpath
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/hw/hw-11/hw-11/.gitignore b/hw/hw-11/hw-11/.gitignore
new file mode 100644
index 0000000..ae3c172
--- /dev/null
+++ b/hw/hw-11/hw-11/.gitignore
@@ -0,0 +1 @@
+/bin/
diff --git a/hw/hw-11/hw-11/.project b/hw/hw-11/hw-11/.project
new file mode 100644
index 0000000..a5b23d1
--- /dev/null
+++ b/hw/hw-11/hw-11/.project
@@ -0,0 +1,17 @@
+
+
+ hw-11
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/hw/hw-11/hw-11/.settings/org.eclipse.core.resources.prefs b/hw/hw-11/hw-11/.settings/org.eclipse.core.resources.prefs
new file mode 100644
index 0000000..99f26c0
--- /dev/null
+++ b/hw/hw-11/hw-11/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,2 @@
+eclipse.preferences.version=1
+encoding/=UTF-8
diff --git a/hw/hw-11/hw-11/.settings/org.eclipse.jdt.core.prefs b/hw/hw-11/hw-11/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..ccfd559
--- /dev/null
+++ b/hw/hw-11/hw-11/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,14 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=20
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=20
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
+org.eclipse.jdt.core.compiler.release=enabled
+org.eclipse.jdt.core.compiler.source=20
diff --git a/hw/hw-11/hw-11/src/CDLinkedList.java b/hw/hw-11/hw-11/src/CDLinkedList.java
new file mode 100644
index 0000000..0ff2b42
--- /dev/null
+++ b/hw/hw-11/hw-11/src/CDLinkedList.java
@@ -0,0 +1,188 @@
+public class CDLinkedList {
+ DListNode header;
+ int size;
+ static final int HEADERVALUE = -9999999;
+
+ public CDLinkedList() {
+ header = new DListNode(HEADERVALUE);
+ makeEmpty();// necessary, otherwise next/previous node will be null
+ }
+
+ public boolean isEmpty() {
+ return header.nextNode == header;
+ }
+
+ public boolean isFull() {
+ return false;
+ }
+
+ /** make the list empty. */
+ public void makeEmpty() {
+ header.nextNode = header;
+ header.previousNode = header;
+ size = 0;
+ }
+
+ // put in new data after the position of p.
+ public void insert(int value, Iterator p) throws Exception {
+ if (p == null || !(p instanceof DListIterator))
+ throw new Exception();
+ DListIterator p2 = (DListIterator) p;
+ if (p2.currentNode == null)
+ throw new Exception();
+
+ DListIterator p3 = new DListIterator(p2.currentNode.nextNode);
+ DListNode n = new DListNode(value, p3.currentNode, p2.currentNode);
+ p2.currentNode.nextNode = n;
+ p3.currentNode.previousNode = n;
+ size++;
+ }
+
+ // return position number of value found in the list.
+ // otherwise, return -1.
+ public int find(int value) throws Exception {
+ DListIterator itr = new DListIterator(header);
+ int index = -1;
+ while (itr.hasNext()) {
+ int v = itr.next();
+ index++;
+ if (itr.currentNode == header)
+ return -1;
+ if (v == value)
+ return index; // return the position of value.
+ }
+ return -1;
+ }
+
+ // return data stored at kth position.
+ public int findKth(int kthPosition) throws Exception {
+ if (kthPosition < 0 || kthPosition > size - 1)
+ throw new Exception();// exit the method if the position is
+ // beyond the first/last possible
+ // position, throwing exception in the process.
+ DListIterator itr = new DListIterator(header);
+ int index = -1;
+ while (itr.hasNext()) {
+ int v = itr.next();
+ index++;
+ if (itr.currentNode == header)
+ throw new Exception();
+ if (index == kthPosition)
+ return v;
+ }
+ throw new Exception();
+ }
+
+ // Return iterator at position before the first position that stores value.
+ // If the value is not found, return null.
+ public Iterator findPrevious(int value) throws Exception {
+ if (isEmpty())
+ return null;
+ Iterator itr1 = new DListIterator(header);
+ Iterator itr2 = new DListIterator(header);
+ int currentData = itr2.next();
+ while (currentData != value) {
+ currentData = itr2.next();
+ itr1.next();
+ if (((DListIterator) itr2).currentNode == header)
+ return null;
+ }
+ return itr1;
+ }
+
+ // remove content at position just after the given iterator. Skip header if
+ // found.
+ public void remove(Iterator p) {
+ if (isEmpty())
+ return;
+ if (p == null || !(p instanceof DListIterator))
+ return;
+ DListIterator p2 = (DListIterator) p;
+ if (p2.currentNode == null)
+ return;
+ if (p2.currentNode.nextNode == header)
+ p2.currentNode = header;
+ if (p2.currentNode.nextNode == null)
+ return;
+ DListIterator p3 = new DListIterator(p2.currentNode.nextNode.nextNode);
+ p2.currentNode.nextNode = p3.currentNode;
+ p3.currentNode.previousNode = p2.currentNode;
+ size--;
+ }
+
+ // remove the first instance of the given data.
+ public void remove(int value) throws Exception {
+ Iterator p = findPrevious(value);
+ if (p == null)
+ return;
+ remove(p);
+ }
+
+ // remove data at position p.
+ // if p points to header or the list is empty, do nothing.
+ public void removeAt(Iterator p) throws Exception {
+ if (isEmpty() || p == null || !(p instanceof DListIterator) || ((DListIterator) p).currentNode == null
+ || ((DListIterator) p).currentNode == header)
+ return;
+
+ DListIterator p2 = (DListIterator) (findPrevious(p));
+ remove(p2);
+
+ }
+
+ // Print each contact out, one by one.
+ // To be completed by students.
+ public void printList() throws Exception {
+ Iterator itr = new DListIterator(header);
+ while (itr.hasNext()) {
+ Object data = itr.next();
+
+ System.out.println(data);
+
+ }
+ }
+
+ public int size() throws Exception {
+ return size;
+ }
+
+ // return iterator pointing to location before position.
+ public Iterator findPrevious(Iterator position) throws Exception {
+ if (position == null)
+ return null;
+ if (!(position instanceof DListIterator))
+ return null;
+ if (((DListIterator) position).currentNode == null)
+ return null;
+
+ DListIterator p = ((DListIterator) position);
+ DListIterator p2 = new DListIterator(p.currentNode.previousNode);
+ return p2;
+
+ }
+
+
+
+
+
+ // write the sort method below
+ public void sort() throws Exception { // any sorting will do BUT you must use only 'this' list. No array or any other data structures allowed!!!
+ for (int i = 1;i<=size-1;i++) {
+ DListIterator j = new DListIterator(header.nextNode);
+ while(j.currentNode!=header.previousNode) {
+ if(j.currentNode.data>j.currentNode.nextNode.data) {
+ DListIterator temp = new DListIterator(j.currentNode.nextNode);
+ temp.currentNode.previousNode = j.currentNode.previousNode;
+ j.currentNode.previousNode.nextNode = temp.currentNode;
+ j.currentNode.nextNode = temp.currentNode.nextNode;
+ temp.currentNode.nextNode.previousNode = j.currentNode;
+ temp.currentNode.nextNode = j.currentNode;
+ j.currentNode.previousNode = temp.currentNode;
+ j = temp;
+ }
+ j.next();
+ }
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/hw/hw-11/hw-11/src/CDLinkedListTest.java b/hw/hw-11/hw-11/src/CDLinkedListTest.java
new file mode 100644
index 0000000..a499f5a
--- /dev/null
+++ b/hw/hw-11/hw-11/src/CDLinkedListTest.java
@@ -0,0 +1,148 @@
+import static org.junit.jupiter.api.Assertions.*;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class CDLinkedListTest {
+ CDLinkedList l1 = new CDLinkedList();
+
+ @BeforeEach
+ void setUp() throws Exception {
+ l1 = new CDLinkedList();
+ }
+
+ @Test
+ void testSortEmptyList() throws Exception {
+ l1.sort();
+ assertTrue(l1.isEmpty());
+ }
+
+ @Test
+ void testSortListOneData() throws Exception {
+ l1.insert(0, new DListIterator(l1.header));
+ l1.sort();
+ assertEquals(0,l1.header.nextNode.data);
+ assertEquals(1,l1.size);
+
+ }
+
+ @Test
+ void testSortListEvenData() throws Exception {
+ l1.insert(0, new DListIterator(l1.header));
+ l1.insert(1, new DListIterator(l1.header));
+ l1.insert(3, new DListIterator(l1.header));
+ l1.insert(2, new DListIterator(l1.header));
+ l1.insert(4, new DListIterator(l1.header));
+ l1.insert(6, new DListIterator(l1.header));
+ l1.sort();
+ DListIterator itr = new DListIterator(l1.header.nextNode);
+ assertEquals(0,itr.currentNode.data);
+ assertEquals(1,itr.next());
+ assertEquals(2,itr.next());
+ assertEquals(3,itr.next());
+ assertEquals(4,itr.next());
+ assertEquals(6,itr.next());
+
+ }
+
+ @Test
+ void testSortListEvenDataReverse() throws Exception {
+ l1.insert(0, new DListIterator(l1.header));
+ l1.insert(1, new DListIterator(l1.header));
+ l1.insert(2, new DListIterator(l1.header));
+ l1.insert(3, new DListIterator(l1.header));
+ l1.insert(4, new DListIterator(l1.header));
+ l1.insert(5, new DListIterator(l1.header));
+ l1.insert(6, new DListIterator(l1.header));
+ l1.insert(7, new DListIterator(l1.header));
+
+ l1.sort();
+ DListIterator itr = new DListIterator(l1.header.nextNode);
+ assertEquals(0,itr.currentNode.data);
+ assertEquals(1,itr.next());
+ assertEquals(2,itr.next());
+ assertEquals(3,itr.next());
+ assertEquals(4,itr.next());
+ assertEquals(5,itr.next());
+ assertEquals(6,itr.next());
+ assertEquals(7,itr.next());
+ }
+
+ @Test
+ void testSortListOddData() throws Exception {
+ l1.insert(6, new DListIterator(l1.header));
+ l1.insert(7, new DListIterator(l1.header));
+ l1.insert(5, new DListIterator(l1.header));
+ l1.insert(2, new DListIterator(l1.header));
+ l1.insert(4, new DListIterator(l1.header));
+
+ l1.sort();
+ DListIterator itr = new DListIterator(l1.header.nextNode);
+ assertEquals(2,itr.currentNode.data);
+ assertEquals(4,itr.next());
+ assertEquals(5,itr.next());
+ assertEquals(6,itr.next());
+ assertEquals(7,itr.next());
+
+ }
+
+ @Test
+ void testSortListOddDataReverse() throws Exception {
+ l1.insert(0, new DListIterator(l1.header));
+ l1.insert(1, new DListIterator(l1.header));
+ l1.insert(2, new DListIterator(l1.header));
+ l1.insert(3, new DListIterator(l1.header));
+ l1.insert(4, new DListIterator(l1.header));
+ l1.insert(5, new DListIterator(l1.header));
+ l1.insert(6, new DListIterator(l1.header));
+ l1.insert(7, new DListIterator(l1.header));
+ l1.insert(8, new DListIterator(l1.header));
+
+ l1.sort();
+ DListIterator itr = new DListIterator(l1.header.nextNode);
+ assertEquals(0,itr.currentNode.data);
+ assertEquals(1,itr.next());
+ assertEquals(2,itr.next());
+ assertEquals(3,itr.next());
+ assertEquals(4,itr.next());
+ assertEquals(5,itr.next());
+ assertEquals(6,itr.next());
+ assertEquals(7,itr.next());
+ assertEquals(8,itr.next());
+
+ }
+
+ @Test
+ void testSortListAlreadySorted() throws Exception {
+ l1.insert(8, new DListIterator(l1.header));
+ l1.insert(7, new DListIterator(l1.header));
+ l1.insert(6, new DListIterator(l1.header));
+ l1.insert(5, new DListIterator(l1.header));
+ l1.insert(4, new DListIterator(l1.header));
+ l1.insert(3, new DListIterator(l1.header));
+ l1.insert(2, new DListIterator(l1.header));
+ l1.insert(1, new DListIterator(l1.header));
+ l1.insert(0, new DListIterator(l1.header));
+
+ l1.sort();
+ DListIterator itr = new DListIterator(l1.header.nextNode);
+ assertEquals(0,itr.currentNode.data);
+ assertEquals(1,itr.next());
+ assertEquals(2,itr.next());
+ assertEquals(3,itr.next());
+ assertEquals(4,itr.next());
+ assertEquals(5,itr.next());
+ assertEquals(6,itr.next());
+ assertEquals(7,itr.next());
+ assertEquals(8,itr.next());
+
+ }
+
+
+
+
+
+
+
+
+}
diff --git a/hw/hw-11/hw-11/src/DListIterator.java b/hw/hw-11/hw-11/src/DListIterator.java
new file mode 100644
index 0000000..aad3e48
--- /dev/null
+++ b/hw/hw-11/hw-11/src/DListIterator.java
@@ -0,0 +1,39 @@
+import java.util.NoSuchElementException;
+
+public class DListIterator implements Iterator {
+ DListNode currentNode; // interested position
+
+ DListIterator(DListNode theNode) {
+ currentNode = theNode;
+ }
+
+ public boolean hasNext() { // always true for circular list.
+ return currentNode.nextNode != null;
+ }
+
+ public boolean hasPrevious() { // always true for circular list.
+ return currentNode.previousNode != null;
+ }
+
+ public int next() throws Exception {
+ // Throw exception if the next data
+ // does not exist.
+ if (!hasNext())
+ throw new NoSuchElementException();
+ currentNode = currentNode.nextNode;
+ return currentNode.data;
+
+ }
+
+ public int previous() throws Exception{
+ if (!hasPrevious())
+ throw new NoSuchElementException();
+ int data = currentNode.data;
+ currentNode = currentNode.previousNode;
+ return data;
+ }
+
+ public void set(int value) {
+ currentNode.data = value;
+ }
+}
diff --git a/hw/hw-11/hw-11/src/DListNode.java b/hw/hw-11/hw-11/src/DListNode.java
new file mode 100644
index 0000000..04a3895
--- /dev/null
+++ b/hw/hw-11/hw-11/src/DListNode.java
@@ -0,0 +1,16 @@
+
+public class DListNode {
+ int data;
+ DListNode nextNode, previousNode;
+
+ DListNode(int data) {
+ this(data, null, null);
+ }
+
+ DListNode(int theElement, DListNode n, DListNode p) {
+ data = theElement;
+ nextNode = n;
+ previousNode = p;
+ }
+
+}
diff --git a/hw/hw-11/hw-11/src/Iterator.java b/hw/hw-11/hw-11/src/Iterator.java
new file mode 100644
index 0000000..bbb54ef
--- /dev/null
+++ b/hw/hw-11/hw-11/src/Iterator.java
@@ -0,0 +1,17 @@
+public interface Iterator {
+
+ public boolean hasNext();
+
+ public boolean hasPrevious();
+
+ // Move iterator to the next position,
+ // then returns the value at that position.
+ public int next() throws Exception;
+
+ // Return the value at current position,
+ // then move the iterator back one position.
+ public int previous() throws Exception;
+
+ public void set(int value);
+
+}
diff --git a/hw/hw-11/hw11_toSTudent_65f954514871a/CDLinkedList.java b/hw/hw-11/hw11_toSTudent_65f954514871a/CDLinkedList.java
new file mode 100644
index 0000000..b1d076e
--- /dev/null
+++ b/hw/hw-11/hw11_toSTudent_65f954514871a/CDLinkedList.java
@@ -0,0 +1,174 @@
+public class CDLinkedList {
+ DListNode header;
+ int size;
+ static final int HEADERVALUE = -9999999;
+
+ public CDLinkedList() {
+ header = new DListNode(HEADERVALUE);
+ makeEmpty();// necessary, otherwise next/previous node will be null
+ }
+
+ public boolean isEmpty() {
+ return header.nextNode == header;
+ }
+
+ public boolean isFull() {
+ return false;
+ }
+
+ /** make the list empty. */
+ public void makeEmpty() {
+ header.nextNode = header;
+ header.previousNode = header;
+ size = 0;
+ }
+
+ // put in new data after the position of p.
+ public void insert(int value, Iterator p) throws Exception {
+ if (p == null || !(p instanceof DListIterator))
+ throw new Exception();
+ DListIterator p2 = (DListIterator) p;
+ if (p2.currentNode == null)
+ throw new Exception();
+
+ DListIterator p3 = new DListIterator(p2.currentNode.nextNode);
+ DListNode n = new DListNode(value, p3.currentNode, p2.currentNode);
+ p2.currentNode.nextNode = n;
+ p3.currentNode.previousNode = n;
+ size++;
+ }
+
+ // return position number of value found in the list.
+ // otherwise, return -1.
+ public int find(int value) throws Exception {
+ DListIterator itr = new DListIterator(header);
+ int index = -1;
+ while (itr.hasNext()) {
+ int v = itr.next();
+ index++;
+ if (itr.currentNode == header)
+ return -1;
+ if (v == value)
+ return index; // return the position of value.
+ }
+ return -1;
+ }
+
+ // return data stored at kth position.
+ public int findKth(int kthPosition) throws Exception {
+ if (kthPosition < 0 || kthPosition > size - 1)
+ throw new Exception();// exit the method if the position is
+ // beyond the first/last possible
+ // position, throwing exception in the process.
+ DListIterator itr = new DListIterator(header);
+ int index = -1;
+ while (itr.hasNext()) {
+ int v = itr.next();
+ index++;
+ if (itr.currentNode == header)
+ throw new Exception();
+ if (index == kthPosition)
+ return v;
+ }
+ throw new Exception();
+ }
+
+ // Return iterator at position before the first position that stores value.
+ // If the value is not found, return null.
+ public Iterator findPrevious(int value) throws Exception {
+ if (isEmpty())
+ return null;
+ Iterator itr1 = new DListIterator(header);
+ Iterator itr2 = new DListIterator(header);
+ int currentData = itr2.next();
+ while (currentData != value) {
+ currentData = itr2.next();
+ itr1.next();
+ if (((DListIterator) itr2).currentNode == header)
+ return null;
+ }
+ return itr1;
+ }
+
+ // remove content at position just after the given iterator. Skip header if
+ // found.
+ public void remove(Iterator p) {
+ if (isEmpty())
+ return;
+ if (p == null || !(p instanceof DListIterator))
+ return;
+ DListIterator p2 = (DListIterator) p;
+ if (p2.currentNode == null)
+ return;
+ if (p2.currentNode.nextNode == header)
+ p2.currentNode = header;
+ if (p2.currentNode.nextNode == null)
+ return;
+ DListIterator p3 = new DListIterator(p2.currentNode.nextNode.nextNode);
+ p2.currentNode.nextNode = p3.currentNode;
+ p3.currentNode.previousNode = p2.currentNode;
+ size--;
+ }
+
+ // remove the first instance of the given data.
+ public void remove(int value) throws Exception {
+ Iterator p = findPrevious(value);
+ if (p == null)
+ return;
+ remove(p);
+ }
+
+ // remove data at position p.
+ // if p points to header or the list is empty, do nothing.
+ public void removeAt(Iterator p) throws Exception {
+ if (isEmpty() || p == null || !(p instanceof DListIterator) || ((DListIterator) p).currentNode == null
+ || ((DListIterator) p).currentNode == header)
+ return;
+
+ DListIterator p2 = (DListIterator) (findPrevious(p));
+ remove(p2);
+
+ }
+
+ // Print each contact out, one by one.
+ // To be completed by students.
+ public void printList() throws Exception {
+ Iterator itr = new DListIterator(header);
+ while (itr.hasNext()) {
+ Object data = itr.next();
+
+ System.out.println(data);
+
+ }
+ }
+
+ public int size() throws Exception {
+ return size;
+ }
+
+ // return iterator pointing to location before position.
+ public Iterator findPrevious(Iterator position) throws Exception {
+ if (position == null)
+ return null;
+ if (!(position instanceof DListIterator))
+ return null;
+ if (((DListIterator) position).currentNode == null)
+ return null;
+
+ DListIterator p = ((DListIterator) position);
+ DListIterator p2 = new DListIterator(p.currentNode.previousNode);
+ return p2;
+
+ }
+
+
+
+
+
+ // write the sort method below
+ public void sort() throws Exception { // any sorting will do BUT you must use only 'this' list. No array or any other data structures allowed!!!
+
+
+ }
+
+}
\ No newline at end of file
diff --git a/hw/hw-11/hw11_toSTudent_65f954514871a/CDLinkedListTest.java b/hw/hw-11/hw11_toSTudent_65f954514871a/CDLinkedListTest.java
new file mode 100644
index 0000000..a499f5a
--- /dev/null
+++ b/hw/hw-11/hw11_toSTudent_65f954514871a/CDLinkedListTest.java
@@ -0,0 +1,148 @@
+import static org.junit.jupiter.api.Assertions.*;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class CDLinkedListTest {
+ CDLinkedList l1 = new CDLinkedList();
+
+ @BeforeEach
+ void setUp() throws Exception {
+ l1 = new CDLinkedList();
+ }
+
+ @Test
+ void testSortEmptyList() throws Exception {
+ l1.sort();
+ assertTrue(l1.isEmpty());
+ }
+
+ @Test
+ void testSortListOneData() throws Exception {
+ l1.insert(0, new DListIterator(l1.header));
+ l1.sort();
+ assertEquals(0,l1.header.nextNode.data);
+ assertEquals(1,l1.size);
+
+ }
+
+ @Test
+ void testSortListEvenData() throws Exception {
+ l1.insert(0, new DListIterator(l1.header));
+ l1.insert(1, new DListIterator(l1.header));
+ l1.insert(3, new DListIterator(l1.header));
+ l1.insert(2, new DListIterator(l1.header));
+ l1.insert(4, new DListIterator(l1.header));
+ l1.insert(6, new DListIterator(l1.header));
+ l1.sort();
+ DListIterator itr = new DListIterator(l1.header.nextNode);
+ assertEquals(0,itr.currentNode.data);
+ assertEquals(1,itr.next());
+ assertEquals(2,itr.next());
+ assertEquals(3,itr.next());
+ assertEquals(4,itr.next());
+ assertEquals(6,itr.next());
+
+ }
+
+ @Test
+ void testSortListEvenDataReverse() throws Exception {
+ l1.insert(0, new DListIterator(l1.header));
+ l1.insert(1, new DListIterator(l1.header));
+ l1.insert(2, new DListIterator(l1.header));
+ l1.insert(3, new DListIterator(l1.header));
+ l1.insert(4, new DListIterator(l1.header));
+ l1.insert(5, new DListIterator(l1.header));
+ l1.insert(6, new DListIterator(l1.header));
+ l1.insert(7, new DListIterator(l1.header));
+
+ l1.sort();
+ DListIterator itr = new DListIterator(l1.header.nextNode);
+ assertEquals(0,itr.currentNode.data);
+ assertEquals(1,itr.next());
+ assertEquals(2,itr.next());
+ assertEquals(3,itr.next());
+ assertEquals(4,itr.next());
+ assertEquals(5,itr.next());
+ assertEquals(6,itr.next());
+ assertEquals(7,itr.next());
+ }
+
+ @Test
+ void testSortListOddData() throws Exception {
+ l1.insert(6, new DListIterator(l1.header));
+ l1.insert(7, new DListIterator(l1.header));
+ l1.insert(5, new DListIterator(l1.header));
+ l1.insert(2, new DListIterator(l1.header));
+ l1.insert(4, new DListIterator(l1.header));
+
+ l1.sort();
+ DListIterator itr = new DListIterator(l1.header.nextNode);
+ assertEquals(2,itr.currentNode.data);
+ assertEquals(4,itr.next());
+ assertEquals(5,itr.next());
+ assertEquals(6,itr.next());
+ assertEquals(7,itr.next());
+
+ }
+
+ @Test
+ void testSortListOddDataReverse() throws Exception {
+ l1.insert(0, new DListIterator(l1.header));
+ l1.insert(1, new DListIterator(l1.header));
+ l1.insert(2, new DListIterator(l1.header));
+ l1.insert(3, new DListIterator(l1.header));
+ l1.insert(4, new DListIterator(l1.header));
+ l1.insert(5, new DListIterator(l1.header));
+ l1.insert(6, new DListIterator(l1.header));
+ l1.insert(7, new DListIterator(l1.header));
+ l1.insert(8, new DListIterator(l1.header));
+
+ l1.sort();
+ DListIterator itr = new DListIterator(l1.header.nextNode);
+ assertEquals(0,itr.currentNode.data);
+ assertEquals(1,itr.next());
+ assertEquals(2,itr.next());
+ assertEquals(3,itr.next());
+ assertEquals(4,itr.next());
+ assertEquals(5,itr.next());
+ assertEquals(6,itr.next());
+ assertEquals(7,itr.next());
+ assertEquals(8,itr.next());
+
+ }
+
+ @Test
+ void testSortListAlreadySorted() throws Exception {
+ l1.insert(8, new DListIterator(l1.header));
+ l1.insert(7, new DListIterator(l1.header));
+ l1.insert(6, new DListIterator(l1.header));
+ l1.insert(5, new DListIterator(l1.header));
+ l1.insert(4, new DListIterator(l1.header));
+ l1.insert(3, new DListIterator(l1.header));
+ l1.insert(2, new DListIterator(l1.header));
+ l1.insert(1, new DListIterator(l1.header));
+ l1.insert(0, new DListIterator(l1.header));
+
+ l1.sort();
+ DListIterator itr = new DListIterator(l1.header.nextNode);
+ assertEquals(0,itr.currentNode.data);
+ assertEquals(1,itr.next());
+ assertEquals(2,itr.next());
+ assertEquals(3,itr.next());
+ assertEquals(4,itr.next());
+ assertEquals(5,itr.next());
+ assertEquals(6,itr.next());
+ assertEquals(7,itr.next());
+ assertEquals(8,itr.next());
+
+ }
+
+
+
+
+
+
+
+
+}
diff --git a/hw/hw-11/hw11_toSTudent_65f954514871a/DListIterator.java b/hw/hw-11/hw11_toSTudent_65f954514871a/DListIterator.java
new file mode 100644
index 0000000..aad3e48
--- /dev/null
+++ b/hw/hw-11/hw11_toSTudent_65f954514871a/DListIterator.java
@@ -0,0 +1,39 @@
+import java.util.NoSuchElementException;
+
+public class DListIterator implements Iterator {
+ DListNode currentNode; // interested position
+
+ DListIterator(DListNode theNode) {
+ currentNode = theNode;
+ }
+
+ public boolean hasNext() { // always true for circular list.
+ return currentNode.nextNode != null;
+ }
+
+ public boolean hasPrevious() { // always true for circular list.
+ return currentNode.previousNode != null;
+ }
+
+ public int next() throws Exception {
+ // Throw exception if the next data
+ // does not exist.
+ if (!hasNext())
+ throw new NoSuchElementException();
+ currentNode = currentNode.nextNode;
+ return currentNode.data;
+
+ }
+
+ public int previous() throws Exception{
+ if (!hasPrevious())
+ throw new NoSuchElementException();
+ int data = currentNode.data;
+ currentNode = currentNode.previousNode;
+ return data;
+ }
+
+ public void set(int value) {
+ currentNode.data = value;
+ }
+}
diff --git a/hw/hw-11/hw11_toSTudent_65f954514871a/DListNode.java b/hw/hw-11/hw11_toSTudent_65f954514871a/DListNode.java
new file mode 100644
index 0000000..04a3895
--- /dev/null
+++ b/hw/hw-11/hw11_toSTudent_65f954514871a/DListNode.java
@@ -0,0 +1,16 @@
+
+public class DListNode {
+ int data;
+ DListNode nextNode, previousNode;
+
+ DListNode(int data) {
+ this(data, null, null);
+ }
+
+ DListNode(int theElement, DListNode n, DListNode p) {
+ data = theElement;
+ nextNode = n;
+ previousNode = p;
+ }
+
+}
diff --git a/hw/hw-11/hw11_toSTudent_65f954514871a/Iterator.java b/hw/hw-11/hw11_toSTudent_65f954514871a/Iterator.java
new file mode 100644
index 0000000..bbb54ef
--- /dev/null
+++ b/hw/hw-11/hw11_toSTudent_65f954514871a/Iterator.java
@@ -0,0 +1,17 @@
+public interface Iterator {
+
+ public boolean hasNext();
+
+ public boolean hasPrevious();
+
+ // Move iterator to the next position,
+ // then returns the value at that position.
+ public int next() throws Exception;
+
+ // Return the value at current position,
+ // then move the iterator back one position.
+ public int previous() throws Exception;
+
+ public void set(int value);
+
+}
diff --git a/hw/hw-11/hw11_toSTudent_65f954514871a/hw11_Sorting.docx b/hw/hw-11/hw11_toSTudent_65f954514871a/hw11_Sorting.docx
new file mode 100644
index 0000000..fcf1424
Binary files /dev/null and b/hw/hw-11/hw11_toSTudent_65f954514871a/hw11_Sorting.docx differ
diff --git a/hw/hw-11/hw11_toSTudent_65f954514871a/hw11_Sorting.pdf b/hw/hw-11/hw11_toSTudent_65f954514871a/hw11_Sorting.pdf
new file mode 100644
index 0000000..39806a6
Binary files /dev/null and b/hw/hw-11/hw11_toSTudent_65f954514871a/hw11_Sorting.pdf differ
diff --git a/hw/hw-12/.DS_Store b/hw/hw-12/.DS_Store
new file mode 100644
index 0000000..d45e3b8
Binary files /dev/null and b/hw/hw-12/.DS_Store differ
diff --git a/hw/hw-12/HW12_Heap_65f9595b952b7/Heap.java b/hw/hw-12/HW12_Heap_65f9595b952b7/Heap.java
new file mode 100644
index 0000000..2edf67b
--- /dev/null
+++ b/hw/hw-12/HW12_Heap_65f9595b952b7/Heap.java
@@ -0,0 +1,85 @@
+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();
+
+ }
+
+ 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];
+ }
+
+ 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;
+ }
+
+ //write this method!!!
+ public static boolean isAHeap(Heap h) {
+
+ }
+
+
+
+}
diff --git a/hw/hw-12/HW12_Heap_65f9595b952b7/TestIsHeap.java b/hw/hw-12/HW12_Heap_65f9595b952b7/TestIsHeap.java
new file mode 100644
index 0000000..73d0445
--- /dev/null
+++ b/hw/hw-12/HW12_Heap_65f9595b952b7/TestIsHeap.java
@@ -0,0 +1,108 @@
+import static org.junit.jupiter.api.Assertions.*;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class TestIsHeap {
+
+ Heap h;
+ @BeforeEach
+ void setUp() throws Exception {
+ h = new Heap();
+ }
+
+ @Test
+ void testEmpty() {
+ assertTrue(Heap.isAHeap(h));
+ }
+
+ @Test
+ void testOneData(){
+ h.add(7);
+ assertTrue(Heap.isAHeap(h));
+ }
+
+ @Test
+ void testTwoData() {
+ h.add(7);
+ h.add(2);
+ assertTrue(Heap.isAHeap(h));
+
+ h.mData[0] =10;
+ assertFalse(Heap.isAHeap(h));
+
+ }
+
+ @Test
+ void testGeneralHeap() {
+ h.add(7);
+ h.add(2);
+ h.add(8);
+ h.add(1);
+ h.add(3);
+ h.add(6);
+ h.add(5);
+ h.add(2);
+ h.add(4);
+ assertTrue(Heap.isAHeap(h));
+ }
+
+ @Test
+ void testGeneralNonHeap() {
+ h.add(7);
+ h.add(2);
+ h.add(8);
+ h.add(1);
+ h.add(3);
+ h.add(6);
+ h.add(5);
+ h.add(2);
+ h.add(4);
+ h.mData[0] = 1;
+ h.mData[1] = 5;
+ h.mData[2] = 10;
+ h.mData[3] = 11;
+ h.mData[4] = 12;
+ h.mData[5] = 15;
+ h.mData[6] = 17;
+ h.mData[7] = 4;
+ assertFalse(Heap.isAHeap(h));
+
+ }
+
+ @Test
+ void testGeneralHeap2() {
+ h.add(4);
+ h.add(9);
+ h.add(1);
+ h.add(0);
+ h.add(8);
+ h.add(7);
+ h.add(5);
+ h.add(2);
+ h.add(3);
+ h.add(6);
+ assertTrue(Heap.isAHeap(h));
+ }
+
+ @Test
+ void testGeneralNonHeap2() {
+ h.add(4);
+ h.add(10);
+ h.add(1);
+ h.add(0);
+ h.add(8);
+ h.add(7);
+ h.add(5);
+ h.add(-1);
+ h.add(3);
+ h.add(6);
+ h.mData[9] = 2;
+ assertFalse(Heap.isAHeap(h));
+ }
+
+
+
+
+
+}
diff --git a/hw/hw-12/HW12_Heap_65f9595b952b7/hw12_Heap.docx b/hw/hw-12/HW12_Heap_65f9595b952b7/hw12_Heap.docx
new file mode 100644
index 0000000..cb0f46c
Binary files /dev/null and b/hw/hw-12/HW12_Heap_65f9595b952b7/hw12_Heap.docx differ
diff --git a/hw/hw-12/HW12_Heap_65f9595b952b7/hw12_Heap.pdf b/hw/hw-12/HW12_Heap_65f9595b952b7/hw12_Heap.pdf
new file mode 100644
index 0000000..4997c88
Binary files /dev/null and b/hw/hw-12/HW12_Heap_65f9595b952b7/hw12_Heap.pdf differ
diff --git a/hw/hw-12/hw-12/.DS_Store b/hw/hw-12/hw-12/.DS_Store
new file mode 100644
index 0000000..1471c14
Binary files /dev/null and b/hw/hw-12/hw-12/.DS_Store differ
diff --git a/hw/hw-12/hw-12/.classpath b/hw/hw-12/hw-12/.classpath
new file mode 100644
index 0000000..86473c8
--- /dev/null
+++ b/hw/hw-12/hw-12/.classpath
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/hw/hw-12/hw-12/.gitignore b/hw/hw-12/hw-12/.gitignore
new file mode 100644
index 0000000..ae3c172
--- /dev/null
+++ b/hw/hw-12/hw-12/.gitignore
@@ -0,0 +1 @@
+/bin/
diff --git a/hw/hw-12/hw-12/.project b/hw/hw-12/hw-12/.project
new file mode 100644
index 0000000..748cc3e
--- /dev/null
+++ b/hw/hw-12/hw-12/.project
@@ -0,0 +1,17 @@
+
+
+ hw-12
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/hw/hw-12/hw-12/.settings/org.eclipse.core.resources.prefs b/hw/hw-12/hw-12/.settings/org.eclipse.core.resources.prefs
new file mode 100644
index 0000000..99f26c0
--- /dev/null
+++ b/hw/hw-12/hw-12/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,2 @@
+eclipse.preferences.version=1
+encoding/=UTF-8
diff --git a/hw/hw-12/hw-12/.settings/org.eclipse.jdt.core.prefs b/hw/hw-12/hw-12/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..ccfd559
--- /dev/null
+++ b/hw/hw-12/hw-12/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,14 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=20
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=20
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
+org.eclipse.jdt.core.compiler.release=enabled
+org.eclipse.jdt.core.compiler.source=20
diff --git a/hw/hw-12/hw-12/src/Heap.java b/hw/hw-12/hw-12/src/Heap.java
new file mode 100644
index 0000000..6497e75
--- /dev/null
+++ b/hw/hw-12/hw-12/src/Heap.java
@@ -0,0 +1,93 @@
+import java.util.Arrays;
+
+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();
+
+ }
+
+ 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];
+ }
+
+ 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;
+ }
+
+ //write this method!!!
+ public static boolean isAHeap(Heap h) {
+ for (int i = 1; i < h.size; i++) {
+ if (h.mData[i] < h.mData[(i - 1) / 2]) {
+ return false;
+ }
+ }
+ return true;
+
+ }
+
+
+
+}
diff --git a/hw/hw-12/hw-12/src/TestIsHeap.java b/hw/hw-12/hw-12/src/TestIsHeap.java
new file mode 100644
index 0000000..73d0445
--- /dev/null
+++ b/hw/hw-12/hw-12/src/TestIsHeap.java
@@ -0,0 +1,108 @@
+import static org.junit.jupiter.api.Assertions.*;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class TestIsHeap {
+
+ Heap h;
+ @BeforeEach
+ void setUp() throws Exception {
+ h = new Heap();
+ }
+
+ @Test
+ void testEmpty() {
+ assertTrue(Heap.isAHeap(h));
+ }
+
+ @Test
+ void testOneData(){
+ h.add(7);
+ assertTrue(Heap.isAHeap(h));
+ }
+
+ @Test
+ void testTwoData() {
+ h.add(7);
+ h.add(2);
+ assertTrue(Heap.isAHeap(h));
+
+ h.mData[0] =10;
+ assertFalse(Heap.isAHeap(h));
+
+ }
+
+ @Test
+ void testGeneralHeap() {
+ h.add(7);
+ h.add(2);
+ h.add(8);
+ h.add(1);
+ h.add(3);
+ h.add(6);
+ h.add(5);
+ h.add(2);
+ h.add(4);
+ assertTrue(Heap.isAHeap(h));
+ }
+
+ @Test
+ void testGeneralNonHeap() {
+ h.add(7);
+ h.add(2);
+ h.add(8);
+ h.add(1);
+ h.add(3);
+ h.add(6);
+ h.add(5);
+ h.add(2);
+ h.add(4);
+ h.mData[0] = 1;
+ h.mData[1] = 5;
+ h.mData[2] = 10;
+ h.mData[3] = 11;
+ h.mData[4] = 12;
+ h.mData[5] = 15;
+ h.mData[6] = 17;
+ h.mData[7] = 4;
+ assertFalse(Heap.isAHeap(h));
+
+ }
+
+ @Test
+ void testGeneralHeap2() {
+ h.add(4);
+ h.add(9);
+ h.add(1);
+ h.add(0);
+ h.add(8);
+ h.add(7);
+ h.add(5);
+ h.add(2);
+ h.add(3);
+ h.add(6);
+ assertTrue(Heap.isAHeap(h));
+ }
+
+ @Test
+ void testGeneralNonHeap2() {
+ h.add(4);
+ h.add(10);
+ h.add(1);
+ h.add(0);
+ h.add(8);
+ h.add(7);
+ h.add(5);
+ h.add(-1);
+ h.add(3);
+ h.add(6);
+ h.mData[9] = 2;
+ assertFalse(Heap.isAHeap(h));
+ }
+
+
+
+
+
+}
diff --git a/hw/hw-13/.DS_Store b/hw/hw-13/.DS_Store
new file mode 100644
index 0000000..b54e6cf
Binary files /dev/null and b/hw/hw-13/.DS_Store differ
diff --git a/hw/hw-13/Hw13_toStudent_65f9647675892/Heap.java b/hw/hw-13/Hw13_toStudent_65f9647675892/Heap.java
new file mode 100644
index 0000000..659e1a4
--- /dev/null
+++ b/hw/hw-13/Hw13_toStudent_65f9647675892/Heap.java
@@ -0,0 +1,79 @@
+public class Heap {
+ int[] mData;
+ int size = 0;
+
+ public Heap() {
+ final int DEFAULT_CAPACITY = 31;
+ 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();
+
+ }
+
+ 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];
+ }
+
+ 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;
+ }
+
+
+}
diff --git a/hw/hw-13/Hw13_toStudent_65f9647675892/TestHeap.java b/hw/hw-13/Hw13_toStudent_65f9647675892/TestHeap.java
new file mode 100644
index 0000000..f2f6d92
--- /dev/null
+++ b/hw/hw-13/Hw13_toStudent_65f9647675892/TestHeap.java
@@ -0,0 +1,228 @@
+import static org.junit.jupiter.api.Assertions.*;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class TestHeap {
+ TernaryHeap h;
+
+ @BeforeEach
+ void setUp() throws Exception {
+ h = new TernaryHeap();
+ }
+
+ @Test
+ void testAdd01() { //1
+ h.add(5);
+ h.add(9);
+ h.add(8);
+ h.add(6);
+ h.add(4);
+
+ assertEquals(5, h.size);
+ assertEquals(4, h.mData[0]);
+ assertEquals(5, h.mData[1]);
+ assertEquals(8, h.mData[2]);
+ assertEquals(6, h.mData[3]);
+ assertEquals(9, h.mData[4]);
+
+ }
+
+ @Test
+ void testAdd2() { //1
+ h.mData[0] = 4;
+ h.mData[1] = 5;
+ h.mData[2] = 8;
+ h.mData[3] = 6;
+ h.mData[4] = 9;
+ h.mData[5] = 10;
+ h.mData[6] = 15;
+ h.mData[7] = 20;
+ h.size = 8;
+
+ h.add(7);
+ assertEquals(9, h.size);
+ assertEquals(4, h.mData[0]);
+ assertEquals(5, h.mData[1]);
+ assertEquals(7, h.mData[2]);
+ assertEquals(6, h.mData[3]);
+ assertEquals(9, h.mData[4]);
+ assertEquals(10, h.mData[5]);
+ assertEquals(15, h.mData[6]);
+ assertEquals(20, h.mData[7]);
+ assertEquals(8, h.mData[8]);
+ }
+
+ @Test
+ void testAdd3() { //1
+ h.mData[0] = 4;
+ h.mData[1] = 5;
+ h.mData[2] = 7;
+ h.mData[3] = 6;
+ h.mData[4] = 9;
+ h.mData[5] = 10;
+ h.mData[6] = 15;
+ h.mData[7] = 20;
+ h.mData[8] = 8;
+ h.mData[9] = 13;
+ h.mData[10] = 12;
+ h.mData[11] = 18;
+ h.size = 12;
+
+ h.add(3);
+ assertEquals(13, h.size);
+ assertEquals(3, h.mData[0]);
+ assertEquals(5, h.mData[1]);
+ assertEquals(7, h.mData[2]);
+ assertEquals(4, h.mData[3]);
+ assertEquals(9, h.mData[4]);
+
+ assertEquals(10, h.mData[5]);
+ assertEquals(15, h.mData[6]);
+ assertEquals(20, h.mData[7]);
+ assertEquals(8, h.mData[8]);
+ assertEquals(13, h.mData[9]);
+
+ assertEquals(12, h.mData[10]);
+ assertEquals(18, h.mData[11]);
+ assertEquals(6, h.mData[12]);
+
+ }
+
+ @Test
+ void testPop01() throws Exception { //2
+ h.mData[0] = 3;
+ h.mData[1] = 5;
+ h.mData[2] = 7;
+ h.mData[3] = 4;
+ h.mData[4] = 9;
+ h.mData[5] = 10;
+ h.mData[6] = 15;
+ h.mData[7] = 20;
+ h.mData[8] = 8;
+ h.mData[9] = 13;
+ h.mData[10] = 18;
+ h.mData[11] = 12;
+ h.mData[12] = 25;
+ h.size = 13;
+
+ h.pop();
+
+ assertEquals(12, h.size);
+ assertEquals(4, h.mData[0]);
+ assertEquals(5, h.mData[1]);
+ assertEquals(7, h.mData[2]);
+ assertEquals(12, h.mData[3]);
+ assertEquals(9, h.mData[4]);
+ assertEquals(10, h.mData[5]);
+ assertEquals(15, h.mData[6]);
+ assertEquals(20, h.mData[7]);
+ assertEquals(8, h.mData[8]);
+ assertEquals(13, h.mData[9]);
+ assertEquals(18, h.mData[10]);
+ assertEquals(25, h.mData[11]);
+
+ }
+
+ @Test
+ void testPop02() throws Exception { //2
+ h.mData[0] = 4;
+ h.mData[1] = 5;
+ h.mData[2] = 7;
+ h.mData[3] = 6;
+ h.mData[4] = 10;
+ h.mData[5] = 9;
+ h.mData[6] = 15;
+ h.mData[7] = 20;
+ h.mData[8] = 8;
+ h.mData[9] = 13;
+ h.mData[10] = 12;
+ h.mData[11] = 18;
+ h.size = 12;
+
+ h.pop();
+
+ assertEquals(11, h.size);
+ assertEquals(5, h.mData[0]);
+ assertEquals(9, h.mData[1]);
+ assertEquals(7, h.mData[2]);
+ assertEquals(6, h.mData[3]);
+ assertEquals(10, h.mData[4]);
+ assertEquals(18, h.mData[5]);
+ assertEquals(15, h.mData[6]);
+ assertEquals(20, h.mData[7]);
+ assertEquals(8, h.mData[8]);
+ assertEquals(13, h.mData[9]);
+ assertEquals(12, h.mData[10]);
+
+
+ }
+
+ @Test
+ void testPop03() throws Exception { //2
+ h.mData[0] = 5;
+ h.mData[1] = 9;
+ h.mData[2] = 6;
+ h.mData[3] = 7;
+ h.mData[4] = 10;
+ h.mData[5] = 18;
+ h.mData[6] = 15;
+ h.mData[7] = 20;
+ h.mData[8] = 13;
+ h.mData[9] = 8;
+ h.mData[10] = 12;
+ h.size = 11;
+
+ h.pop();
+
+ assertEquals(10, h.size);
+ assertEquals(6, h.mData[0]);
+ assertEquals(9, h.mData[1]);
+ assertEquals(8, h.mData[2]);
+ assertEquals(7, h.mData[3]);
+ assertEquals(10, h.mData[4]);
+ assertEquals(18, h.mData[5]);
+ assertEquals(15, h.mData[6]);
+ assertEquals(20, h.mData[7]);
+ assertEquals(13, h.mData[8]);
+ assertEquals(12, h.mData[9]);
+
+ }
+
+ @Test
+ void testPop04() throws Exception { //2
+ h.mData[0] = 3;
+ h.mData[1] = 5;
+ h.mData[2] = 7;
+ h.mData[3] = 4;
+ h.mData[4] = 9;
+ h.mData[5] = 10;
+ h.mData[6] = 15;
+ h.mData[7] = 20;
+ h.mData[8] = 8;
+ h.mData[9] = 13;
+ h.mData[10] = 12;
+ h.mData[11] = 18;
+ h.mData[12] = 25;
+ h.size = 13;
+
+ h.pop();
+
+ assertEquals(12, h.size);
+ assertEquals(4, h.mData[0]);
+ assertEquals(5, h.mData[1]);
+ assertEquals(7, h.mData[2]);
+ assertEquals(12, h.mData[3]);
+ assertEquals(9, h.mData[4]);
+ assertEquals(10, h.mData[5]);
+ assertEquals(15, h.mData[6]);
+ assertEquals(20, h.mData[7]);
+ assertEquals(8, h.mData[8]);
+ assertEquals(13, h.mData[9]);
+ assertEquals(25, h.mData[10]);
+ assertEquals(18, h.mData[11]);
+
+ }
+
+
+}
diff --git a/hw/hw-13/Hw13_toStudent_65f9647675892/hw13 - ternaryHeap.docx b/hw/hw-13/Hw13_toStudent_65f9647675892/hw13 - ternaryHeap.docx
new file mode 100644
index 0000000..e7bd0b2
Binary files /dev/null and b/hw/hw-13/Hw13_toStudent_65f9647675892/hw13 - ternaryHeap.docx differ
diff --git a/hw/hw-13/Hw13_toStudent_65f9647675892/hw13 - ternaryHeap.pdf b/hw/hw-13/Hw13_toStudent_65f9647675892/hw13 - ternaryHeap.pdf
new file mode 100644
index 0000000..6ede7d7
Binary files /dev/null and b/hw/hw-13/Hw13_toStudent_65f9647675892/hw13 - ternaryHeap.pdf differ
diff --git a/hw/hw-13/hw-13/.DS_Store b/hw/hw-13/hw-13/.DS_Store
new file mode 100644
index 0000000..1471c14
Binary files /dev/null and b/hw/hw-13/hw-13/.DS_Store differ
diff --git a/hw/hw-13/hw-13/.classpath b/hw/hw-13/hw-13/.classpath
new file mode 100644
index 0000000..86473c8
--- /dev/null
+++ b/hw/hw-13/hw-13/.classpath
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/hw/hw-13/hw-13/.gitignore b/hw/hw-13/hw-13/.gitignore
new file mode 100644
index 0000000..ae3c172
--- /dev/null
+++ b/hw/hw-13/hw-13/.gitignore
@@ -0,0 +1 @@
+/bin/
diff --git a/hw/hw-13/hw-13/.project b/hw/hw-13/hw-13/.project
new file mode 100644
index 0000000..1d2dcff
--- /dev/null
+++ b/hw/hw-13/hw-13/.project
@@ -0,0 +1,17 @@
+
+
+ hw-13
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/hw/hw-13/hw-13/.settings/org.eclipse.core.resources.prefs b/hw/hw-13/hw-13/.settings/org.eclipse.core.resources.prefs
new file mode 100644
index 0000000..99f26c0
--- /dev/null
+++ b/hw/hw-13/hw-13/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,2 @@
+eclipse.preferences.version=1
+encoding/=UTF-8
diff --git a/hw/hw-13/hw-13/.settings/org.eclipse.jdt.core.prefs b/hw/hw-13/hw-13/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..ccfd559
--- /dev/null
+++ b/hw/hw-13/hw-13/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,14 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=20
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=20
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
+org.eclipse.jdt.core.compiler.release=enabled
+org.eclipse.jdt.core.compiler.source=20
diff --git a/hw/hw-13/hw-13/src/Heap.java b/hw/hw-13/hw-13/src/Heap.java
new file mode 100644
index 0000000..659e1a4
--- /dev/null
+++ b/hw/hw-13/hw-13/src/Heap.java
@@ -0,0 +1,79 @@
+public class Heap {
+ int[] mData;
+ int size = 0;
+
+ public Heap() {
+ final int DEFAULT_CAPACITY = 31;
+ 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();
+
+ }
+
+ 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];
+ }
+
+ 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;
+ }
+
+
+}
diff --git a/hw/hw-13/hw-13/src/TernaryHeap.java b/hw/hw-13/hw-13/src/TernaryHeap.java
new file mode 100644
index 0000000..00917ca
--- /dev/null
+++ b/hw/hw-13/hw-13/src/TernaryHeap.java
@@ -0,0 +1,63 @@
+
+public class TernaryHeap extends Heap {
+ public TernaryHeap() {
+ super();
+ }
+
+ public void add(int element) {
+ if (size == mData.length) {
+ int[] newHeap = new int[3 * mData.length];
+ System.arraycopy(mData, 0, newHeap, 0, size);
+ mData = newHeap;
+ }
+ mData[size++] = element;
+ percolateUp();
+
+ }
+
+ protected void percolateUp() {
+ int parent;
+ int child = size - 1;
+ int temp;
+ while (child > 0) {
+ parent = (child - 1) / 3;
+ if (mData[parent] <= mData[child])
+ break;
+ temp = mData[parent];
+ mData[parent] = mData[child];
+ mData[child] = temp;
+ child = parent;
+ }
+ }
+
+ protected void percolateDown(int start) {
+ int parent = start;
+ while (true) {
+ int smallest = parent;
+ int firstChild = 3 * parent + 1;
+ int secondChild = 3 * parent + 2;
+ int thirdChild = 3 * parent + 3;
+
+ if (firstChild < size && mData[firstChild] < mData[smallest]) {
+ smallest = firstChild;
+ }
+
+ if (secondChild < size && mData[secondChild] < mData[smallest]) {
+ smallest = secondChild;
+ }
+
+ if (thirdChild < size && mData[thirdChild] < mData[smallest]) {
+ smallest = thirdChild;
+ }
+
+ if (smallest != parent) {
+ int temp = mData[parent];
+ mData[parent] = mData[smallest];
+ mData[smallest] = temp;
+ parent = smallest;
+ } else {
+ break;
+ }
+ }
+ }
+}
diff --git a/hw/hw-13/hw-13/src/TestHeap.java b/hw/hw-13/hw-13/src/TestHeap.java
new file mode 100644
index 0000000..f2f6d92
--- /dev/null
+++ b/hw/hw-13/hw-13/src/TestHeap.java
@@ -0,0 +1,228 @@
+import static org.junit.jupiter.api.Assertions.*;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class TestHeap {
+ TernaryHeap h;
+
+ @BeforeEach
+ void setUp() throws Exception {
+ h = new TernaryHeap();
+ }
+
+ @Test
+ void testAdd01() { //1
+ h.add(5);
+ h.add(9);
+ h.add(8);
+ h.add(6);
+ h.add(4);
+
+ assertEquals(5, h.size);
+ assertEquals(4, h.mData[0]);
+ assertEquals(5, h.mData[1]);
+ assertEquals(8, h.mData[2]);
+ assertEquals(6, h.mData[3]);
+ assertEquals(9, h.mData[4]);
+
+ }
+
+ @Test
+ void testAdd2() { //1
+ h.mData[0] = 4;
+ h.mData[1] = 5;
+ h.mData[2] = 8;
+ h.mData[3] = 6;
+ h.mData[4] = 9;
+ h.mData[5] = 10;
+ h.mData[6] = 15;
+ h.mData[7] = 20;
+ h.size = 8;
+
+ h.add(7);
+ assertEquals(9, h.size);
+ assertEquals(4, h.mData[0]);
+ assertEquals(5, h.mData[1]);
+ assertEquals(7, h.mData[2]);
+ assertEquals(6, h.mData[3]);
+ assertEquals(9, h.mData[4]);
+ assertEquals(10, h.mData[5]);
+ assertEquals(15, h.mData[6]);
+ assertEquals(20, h.mData[7]);
+ assertEquals(8, h.mData[8]);
+ }
+
+ @Test
+ void testAdd3() { //1
+ h.mData[0] = 4;
+ h.mData[1] = 5;
+ h.mData[2] = 7;
+ h.mData[3] = 6;
+ h.mData[4] = 9;
+ h.mData[5] = 10;
+ h.mData[6] = 15;
+ h.mData[7] = 20;
+ h.mData[8] = 8;
+ h.mData[9] = 13;
+ h.mData[10] = 12;
+ h.mData[11] = 18;
+ h.size = 12;
+
+ h.add(3);
+ assertEquals(13, h.size);
+ assertEquals(3, h.mData[0]);
+ assertEquals(5, h.mData[1]);
+ assertEquals(7, h.mData[2]);
+ assertEquals(4, h.mData[3]);
+ assertEquals(9, h.mData[4]);
+
+ assertEquals(10, h.mData[5]);
+ assertEquals(15, h.mData[6]);
+ assertEquals(20, h.mData[7]);
+ assertEquals(8, h.mData[8]);
+ assertEquals(13, h.mData[9]);
+
+ assertEquals(12, h.mData[10]);
+ assertEquals(18, h.mData[11]);
+ assertEquals(6, h.mData[12]);
+
+ }
+
+ @Test
+ void testPop01() throws Exception { //2
+ h.mData[0] = 3;
+ h.mData[1] = 5;
+ h.mData[2] = 7;
+ h.mData[3] = 4;
+ h.mData[4] = 9;
+ h.mData[5] = 10;
+ h.mData[6] = 15;
+ h.mData[7] = 20;
+ h.mData[8] = 8;
+ h.mData[9] = 13;
+ h.mData[10] = 18;
+ h.mData[11] = 12;
+ h.mData[12] = 25;
+ h.size = 13;
+
+ h.pop();
+
+ assertEquals(12, h.size);
+ assertEquals(4, h.mData[0]);
+ assertEquals(5, h.mData[1]);
+ assertEquals(7, h.mData[2]);
+ assertEquals(12, h.mData[3]);
+ assertEquals(9, h.mData[4]);
+ assertEquals(10, h.mData[5]);
+ assertEquals(15, h.mData[6]);
+ assertEquals(20, h.mData[7]);
+ assertEquals(8, h.mData[8]);
+ assertEquals(13, h.mData[9]);
+ assertEquals(18, h.mData[10]);
+ assertEquals(25, h.mData[11]);
+
+ }
+
+ @Test
+ void testPop02() throws Exception { //2
+ h.mData[0] = 4;
+ h.mData[1] = 5;
+ h.mData[2] = 7;
+ h.mData[3] = 6;
+ h.mData[4] = 10;
+ h.mData[5] = 9;
+ h.mData[6] = 15;
+ h.mData[7] = 20;
+ h.mData[8] = 8;
+ h.mData[9] = 13;
+ h.mData[10] = 12;
+ h.mData[11] = 18;
+ h.size = 12;
+
+ h.pop();
+
+ assertEquals(11, h.size);
+ assertEquals(5, h.mData[0]);
+ assertEquals(9, h.mData[1]);
+ assertEquals(7, h.mData[2]);
+ assertEquals(6, h.mData[3]);
+ assertEquals(10, h.mData[4]);
+ assertEquals(18, h.mData[5]);
+ assertEquals(15, h.mData[6]);
+ assertEquals(20, h.mData[7]);
+ assertEquals(8, h.mData[8]);
+ assertEquals(13, h.mData[9]);
+ assertEquals(12, h.mData[10]);
+
+
+ }
+
+ @Test
+ void testPop03() throws Exception { //2
+ h.mData[0] = 5;
+ h.mData[1] = 9;
+ h.mData[2] = 6;
+ h.mData[3] = 7;
+ h.mData[4] = 10;
+ h.mData[5] = 18;
+ h.mData[6] = 15;
+ h.mData[7] = 20;
+ h.mData[8] = 13;
+ h.mData[9] = 8;
+ h.mData[10] = 12;
+ h.size = 11;
+
+ h.pop();
+
+ assertEquals(10, h.size);
+ assertEquals(6, h.mData[0]);
+ assertEquals(9, h.mData[1]);
+ assertEquals(8, h.mData[2]);
+ assertEquals(7, h.mData[3]);
+ assertEquals(10, h.mData[4]);
+ assertEquals(18, h.mData[5]);
+ assertEquals(15, h.mData[6]);
+ assertEquals(20, h.mData[7]);
+ assertEquals(13, h.mData[8]);
+ assertEquals(12, h.mData[9]);
+
+ }
+
+ @Test
+ void testPop04() throws Exception { //2
+ h.mData[0] = 3;
+ h.mData[1] = 5;
+ h.mData[2] = 7;
+ h.mData[3] = 4;
+ h.mData[4] = 9;
+ h.mData[5] = 10;
+ h.mData[6] = 15;
+ h.mData[7] = 20;
+ h.mData[8] = 8;
+ h.mData[9] = 13;
+ h.mData[10] = 12;
+ h.mData[11] = 18;
+ h.mData[12] = 25;
+ h.size = 13;
+
+ h.pop();
+
+ assertEquals(12, h.size);
+ assertEquals(4, h.mData[0]);
+ assertEquals(5, h.mData[1]);
+ assertEquals(7, h.mData[2]);
+ assertEquals(12, h.mData[3]);
+ assertEquals(9, h.mData[4]);
+ assertEquals(10, h.mData[5]);
+ assertEquals(15, h.mData[6]);
+ assertEquals(20, h.mData[7]);
+ assertEquals(8, h.mData[8]);
+ assertEquals(13, h.mData[9]);
+ assertEquals(25, h.mData[10]);
+ assertEquals(18, h.mData[11]);
+
+ }
+
+
+}
diff --git a/hw/hw-14/.DS_Store b/hw/hw-14/.DS_Store
new file mode 100644
index 0000000..a9d333f
Binary files /dev/null and b/hw/hw-14/.DS_Store differ
diff --git a/hw/hw-14/hw14_toStudent_65f9b184ab37a/hw14.PNG b/hw/hw-14/hw14_toStudent_65f9b184ab37a/hw14.PNG
new file mode 100644
index 0000000..2d9f151
Binary files /dev/null and b/hw/hw-14/hw14_toStudent_65f9b184ab37a/hw14.PNG differ
diff --git a/hw/hw-14/hw14_toStudent_65f9b184ab37a/hw14_Separate_Chaining.docx b/hw/hw-14/hw14_toStudent_65f9b184ab37a/hw14_Separate_Chaining.docx
new file mode 100644
index 0000000..44c505b
Binary files /dev/null and b/hw/hw-14/hw14_toStudent_65f9b184ab37a/hw14_Separate_Chaining.docx differ
diff --git a/hw/hw-15/.DS_Store b/hw/hw-15/.DS_Store
new file mode 100644
index 0000000..7be422e
Binary files /dev/null and b/hw/hw-15/.DS_Store differ
diff --git a/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/AVLTree.java b/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/AVLTree.java
new file mode 100644
index 0000000..29908f6
--- /dev/null
+++ b/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/AVLTree.java
@@ -0,0 +1,164 @@
+public class AVLTree {
+ AVLNode root;
+ int size;
+
+ public AVLTree() {
+ root = null;
+ size = 0;
+ }
+
+ public boolean isEmpty() {
+ return size == 0;
+ }
+
+ public void makeEmpty() {
+ root = null;
+ size = 0;
+ }
+
+ public Iterator findMin() {
+ return findMin(root);
+ }
+
+ public Iterator findMin(AVLNode n) {
+ if (n == null)
+ return null;
+ if (n.left == null) {
+ Iterator itr = new AVLTreeIterator(n);
+ return itr;
+ }
+ return findMin(n.left);
+ }
+
+ public Iterator findMax() {
+ return findMax(root);
+ }
+
+ public Iterator findMax(AVLNode n) {
+ if (n == null)
+ return null;
+ if (n.right == null) {
+ Iterator itr = new AVLTreeIterator(n);
+ return itr;
+ }
+ return findMax(n.right);
+ }
+
+ public Iterator find(int v) {
+ return find(v, root);
+ }
+
+ public Iterator find(int v, AVLNode n) {
+ if (n == null)
+ return null;
+ if (v == n.data)
+ return new AVLTreeIterator(n);
+ if (v < n.data)
+ return find(v, n.left);
+ else
+ return find(v, n.right);
+ }
+
+ public AVLNode insert(int v) {
+ return insert(v, root, null);
+ }
+
+ // return the node n after v was added into the tree
+ public AVLNode insert(int v, AVLNode n, AVLNode parent) {
+ if (n == null) {
+ n = new AVLNode(v, null, null, parent, 0);
+ size++;
+ } else if (v < n.data) {
+ n.left = insert(v, n.left, n);
+ } else if (v > n.data) {
+ n.right = insert(v, n.right, n);
+ }
+ n = rebalance(n);
+ return n;
+ }
+
+ public AVLNode remove(int v) {
+ return remove(v, root, null);
+ }
+
+ // return the node n after v was removed from the tree
+ public AVLNode remove(int v, AVLNode n, AVLNode parent) {
+ if (n == null)
+ ; // do nothing, there is nothing to be removed
+ else if (v < n.data) {
+ n.left = remove(v, n.left, n);
+ } else if (v > n.data) {
+ n.right = remove(v, n.right, n);
+ } else {
+ if (n.left == null && n.right == null) {
+ n = null;
+ size--;
+ } else if (n.left != null && n.right == null) {
+ n.left.parent = parent;
+ n = n.left;
+ size--;
+ } else if (n.right != null && n.left == null) {
+ n.right.parent = parent;
+ n = n.right;
+ size--;
+ } else {
+ AVLTreeIterator i = (AVLTreeIterator) findMin(n.right);
+ int minInRightSubtree = i.currentNode.data;
+ n.data = minInRightSubtree;
+ n.right = remove(minInRightSubtree, n.right, n);
+ }
+ }
+ n = rebalance(n);
+ return n;
+ }
+
+ public AVLNode rebalance(AVLNode n) {
+ if (n == null)
+ return n;
+ int balance = AVLNode.tiltDegree(n);
+ if (balance >= 2) {
+ if (AVLNode.tiltDegree(n.left) <= -1) // 3rd case
+ n.left = rotateRightChild(n.left);
+ n = rotateLeftChild(n); // 1st case
+ } else if (balance <= -2) {
+ if (AVLNode.tiltDegree(n.right) >= 1) // 4th case
+ n.right = rotateLeftChild(n.right);
+ n = rotateRightChild(n); // 2nd case
+ }
+ AVLNode.updateHeight(n);
+ return n;
+ }
+
+ public AVLNode rotateLeftChild(AVLNode n) {
+ AVLNode l = n.left;
+ AVLNode lr = n.left.right; // can be null
+ n.left = lr;
+ if (lr != null) {
+ lr.parent = n;
+ }
+ l.right = n;
+ l.parent = n.parent;
+ n.parent = l;
+
+ AVLNode.updateHeight(n);
+ AVLNode.updateHeight(l);
+ return l;
+ }
+
+ public AVLNode rotateRightChild(AVLNode n) {
+ AVLNode r = n.right;
+ AVLNode rl = n.right.left; // can be null
+ n.right = rl;
+ if (rl != null) {
+ rl.parent = n;
+ }
+ r.left = n;
+ r.parent = n.parent;
+ n.parent = r;
+
+ AVLNode.updateHeight(n);
+ AVLNode.updateHeight(r);
+ return r;
+ }
+
+}
diff --git a/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/BST.java b/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/BST.java
new file mode 100644
index 0000000..122b9e0
--- /dev/null
+++ b/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/BST.java
@@ -0,0 +1,214 @@
+
+public class BST {
+ BSTNode root;
+ int size;
+
+ public BST() {
+ root = null;
+ size = 0;
+ }
+
+ public BST(BSTNode root, int size) {
+ this.root = root;
+ this.size = size;
+ }
+
+ public boolean isEmpty() {
+ return size == 0;
+ }
+
+ public void makeEmpty() {
+ root = null;
+ size = 0;
+ }
+
+ public Iterator find(int v) {
+ BSTNode temp = root;
+
+ while (temp != null && temp.data != v) {
+ if (v < temp.data) {
+ temp = temp.left;
+ } else {
+ temp = temp.right;
+ }
+ }
+ if (temp == null) // not found
+ return null;
+ return new TreeIterator(temp);
+ }
+
+ public Iterator insert(int v) {
+ BSTNode parent = null;
+ BSTNode temp = root;
+
+ // This first part is almost the same as find,
+ // but it has an extra pointer called parent.
+ while (temp != null && temp.data != v) {
+ if (v < temp.data) {
+ parent = temp;
+ temp = temp.left;
+
+ } else {
+ parent = temp;
+ temp = temp.right;
+
+ }
+ }
+
+ if (temp == null) {
+ BSTNode n = new BSTNode(v, null, null, parent);
+ if (parent == null) {
+ root = n;
+ } else if (v < parent.data) {
+ parent.left = n;
+ } else {
+ parent.right = n;
+ }
+ size++;
+ return new TreeIterator(n);
+ } else {
+ // we do nothing since
+ // we don't want to add duplicated data.
+ return null;
+ }
+
+ }
+
+ public void remove(int v) {
+ BSTNode parent = null;
+ BSTNode temp = root;
+
+ TreeIterator i = (TreeIterator) find(v);
+ if (i == null) { // not found, we can not remove it
+ return;
+ }
+
+ temp = i.currentNode;
+ parent = temp.parent;
+
+ // otherwise, we remove the value.
+ size--;
+ if (temp.left == null && temp.right == null) {// both subtrees are empty
+ if (parent == null) {
+ root = null;
+ } else if (parent.left == temp) {
+ parent.left = null;
+ } else {
+ parent.right = null;
+ }
+ } else if (temp.left == null && temp.right != null) {// only right child
+ if (parent == null) {
+ root = temp.right;
+ root.parent = null;
+ } else if (parent.right == temp) {
+ BSTNode n = temp.right;
+ n.parent = parent;
+ parent.right = n;
+ // temp.right = null;
+ // temp.parent = null;
+ } else {// parent.left == temp
+ BSTNode n = temp.right;
+ n.parent = parent;
+ parent.left = n;
+ }
+ } else if (temp.right == null && temp.left != null) {
+ if (parent == null) {
+ root = temp.left;
+ root.parent = null;
+ } else if (parent.right == temp) {
+ BSTNode n = temp.left;
+ n.parent = parent;
+ parent.right = n;
+ } else {
+ BSTNode n = temp.left;
+ n.parent = parent;
+ parent.left = n;
+
+ }
+
+ } else {// temp has two subtrees
+ BSTNode n = temp.right;
+ TreeIterator itr = (TreeIterator)(findMin(n));
+ BSTNode minInSubtree = itr.currentNode;
+
+ temp.data = minInSubtree.data;
+
+ BSTNode parentOfMin = minInSubtree.parent;
+ if (parentOfMin.left == minInSubtree) {
+ parentOfMin.left = minInSubtree.right;
+
+ } else { // parentOfMin.right == minInSubtree
+ parentOfMin.right = minInSubtree.right;
+
+ }
+
+ if (minInSubtree.right != null) {
+ minInSubtree.right.parent = parentOfMin;
+ }
+
+ }
+ }
+
+ public Iterator findMin() {
+ BSTNode temp = root;
+ if(temp == null)
+ return null;
+ while (temp.left != null) {
+ temp = temp.left;
+ }
+ Iterator itr = new TreeIterator(temp);
+ return itr;
+ }
+
+ public Iterator findMin(BSTNode n) {
+ BSTNode temp = n;
+ if(temp == null)
+ return null;
+ while (temp.left != null) {
+ temp = temp.left;
+ }
+ Iterator itr = new TreeIterator(temp);
+ return itr;
+ }
+
+ public Iterator findMax() {
+ BSTNode temp = root;
+ if(temp == null)
+ return null;
+ while (temp.right != null) {
+ temp = temp.right;
+ }
+ Iterator itr = new TreeIterator(temp);
+ return itr;
+ }
+
+ public Iterator findMax(BSTNode n) {
+ BSTNode temp = n;
+ if(temp == null)
+ return null;
+ while (temp.right != null) {
+ temp = temp.right;
+ }
+ Iterator itr = new TreeIterator(temp);
+ return itr;
+ }
+
+ public static void main(String[] args) throws Exception {
+ //Printing example.
+ //You can print how the tree looks to help with debugging.
+
+ BSTNode r = new BSTNode(7);
+ BST t = new BST(r, 1);
+ t.insert(3);
+ t.insert(11);
+ t.insert(2);
+ t.insert(5);
+ t.insert(8);
+
+
+
+
+
+ }
+
+}
diff --git a/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/BSTNode.java b/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/BSTNode.java
new file mode 100644
index 0000000..147dfec
--- /dev/null
+++ b/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/BSTNode.java
@@ -0,0 +1,23 @@
+
+
+public class BSTNode {
+ int data; // value stored in the node.
+ BSTNode left; //pointer to lower left BSTNode.
+ BSTNode right; //pointer to lower right BSTNode.
+ BSTNode parent; //pointer to the BSTNode above.
+
+ public BSTNode(int data){
+ this(data,null,null,null);
+ }
+
+ public BSTNode(int data, BSTNode left, BSTNode right, BSTNode parent) {
+ this.data = data;
+ this.left = left;
+ this.right = right;
+ this.parent = parent;
+ }
+
+ public static void main(String[] args) {
+ BSTNode b = new BSTNode(9);
+ }
+}
diff --git a/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/Iterator.java b/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/Iterator.java
new file mode 100644
index 0000000..3b2365e
--- /dev/null
+++ b/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/Iterator.java
@@ -0,0 +1,19 @@
+
+
+public interface Iterator {
+
+ public boolean hasNext();
+
+ public boolean hasPrevious();
+
+ public int next() throws Exception;
+ // move iterator to the next position,
+ // then returns the value at that position.
+
+ public int previous() throws Exception;
+ // return the value at current position,
+ // then move the iterator back one position.
+
+ public void set(int value);
+
+}
diff --git a/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/TestTreap.java b/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/TestTreap.java
new file mode 100644
index 0000000..3b8d1a2
--- /dev/null
+++ b/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/TestTreap.java
@@ -0,0 +1,81 @@
+import static org.junit.jupiter.api.Assertions.*;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class TestTreap {
+ Treap t;
+ @BeforeEach
+ void setUp() throws Exception {
+ t = new Treap();
+ }
+
+ @Test
+ void testInsert01() {//1
+ t.insert(4, 5);
+ t.insert(2, 3);
+
+ assertEquals(2, t.size);
+ assertEquals(2, t.root.bstValue);
+ assertEquals(4, t.root.right.bstValue);
+ }
+
+ @Test
+ void testInsert02() {//1
+ t.insert(4, 5);
+ t.insert(8, 1);
+
+ assertEquals(2, t.size);
+ assertEquals(8, t.root.bstValue);
+ assertEquals(4, t.root.left.bstValue);
+
+ }
+
+ @Test
+ void testInsert03() {//2
+ t.insert(4, 5);
+ t.insert(8, 8);
+ t.insert(7, 6);
+
+ assertEquals(3, t.size);
+ assertEquals(4, t.root.bstValue);
+ assertEquals(7, t.root.right.bstValue);
+ assertEquals(8, t.root.right.right.bstValue);
+ }
+
+ @Test
+ void testInsert04() {//3
+ t.insert(10, 50);
+ t.insert(5, 70);
+ t.insert(15, 60);
+ t.insert(3, 30);
+
+ assertEquals(4, t.size);
+ assertEquals(3, t.root.bstValue);
+ assertEquals(10, t.root.right.bstValue);
+ assertEquals(15, t.root.right.right.bstValue);
+ assertEquals(5, t.root.right.left.bstValue);
+ }
+
+ @Test
+ void testInsert05() {//4
+ t.insert(30, 50);
+ t.insert(70, 75);
+ t.insert(15, 60);
+ t.insert(13, 120);
+ t.insert(18, 110);
+ t.insert(16, 180);
+ t.insert(19, 30);
+ assertEquals(7, t.size);
+
+ assertEquals(19, t.root.bstValue);
+ assertEquals(15, t.root.left.bstValue);
+ assertEquals(30, t.root.right.bstValue);
+ assertEquals(13, t.root.left.left.bstValue);
+ assertEquals(18, t.root.left.right.bstValue);
+ assertEquals(70, t.root.right.right.bstValue);
+ assertEquals(16, t.root.left.right.left.bstValue);
+ }
+
+
+}
diff --git a/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/TreapNode.java b/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/TreapNode.java
new file mode 100644
index 0000000..66e0064
--- /dev/null
+++ b/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/TreapNode.java
@@ -0,0 +1,26 @@
+
+
+public class TreapNode {
+ int bstValue; // value stored in the node.
+ int heapValue; //priority value as in a min heap
+
+ TreapNode left; //pointer to lower left node.
+ TreapNode right; //pointer to lower right node.
+ TreapNode parent; //pointer to the node above.
+
+ public TreapNode(int data, int heapValue){
+ this(data,heapValue,null,null,null);
+ }
+
+ public TreapNode(int data, int hv, TreapNode left, TreapNode right, TreapNode parent) {
+ bstValue = data;
+ heapValue = hv;
+ this.left = left;
+ this.right = right;
+ this.parent = parent;
+ }
+
+ public static void main(String[] args) {
+ TreapNode b = new TreapNode(9,50);
+ }
+}
diff --git a/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/TreeIterator.java b/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/TreeIterator.java
new file mode 100644
index 0000000..b555d4e
--- /dev/null
+++ b/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/TreeIterator.java
@@ -0,0 +1,113 @@
+
+
+import java.util.NoSuchElementException;
+
+public class TreeIterator implements Iterator {
+
+ BSTNode currentNode;
+
+ public TreeIterator(BSTNode currentNode) {
+ this.currentNode = currentNode;
+ }
+
+ @Override
+ public boolean hasNext() {
+ BSTNode temp = currentNode;
+
+ if (temp.right != null) {
+ return true;
+ }
+
+ BSTNode p = temp.parent;
+ while (p != null && p.right == temp) {
+ temp = p;
+ p = temp.parent;
+ }
+
+ if (p == null)
+ return false;
+ else
+ return true;
+ }
+
+ @Override
+ public boolean hasPrevious() {
+ BSTNode temp = currentNode;
+
+ if (temp.left != null) {
+ return true;
+ }
+
+ BSTNode p = temp.parent;
+ while (p != null && p.left == temp) {
+ temp = p;
+ p = temp.parent;
+ }
+
+ if (p == null)
+ return false;
+ else
+ return true;
+
+ }
+
+ @Override
+ public int next() throws Exception {
+ // Throw exception if the next data
+ // does not exist.
+ BSTNode temp = currentNode;
+
+ if (temp.right != null) {
+ temp = temp.right;
+ while (temp.left != null) {
+ temp = temp.left;
+ }
+ } else {
+ BSTNode p = temp.parent;
+ while (p != null && p.right == temp) {
+ temp = p;
+ p = temp.parent;
+ }
+ temp = p;
+ }
+
+ if (temp == null) // hasNext() == false
+ throw new NoSuchElementException();
+ currentNode = temp;
+ return currentNode.data;
+ }
+
+ @Override
+ public int previous() throws Exception {
+ // Throw exception if the previous data
+ // does not exist.
+ BSTNode temp = currentNode;
+ int d = currentNode.data;
+
+ if (temp.left != null) {
+ temp = temp.left;
+ while (temp.right != null) {
+ temp = temp.right;
+ }
+ } else {
+ BSTNode p = temp.parent;
+ while (p != null && p.left == temp) {
+ temp = p;
+ p = temp.parent;
+ }
+ temp = p;
+ }
+
+ if (temp == null) // hasPrevious() == false
+ throw new NoSuchElementException();
+ currentNode = temp;
+ return d;
+
+ }
+
+ @Override
+ public void set(int value) {
+ currentNode.data = value;
+ }
+
+}
diff --git a/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/hw15 - Treap (AVL).docx b/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/hw15 - Treap (AVL).docx
new file mode 100644
index 0000000..a02a351
Binary files /dev/null and b/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/hw15 - Treap (AVL).docx differ
diff --git a/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/hw15 - Treap (AVL).pdf b/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/hw15 - Treap (AVL).pdf
new file mode 100644
index 0000000..3c2e911
Binary files /dev/null and b/hw/hw-15/Hw15_toStudent_65f9ae8d6b1ab/hw15 - Treap (AVL).pdf differ
diff --git a/hw/hw-15/hw-15/.DS_Store b/hw/hw-15/hw-15/.DS_Store
new file mode 100644
index 0000000..1471c14
Binary files /dev/null and b/hw/hw-15/hw-15/.DS_Store differ
diff --git a/hw/hw-15/hw-15/.classpath b/hw/hw-15/hw-15/.classpath
new file mode 100644
index 0000000..86473c8
--- /dev/null
+++ b/hw/hw-15/hw-15/.classpath
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/hw/hw-15/hw-15/.gitignore b/hw/hw-15/hw-15/.gitignore
new file mode 100644
index 0000000..ae3c172
--- /dev/null
+++ b/hw/hw-15/hw-15/.gitignore
@@ -0,0 +1 @@
+/bin/
diff --git a/hw/hw-15/hw-15/.project b/hw/hw-15/hw-15/.project
new file mode 100644
index 0000000..c6cbbf9
--- /dev/null
+++ b/hw/hw-15/hw-15/.project
@@ -0,0 +1,17 @@
+
+
+ hw-15
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/hw/hw-15/hw-15/.settings/org.eclipse.core.resources.prefs b/hw/hw-15/hw-15/.settings/org.eclipse.core.resources.prefs
new file mode 100644
index 0000000..99f26c0
--- /dev/null
+++ b/hw/hw-15/hw-15/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,2 @@
+eclipse.preferences.version=1
+encoding/=UTF-8
diff --git a/hw/hw-15/hw-15/.settings/org.eclipse.jdt.core.prefs b/hw/hw-15/hw-15/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..ccfd559
--- /dev/null
+++ b/hw/hw-15/hw-15/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,14 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=20
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=20
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
+org.eclipse.jdt.core.compiler.release=enabled
+org.eclipse.jdt.core.compiler.source=20
diff --git a/hw/hw-15/hw-15/src/AVLTree.java b/hw/hw-15/hw-15/src/AVLTree.java
new file mode 100644
index 0000000..29908f6
--- /dev/null
+++ b/hw/hw-15/hw-15/src/AVLTree.java
@@ -0,0 +1,164 @@
+public class AVLTree {
+ AVLNode root;
+ int size;
+
+ public AVLTree() {
+ root = null;
+ size = 0;
+ }
+
+ public boolean isEmpty() {
+ return size == 0;
+ }
+
+ public void makeEmpty() {
+ root = null;
+ size = 0;
+ }
+
+ public Iterator findMin() {
+ return findMin(root);
+ }
+
+ public Iterator findMin(AVLNode n) {
+ if (n == null)
+ return null;
+ if (n.left == null) {
+ Iterator itr = new AVLTreeIterator(n);
+ return itr;
+ }
+ return findMin(n.left);
+ }
+
+ public Iterator findMax() {
+ return findMax(root);
+ }
+
+ public Iterator findMax(AVLNode n) {
+ if (n == null)
+ return null;
+ if (n.right == null) {
+ Iterator itr = new AVLTreeIterator(n);
+ return itr;
+ }
+ return findMax(n.right);
+ }
+
+ public Iterator find(int v) {
+ return find(v, root);
+ }
+
+ public Iterator find(int v, AVLNode n) {
+ if (n == null)
+ return null;
+ if (v == n.data)
+ return new AVLTreeIterator(n);
+ if (v < n.data)
+ return find(v, n.left);
+ else
+ return find(v, n.right);
+ }
+
+ public AVLNode insert(int v) {
+ return insert(v, root, null);
+ }
+
+ // return the node n after v was added into the tree
+ public AVLNode insert(int v, AVLNode n, AVLNode parent) {
+ if (n == null) {
+ n = new AVLNode(v, null, null, parent, 0);
+ size++;
+ } else if (v < n.data) {
+ n.left = insert(v, n.left, n);
+ } else if (v > n.data) {
+ n.right = insert(v, n.right, n);
+ }
+ n = rebalance(n);
+ return n;
+ }
+
+ public AVLNode remove(int v) {
+ return remove(v, root, null);
+ }
+
+ // return the node n after v was removed from the tree
+ public AVLNode remove(int v, AVLNode n, AVLNode parent) {
+ if (n == null)
+ ; // do nothing, there is nothing to be removed
+ else if (v < n.data) {
+ n.left = remove(v, n.left, n);
+ } else if (v > n.data) {
+ n.right = remove(v, n.right, n);
+ } else {
+ if (n.left == null && n.right == null) {
+ n = null;
+ size--;
+ } else if (n.left != null && n.right == null) {
+ n.left.parent = parent;
+ n = n.left;
+ size--;
+ } else if (n.right != null && n.left == null) {
+ n.right.parent = parent;
+ n = n.right;
+ size--;
+ } else {
+ AVLTreeIterator i = (AVLTreeIterator) findMin(n.right);
+ int minInRightSubtree = i.currentNode.data;
+ n.data = minInRightSubtree;
+ n.right = remove(minInRightSubtree, n.right, n);
+ }
+ }
+ n = rebalance(n);
+ return n;
+ }
+
+ public AVLNode rebalance(AVLNode n) {
+ if (n == null)
+ return n;
+ int balance = AVLNode.tiltDegree(n);
+ if (balance >= 2) {
+ if (AVLNode.tiltDegree(n.left) <= -1) // 3rd case
+ n.left = rotateRightChild(n.left);
+ n = rotateLeftChild(n); // 1st case
+ } else if (balance <= -2) {
+ if (AVLNode.tiltDegree(n.right) >= 1) // 4th case
+ n.right = rotateLeftChild(n.right);
+ n = rotateRightChild(n); // 2nd case
+ }
+ AVLNode.updateHeight(n);
+ return n;
+ }
+
+ public AVLNode rotateLeftChild(AVLNode n) {
+ AVLNode l = n.left;
+ AVLNode lr = n.left.right; // can be null
+ n.left = lr;
+ if (lr != null) {
+ lr.parent = n;
+ }
+ l.right = n;
+ l.parent = n.parent;
+ n.parent = l;
+
+ AVLNode.updateHeight(n);
+ AVLNode.updateHeight(l);
+ return l;
+ }
+
+ public AVLNode rotateRightChild(AVLNode n) {
+ AVLNode r = n.right;
+ AVLNode rl = n.right.left; // can be null
+ n.right = rl;
+ if (rl != null) {
+ rl.parent = n;
+ }
+ r.left = n;
+ r.parent = n.parent;
+ n.parent = r;
+
+ AVLNode.updateHeight(n);
+ AVLNode.updateHeight(r);
+ return r;
+ }
+
+}
diff --git a/hw/hw-15/hw-15/src/BST.java b/hw/hw-15/hw-15/src/BST.java
new file mode 100644
index 0000000..122b9e0
--- /dev/null
+++ b/hw/hw-15/hw-15/src/BST.java
@@ -0,0 +1,214 @@
+
+public class BST {
+ BSTNode root;
+ int size;
+
+ public BST() {
+ root = null;
+ size = 0;
+ }
+
+ public BST(BSTNode root, int size) {
+ this.root = root;
+ this.size = size;
+ }
+
+ public boolean isEmpty() {
+ return size == 0;
+ }
+
+ public void makeEmpty() {
+ root = null;
+ size = 0;
+ }
+
+ public Iterator find(int v) {
+ BSTNode temp = root;
+
+ while (temp != null && temp.data != v) {
+ if (v < temp.data) {
+ temp = temp.left;
+ } else {
+ temp = temp.right;
+ }
+ }
+ if (temp == null) // not found
+ return null;
+ return new TreeIterator(temp);
+ }
+
+ public Iterator insert(int v) {
+ BSTNode parent = null;
+ BSTNode temp = root;
+
+ // This first part is almost the same as find,
+ // but it has an extra pointer called parent.
+ while (temp != null && temp.data != v) {
+ if (v < temp.data) {
+ parent = temp;
+ temp = temp.left;
+
+ } else {
+ parent = temp;
+ temp = temp.right;
+
+ }
+ }
+
+ if (temp == null) {
+ BSTNode n = new BSTNode(v, null, null, parent);
+ if (parent == null) {
+ root = n;
+ } else if (v < parent.data) {
+ parent.left = n;
+ } else {
+ parent.right = n;
+ }
+ size++;
+ return new TreeIterator(n);
+ } else {
+ // we do nothing since
+ // we don't want to add duplicated data.
+ return null;
+ }
+
+ }
+
+ public void remove(int v) {
+ BSTNode parent = null;
+ BSTNode temp = root;
+
+ TreeIterator i = (TreeIterator) find(v);
+ if (i == null) { // not found, we can not remove it
+ return;
+ }
+
+ temp = i.currentNode;
+ parent = temp.parent;
+
+ // otherwise, we remove the value.
+ size--;
+ if (temp.left == null && temp.right == null) {// both subtrees are empty
+ if (parent == null) {
+ root = null;
+ } else if (parent.left == temp) {
+ parent.left = null;
+ } else {
+ parent.right = null;
+ }
+ } else if (temp.left == null && temp.right != null) {// only right child
+ if (parent == null) {
+ root = temp.right;
+ root.parent = null;
+ } else if (parent.right == temp) {
+ BSTNode n = temp.right;
+ n.parent = parent;
+ parent.right = n;
+ // temp.right = null;
+ // temp.parent = null;
+ } else {// parent.left == temp
+ BSTNode n = temp.right;
+ n.parent = parent;
+ parent.left = n;
+ }
+ } else if (temp.right == null && temp.left != null) {
+ if (parent == null) {
+ root = temp.left;
+ root.parent = null;
+ } else if (parent.right == temp) {
+ BSTNode n = temp.left;
+ n.parent = parent;
+ parent.right = n;
+ } else {
+ BSTNode n = temp.left;
+ n.parent = parent;
+ parent.left = n;
+
+ }
+
+ } else {// temp has two subtrees
+ BSTNode n = temp.right;
+ TreeIterator itr = (TreeIterator)(findMin(n));
+ BSTNode minInSubtree = itr.currentNode;
+
+ temp.data = minInSubtree.data;
+
+ BSTNode parentOfMin = minInSubtree.parent;
+ if (parentOfMin.left == minInSubtree) {
+ parentOfMin.left = minInSubtree.right;
+
+ } else { // parentOfMin.right == minInSubtree
+ parentOfMin.right = minInSubtree.right;
+
+ }
+
+ if (minInSubtree.right != null) {
+ minInSubtree.right.parent = parentOfMin;
+ }
+
+ }
+ }
+
+ public Iterator findMin() {
+ BSTNode temp = root;
+ if(temp == null)
+ return null;
+ while (temp.left != null) {
+ temp = temp.left;
+ }
+ Iterator itr = new TreeIterator(temp);
+ return itr;
+ }
+
+ public Iterator findMin(BSTNode n) {
+ BSTNode temp = n;
+ if(temp == null)
+ return null;
+ while (temp.left != null) {
+ temp = temp.left;
+ }
+ Iterator itr = new TreeIterator(temp);
+ return itr;
+ }
+
+ public Iterator findMax() {
+ BSTNode temp = root;
+ if(temp == null)
+ return null;
+ while (temp.right != null) {
+ temp = temp.right;
+ }
+ Iterator itr = new TreeIterator(temp);
+ return itr;
+ }
+
+ public Iterator findMax(BSTNode n) {
+ BSTNode temp = n;
+ if(temp == null)
+ return null;
+ while (temp.right != null) {
+ temp = temp.right;
+ }
+ Iterator itr = new TreeIterator(temp);
+ return itr;
+ }
+
+ public static void main(String[] args) throws Exception {
+ //Printing example.
+ //You can print how the tree looks to help with debugging.
+
+ BSTNode r = new BSTNode(7);
+ BST t = new BST(r, 1);
+ t.insert(3);
+ t.insert(11);
+ t.insert(2);
+ t.insert(5);
+ t.insert(8);
+
+
+
+
+
+ }
+
+}
diff --git a/hw/hw-15/hw-15/src/BSTNode.java b/hw/hw-15/hw-15/src/BSTNode.java
new file mode 100644
index 0000000..147dfec
--- /dev/null
+++ b/hw/hw-15/hw-15/src/BSTNode.java
@@ -0,0 +1,23 @@
+
+
+public class BSTNode {
+ int data; // value stored in the node.
+ BSTNode left; //pointer to lower left BSTNode.
+ BSTNode right; //pointer to lower right BSTNode.
+ BSTNode parent; //pointer to the BSTNode above.
+
+ public BSTNode(int data){
+ this(data,null,null,null);
+ }
+
+ public BSTNode(int data, BSTNode left, BSTNode right, BSTNode parent) {
+ this.data = data;
+ this.left = left;
+ this.right = right;
+ this.parent = parent;
+ }
+
+ public static void main(String[] args) {
+ BSTNode b = new BSTNode(9);
+ }
+}
diff --git a/hw/hw-15/hw-15/src/Iterator.java b/hw/hw-15/hw-15/src/Iterator.java
new file mode 100644
index 0000000..3b2365e
--- /dev/null
+++ b/hw/hw-15/hw-15/src/Iterator.java
@@ -0,0 +1,19 @@
+
+
+public interface Iterator {
+
+ public boolean hasNext();
+
+ public boolean hasPrevious();
+
+ public int next() throws Exception;
+ // move iterator to the next position,
+ // then returns the value at that position.
+
+ public int previous() throws Exception;
+ // return the value at current position,
+ // then move the iterator back one position.
+
+ public void set(int value);
+
+}
diff --git a/hw/hw-15/hw-15/src/TestTreap.java b/hw/hw-15/hw-15/src/TestTreap.java
new file mode 100644
index 0000000..3b8d1a2
--- /dev/null
+++ b/hw/hw-15/hw-15/src/TestTreap.java
@@ -0,0 +1,81 @@
+import static org.junit.jupiter.api.Assertions.*;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class TestTreap {
+ Treap t;
+ @BeforeEach
+ void setUp() throws Exception {
+ t = new Treap();
+ }
+
+ @Test
+ void testInsert01() {//1
+ t.insert(4, 5);
+ t.insert(2, 3);
+
+ assertEquals(2, t.size);
+ assertEquals(2, t.root.bstValue);
+ assertEquals(4, t.root.right.bstValue);
+ }
+
+ @Test
+ void testInsert02() {//1
+ t.insert(4, 5);
+ t.insert(8, 1);
+
+ assertEquals(2, t.size);
+ assertEquals(8, t.root.bstValue);
+ assertEquals(4, t.root.left.bstValue);
+
+ }
+
+ @Test
+ void testInsert03() {//2
+ t.insert(4, 5);
+ t.insert(8, 8);
+ t.insert(7, 6);
+
+ assertEquals(3, t.size);
+ assertEquals(4, t.root.bstValue);
+ assertEquals(7, t.root.right.bstValue);
+ assertEquals(8, t.root.right.right.bstValue);
+ }
+
+ @Test
+ void testInsert04() {//3
+ t.insert(10, 50);
+ t.insert(5, 70);
+ t.insert(15, 60);
+ t.insert(3, 30);
+
+ assertEquals(4, t.size);
+ assertEquals(3, t.root.bstValue);
+ assertEquals(10, t.root.right.bstValue);
+ assertEquals(15, t.root.right.right.bstValue);
+ assertEquals(5, t.root.right.left.bstValue);
+ }
+
+ @Test
+ void testInsert05() {//4
+ t.insert(30, 50);
+ t.insert(70, 75);
+ t.insert(15, 60);
+ t.insert(13, 120);
+ t.insert(18, 110);
+ t.insert(16, 180);
+ t.insert(19, 30);
+ assertEquals(7, t.size);
+
+ assertEquals(19, t.root.bstValue);
+ assertEquals(15, t.root.left.bstValue);
+ assertEquals(30, t.root.right.bstValue);
+ assertEquals(13, t.root.left.left.bstValue);
+ assertEquals(18, t.root.left.right.bstValue);
+ assertEquals(70, t.root.right.right.bstValue);
+ assertEquals(16, t.root.left.right.left.bstValue);
+ }
+
+
+}
diff --git a/hw/hw-15/hw-15/src/Treap.java b/hw/hw-15/hw-15/src/Treap.java
new file mode 100644
index 0000000..eca0239
--- /dev/null
+++ b/hw/hw-15/hw-15/src/Treap.java
@@ -0,0 +1,93 @@
+
+public class Treap {
+ TreapNode root;
+ int size;
+
+ public Treap() {
+ root = null;
+ size = 0;
+ }
+
+ public TreapNode insert(int v, int h) {
+ TreapNode n = insert(v, h, root, null);
+ if(root.parent != null) root = root.parent;
+ return n;
+ }
+
+ public TreapNode insert(int v, int h, TreapNode n, TreapNode parent) {
+ if (size == 0) {
+ this.root = new TreapNode(v, h, null, null, parent);
+ size++;
+ return n;
+ }
+ if (n == null) {
+ n = new TreapNode(v, h, null, null, parent);
+ size++;
+ } else if (v < n.bstValue) {
+ n.left = insert(v, h, n.left, n);
+ } else if (v > n.bstValue) {
+ n.right = insert(v, h, n.right, n);
+ }
+ n = rebalance(n);
+ return n;
+ }
+
+ public TreapNode rebalance(TreapNode n) {
+ if (n == null)
+ return n;
+ if(n.left == null && n.right == null) return n;
+ if(n.left == null || n.right == null) {
+ if(n.left == null) {
+ if(n.right.heapValue < n.heapValue) {
+ n = rotateRightChild(n);
+ }
+ }
+ else if(n.right == null) {
+ if(n.left.heapValue < n.heapValue) {
+ n = rotateLeftChild(n);
+ }
+ }
+ }
+ else {
+ if(n.left.heapValue < n.heapValue) {
+ //rotate left
+ n = rotateLeftChild(n);
+ }
+ else if(n.right.heapValue < n.heapValue) {
+ //rotate right
+ n = rotateRightChild(n);
+ }
+ }
+
+ return n;
+ }
+
+ public TreapNode rotateLeftChild(TreapNode n) {
+ TreapNode l = n.left;
+ TreapNode lr = n.left.right; // can be null
+ n.left = lr;
+ if (lr != null) {
+ lr.parent = n;
+ }
+ l.right = n;
+ l.parent = n.parent;
+ n.parent = l;
+
+ return l;
+ }
+
+ public TreapNode rotateRightChild(TreapNode n) {
+ TreapNode r = n.right;
+ TreapNode rl = n.right.left; // can be null
+ n.right = rl;
+ if (rl != null) {
+ rl.parent = n;
+ }
+ r.left = n;
+ r.parent = n.parent;
+ n.parent = r;
+
+ return r;
+ }
+
+}
diff --git a/hw/hw-15/hw-15/src/TreapNode.java b/hw/hw-15/hw-15/src/TreapNode.java
new file mode 100644
index 0000000..66e0064
--- /dev/null
+++ b/hw/hw-15/hw-15/src/TreapNode.java
@@ -0,0 +1,26 @@
+
+
+public class TreapNode {
+ int bstValue; // value stored in the node.
+ int heapValue; //priority value as in a min heap
+
+ TreapNode left; //pointer to lower left node.
+ TreapNode right; //pointer to lower right node.
+ TreapNode parent; //pointer to the node above.
+
+ public TreapNode(int data, int heapValue){
+ this(data,heapValue,null,null,null);
+ }
+
+ public TreapNode(int data, int hv, TreapNode left, TreapNode right, TreapNode parent) {
+ bstValue = data;
+ heapValue = hv;
+ this.left = left;
+ this.right = right;
+ this.parent = parent;
+ }
+
+ public static void main(String[] args) {
+ TreapNode b = new TreapNode(9,50);
+ }
+}
diff --git a/hw/hw-15/hw-15/src/TreeIterator.java b/hw/hw-15/hw-15/src/TreeIterator.java
new file mode 100644
index 0000000..b555d4e
--- /dev/null
+++ b/hw/hw-15/hw-15/src/TreeIterator.java
@@ -0,0 +1,113 @@
+
+
+import java.util.NoSuchElementException;
+
+public class TreeIterator implements Iterator {
+
+ BSTNode currentNode;
+
+ public TreeIterator(BSTNode currentNode) {
+ this.currentNode = currentNode;
+ }
+
+ @Override
+ public boolean hasNext() {
+ BSTNode temp = currentNode;
+
+ if (temp.right != null) {
+ return true;
+ }
+
+ BSTNode p = temp.parent;
+ while (p != null && p.right == temp) {
+ temp = p;
+ p = temp.parent;
+ }
+
+ if (p == null)
+ return false;
+ else
+ return true;
+ }
+
+ @Override
+ public boolean hasPrevious() {
+ BSTNode temp = currentNode;
+
+ if (temp.left != null) {
+ return true;
+ }
+
+ BSTNode p = temp.parent;
+ while (p != null && p.left == temp) {
+ temp = p;
+ p = temp.parent;
+ }
+
+ if (p == null)
+ return false;
+ else
+ return true;
+
+ }
+
+ @Override
+ public int next() throws Exception {
+ // Throw exception if the next data
+ // does not exist.
+ BSTNode temp = currentNode;
+
+ if (temp.right != null) {
+ temp = temp.right;
+ while (temp.left != null) {
+ temp = temp.left;
+ }
+ } else {
+ BSTNode p = temp.parent;
+ while (p != null && p.right == temp) {
+ temp = p;
+ p = temp.parent;
+ }
+ temp = p;
+ }
+
+ if (temp == null) // hasNext() == false
+ throw new NoSuchElementException();
+ currentNode = temp;
+ return currentNode.data;
+ }
+
+ @Override
+ public int previous() throws Exception {
+ // Throw exception if the previous data
+ // does not exist.
+ BSTNode temp = currentNode;
+ int d = currentNode.data;
+
+ if (temp.left != null) {
+ temp = temp.left;
+ while (temp.right != null) {
+ temp = temp.right;
+ }
+ } else {
+ BSTNode p = temp.parent;
+ while (p != null && p.left == temp) {
+ temp = p;
+ p = temp.parent;
+ }
+ temp = p;
+ }
+
+ if (temp == null) // hasPrevious() == false
+ throw new NoSuchElementException();
+ currentNode = temp;
+ return d;
+
+ }
+
+ @Override
+ public void set(int value) {
+ currentNode.data = value;
+ }
+
+}
diff --git a/lab/.DS_Store b/lab/.DS_Store
index 4eead7b..036ae1f 100644
Binary files a/lab/.DS_Store and b/lab/.DS_Store differ
diff --git a/lab/lab-02/Lab02_6538075121.jar b/lab/lab-02/Lab02_6538075121.jar
index 4655499..08ba856 100644
Binary files a/lab/lab-02/Lab02_6538075121.jar and b/lab/lab-02/Lab02_6538075121.jar differ
diff --git a/lab/lab-02/Lab02_6538075121_new.jar b/lab/lab-02/Lab02_6538075121_new.jar
new file mode 100644
index 0000000..8ca85cc
Binary files /dev/null and b/lab/lab-02/Lab02_6538075121_new.jar differ
diff --git a/lab/lab-04/6538075121_Lab04.jar b/lab/lab-04/6538075121_Lab04.jar
index 1e412b4..9aa4cbc 100644
Binary files a/lab/lab-04/6538075121_Lab04.jar and b/lab/lab-04/6538075121_Lab04.jar differ
diff --git a/lab/lab-05/Lab05_6538075121.jar b/lab/lab-05/Lab05_6538075121.jar
index eba702f..411aa98 100644
Binary files a/lab/lab-05/Lab05_6538075121.jar and b/lab/lab-05/Lab05_6538075121.jar differ
diff --git a/lab/lab-06/Lab06_6538075121.jar b/lab/lab-06/Lab06_6538075121.jar
index 94f774c..66744b8 100644
Binary files a/lab/lab-06/Lab06_6538075121.jar and b/lab/lab-06/Lab06_6538075121.jar differ
diff --git a/lab/lab-07/Lab07_6538075121.jar b/lab/lab-07/Lab07_6538075121.jar
index 8450938..bdbccc3 100644
Binary files a/lab/lab-07/Lab07_6538075121.jar and b/lab/lab-07/Lab07_6538075121.jar differ
diff --git a/lab/lab-08/6538075121_Lab08_BST.jar b/lab/lab-08/6538075121_Lab08_BST.jar
index ade774e..dc3aee5 100644
Binary files a/lab/lab-08/6538075121_Lab08_BST.jar and b/lab/lab-08/6538075121_Lab08_BST.jar differ
diff --git a/lab/lab-09/6538075121_Lab09_BSTRecursive.jar b/lab/lab-09/6538075121_Lab09_BSTRecursive.jar
new file mode 100644
index 0000000..e598ea9
Binary files /dev/null and b/lab/lab-09/6538075121_Lab09_BSTRecursive.jar differ
diff --git a/lab/lab-09/Archive_64114912ab120/~$b09 BST Recursive.docx b/lab/lab-09/Archive_64114912ab120/~$b09 BST Recursive.docx
deleted file mode 100644
index 3864952..0000000
Binary files a/lab/lab-09/Archive_64114912ab120/~$b09 BST Recursive.docx and /dev/null differ
diff --git a/lab/lab-09/lab-09/src/BSTRecursive.java b/lab/lab-09/lab-09/src/BSTRecursive.java
index 26d63e7..d1b2637 100644
--- a/lab/lab-09/lab-09/src/BSTRecursive.java
+++ b/lab/lab-09/lab-09/src/BSTRecursive.java
@@ -217,40 +217,16 @@ public void insertInOrder(BSTNode n) {
* the value v
* @return next data of v in the given subtree
*/
- public int nextOf0(BSTNode n,int v) {
- // base case
- if (n == null) {
- return v;
- }
- // recursive case
- else {
- if (v < n.data) { // if v is on left of n
- if (nextOf(n.left, v) != v) { // if the left has potential answer
- return Math.min(nextOf(n.left, v), n.data); // compare to n
- } else { // if the left has no potential answer
- return n.data; // then n is the answer
- }
- } else { // if v is on the right of n
- return Math.max(nextOf(n.left, v), nextOf(n.right, v));
- }
- }
- }
+
public int nextOf(BSTNode n,int v) {
- BSTNode successor = null;
-
- while (n != null) {
- if (v < n.data) {
- // If v is less than n.data, n could be a successor. Keep searching in the left subtree
- successor = n; // Update successor to current node as it's a potential successor
- n = n.left; // Move to left child
- } else {
- // If v is greater than or equal to n.data, successor must be in the right subtree
- n = n.right; // Move to right child
- }
- }
-
- // If a successor is found, return its data; otherwise, return null to indicate not found
- return successor != null ? successor.data : null;
+ if (n == null) return v;
+ if (n.data > v) {
+ int left = nextOf(n.left, v);
+ if (left == v) return n.data;
+ else return left;
+ } else {
+ return nextOf(n.right, v);
+ }
}
}
diff --git a/lab/lab-10/.DS_Store b/lab/lab-10/.DS_Store
new file mode 100644
index 0000000..dc68c8a
Binary files /dev/null and b/lab/lab-10/.DS_Store differ
diff --git a/lab/lab-10/Book1.xlsx b/lab/lab-10/Book1.xlsx
new file mode 100644
index 0000000..e40a496
Binary files /dev/null and b/lab/lab-10/Book1.xlsx differ
diff --git a/lab/lab-10/Lab 10 Hashing_6603c22115a04/HashTable.java b/lab/lab-10/Lab 10 Hashing_6603c22115a04/HashTable.java
new file mode 100644
index 0000000..5152011
--- /dev/null
+++ b/lab/lab-10/Lab 10 Hashing_6603c22115a04/HashTable.java
@@ -0,0 +1,60 @@
+import java.util.Arrays;
+
+class OpenAddressHashing {
+ private final int[] table;
+ private final int size;
+ private final int EMPTY = -1;
+
+ public OpenAddressHashing(int size) {
+ this.size = size;
+ table = new int[size];
+ Arrays.fill(table, EMPTY);
+ }
+
+ // Hash function using linear probing
+ private int linearProbe(int key, int i) {
+ return (key + i) % size;
+ }
+
+ // Hash function using quadratic probing
+ private int quadraticProbe(int key, int i) {
+ return (key + i * i) % size;
+ }
+
+ // Insertion operation with linear probing
+ public void insertLinear(int key) {
+ int index = key % size;
+ int i = 1;
+ while (table[index] != EMPTY) {
+ index = linearProbe(key, i);
+ i++;
+ }
+ table[index] = key;
+ }
+
+ // Insertion operation with quadratic probing
+ public void insertQuadratic(int key) {
+ int index = key % size;
+ int i = 1;
+ while (table[index] != EMPTY) {
+ index = quadraticProbe(key, i);
+ i++;
+ }
+ table[index] = key;
+ }
+
+
+ public static void main(String[] args) {
+ int tableSize = 10000;
+ for(int n = 100 ;n <= 9900;n+=100) {
+ OpenAddressHashing linearHashing = new OpenAddressHashing(tableSize);
+ OpenAddressHashing quadraticHashing = new OpenAddressHashing(tableSize);
+ int[] data = new int[n];
+ for(int i=0;i
+
+
+
+
+
+
+
+
+
diff --git a/lab/lab-10/lab-10/.gitignore b/lab/lab-10/lab-10/.gitignore
new file mode 100644
index 0000000..ae3c172
--- /dev/null
+++ b/lab/lab-10/lab-10/.gitignore
@@ -0,0 +1 @@
+/bin/
diff --git a/lab/lab-10/lab-10/.project b/lab/lab-10/lab-10/.project
new file mode 100644
index 0000000..59180e5
--- /dev/null
+++ b/lab/lab-10/lab-10/.project
@@ -0,0 +1,17 @@
+
+
+ lab-10
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/lab/lab-10/lab-10/.settings/org.eclipse.core.resources.prefs b/lab/lab-10/lab-10/.settings/org.eclipse.core.resources.prefs
new file mode 100644
index 0000000..99f26c0
--- /dev/null
+++ b/lab/lab-10/lab-10/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,2 @@
+eclipse.preferences.version=1
+encoding/=UTF-8
diff --git a/lab/lab-10/lab-10/.settings/org.eclipse.jdt.core.prefs b/lab/lab-10/lab-10/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..ccfd559
--- /dev/null
+++ b/lab/lab-10/lab-10/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,14 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=20
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=20
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
+org.eclipse.jdt.core.compiler.release=enabled
+org.eclipse.jdt.core.compiler.source=20
diff --git a/lab/lab-10/lab-10/src/HashTable.java b/lab/lab-10/lab-10/src/HashTable.java
new file mode 100644
index 0000000..2e10643
--- /dev/null
+++ b/lab/lab-10/lab-10/src/HashTable.java
@@ -0,0 +1,70 @@
+import java.util.Arrays;
+
+class OpenAddressHashing {
+ private final int[] table;
+ private final int size;
+ private final int EMPTY = -1;
+
+ public OpenAddressHashing(int size) {
+ this.size = size;
+ table = new int[size];
+ Arrays.fill(table, EMPTY);
+ }
+
+ // Hash function using linear probing
+ private int linearProbe(int key, int i) {
+ return (key + i) % size;
+ }
+
+ // Hash function using quadratic probing
+ private int quadraticProbe(int key, int i) {
+ return (key + i * i) % size;
+ }
+
+ // Insertion operation with linear probing
+ public int insertLinear(int key) {
+ int index = key % size;
+ int i = 1;
+ while (table[index] != EMPTY) {
+ index = linearProbe(key, i);
+ i++;
+ }
+ table[index] = key;
+ return i;
+ }
+
+ // Insertion operation with quadratic probing
+ public int insertQuadratic(int key) {
+ int index = key % size;
+ int i = 1;
+ while (table[index] != EMPTY) {
+ index = quadraticProbe(key, i);
+ i++;
+ }
+ table[index] = key;
+ return i;
+ }
+
+
+ public static void main(String[] args) {
+ int tableSize = 10000;
+ for(int n = 100 ;n <= 9900;n+=100) {
+ OpenAddressHashing linearHashing = new OpenAddressHashing(tableSize);
+ OpenAddressHashing quadraticHashing = new OpenAddressHashing(tableSize);
+ int[] data = new int[n];
+ int li = 0;
+ int qu = 0;
+ for(int i=0;i 0; i--) swap(d, i, rnd.nextInt(i));
+ break;
+ case 2: for (int i = 0; i < n; i++) d[i] = n-i;
+ }
+ long sum = 0;
+ long min = 0;
+ long time = 0;
+ for (int k = 0; k < repeat; k++) {
+ int[] t = (int[])d.clone();
+ long start = System.currentTimeMillis();
+//-------------------------------------------------
+ bubbleSort(t);
+ // selectionSort(t);
+ // insertionSort(t);
+ // shellSort(t);
+ // heapSort(t);
+ // mergeSort(t);
+ // quickSort(t);
+//-------------------------------------------------
+ time = System.currentTimeMillis()- start;
+ sum += time;
+ if (min == 0 || time < min) min = time;
+ checkSorted(t);
+ }
+ //System.out.printf("%d \t %f \n", n, (double) sum / repeat);
+ System.out.printf("%d \t %d \n", n, min);
+ }
+ }
+ System.out.println("Finished");
+ }
+
+ //-----------------------------------------------------------------
+ static void shellSort(int[] d) {
+ int h;
+ for (h = 1; h <= d.length / 4; h = 2 * h + 1);
+ for (; h > 0; h /= 2) {
+ for (int m = 0; m < h; m++) {
+ for (int i = m + h; i < d.length; i += h) {
+ int t = d[i];
+ int j = i - h;
+ while (j >= 0 && t < d[j]) {
+ d[j + h] = d[j];
+ j -= h;
+ }
+ d[j + h] = t;
+ }
+ }
+ }
+ }
+ //-----------------------------------------------------------------
+ public static void quickSort(int[] d) {
+ quickSortR(d, 0, d.length - 1);
+ }
+ private static void quickSortR(int[] d, int left, int right) {
+ if (left < right) {
+ int j = partition(d, left, right);
+ quickSortR(d, left, j - 1);
+ quickSortR(d, j + 1, right);
+ }
+ }
+ private static int partition(int[] d, int left, int right) {
+ int center = (left + right) / 2;
+ if ((d[left] < d[center])) swap(d, left, center);
+ if ((d[right] < d[center])) swap(d, center, right);
+ if ((d[right] < d[left])) swap(d, left, right);
+// int r = left + (int)(Math.random() * (right - left + 1));
+// swap(d, left, r);
+ int p = d[left];
+ int i = left, j = right + 1;
+ while (i < j) {
+ while (p < d[--j]);
+ while (d[++i] < p) {
+ if (i == right) break;
+ }
+ if (i < j) swap(d, i, j);
+ }
+ swap(d, left, j);
+ return j;
+ }
+ //-----------------------------------------------------------------
+ public static void mergeSort(int[] d) {
+ mergeSortR(d, 0, d.length - 1, d.clone());
+ }
+ private static
+ void mergeSortR(int[] d, int left, int right, int[] t) {
+ if (left < right) {
+ int m = left + (right - left) / 2;
+ mergeSortR(t, left, m, d);
+ mergeSortR(t, m + 1, right, d);
+ merge(t, left, m, right, d);
+ }
+ }
+ private static void merge(int[] d, int left, int mid,
+ int right, int[] t) {
+ int i = left, j = mid + 1;
+ int k = left;
+ while (i <= mid && j <= right) {
+ t[k++] = d[i] < d[j] ? d[i++] : d[j++];
+ }
+ while (i <= mid) {
+ t[k++] = d[i++];
+ }
+ while (j <= right) {
+ t[k++] = d[j++];
+ }
+ }
+ //-----------------------------------------------------------------
+ public static void heapSort(int[] d) {
+ int size = d.length;
+ for (int k = size / 2 - 1; k >= 0; k--) fixDown(d, size, k);
+ for (int k = size-1; k > 0; k--) {
+ swap(d, 0, k);
+ fixDown(d, --size, 0);
+ }
+ }
+ private static void fixDown(int[] d, int size, int p) {
+ int c;
+ while ((c = 2 * p + 1) < size) {
+ if (c < size-1 && (d[c] < d[c + 1])) c++;
+ if (d[c] <= d[p]) break;
+ swap(d, c, p);
+ p = c;
+ }
+ }
+ //--------------------------------------------------------
+ public static void insertionSort(int[] d) {
+ for (int k = 1; k < d.length; k++) {
+ int t = d[k];
+ int j = k - 1;
+ while (j >= 0) {
+ if (d[j] <= t) break;
+ d[j + 1] = d[j];
+ j--;
+ }
+ d[j + 1] = t;
+ }
+ }
+ //-----------------------------------------------------------------
+ public static void selectionSort(int[] d) {
+ for (int k = d.length - 1; k > 0; k--) {
+ int maxI = 0;
+ for (int i = 1; i <= k; i++) {
+ if (d[maxI] < d[i]) maxI = i;
+ }
+ swap(d, maxI, k);
+ }
+ }
+ //-----------------------------------------------------------------
+ public static void bubbleSort(int[] d) {
+ for (int i = d.length - 1; i > 0; i--) {
+ for (int j = 0; j < i; j++) {
+ if (d[j + 1] < d[j]) swap(d, j, j + 1);
+ }
+ }
+ }
+ //-----------------------------------------------------------------
+ private static void checkSorted(int[] d) {
+ for (int i = 1; i < d.length; i++) {
+ if (d[i - 1] > d[i]) throw new RuntimeException("WRONG");
+ }
+ }
+ private static void swap(int[] d, int i, int j) {
+ int t = d[i]; d[i] = d[j]; d[j] = t;
+ }
+}
diff --git a/lab/lab-11/Lab11 Sorting_624fb310dfe41/exampleGraphs.xlsx b/lab/lab-11/Lab11 Sorting_624fb310dfe41/exampleGraphs.xlsx
new file mode 100644
index 0000000..76c3657
Binary files /dev/null and b/lab/lab-11/Lab11 Sorting_624fb310dfe41/exampleGraphs.xlsx differ
diff --git a/lab/lab-11/Lab11 Sorting_624fb310dfe41/instructions.docx b/lab/lab-11/Lab11 Sorting_624fb310dfe41/instructions.docx
new file mode 100644
index 0000000..a64fb3a
Binary files /dev/null and b/lab/lab-11/Lab11 Sorting_624fb310dfe41/instructions.docx differ
diff --git a/lab/lab-11/lab-11/.DS_Store b/lab/lab-11/lab-11/.DS_Store
new file mode 100644
index 0000000..0e67fd6
Binary files /dev/null and b/lab/lab-11/lab-11/.DS_Store differ
diff --git a/lab/lab-11/lab-11/.classpath b/lab/lab-11/lab-11/.classpath
new file mode 100644
index 0000000..7b57c0d
--- /dev/null
+++ b/lab/lab-11/lab-11/.classpath
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/lab/lab-11/lab-11/.gitignore b/lab/lab-11/lab-11/.gitignore
new file mode 100644
index 0000000..ae3c172
--- /dev/null
+++ b/lab/lab-11/lab-11/.gitignore
@@ -0,0 +1 @@
+/bin/
diff --git a/lab/lab-11/lab-11/.project b/lab/lab-11/lab-11/.project
new file mode 100644
index 0000000..e08e758
--- /dev/null
+++ b/lab/lab-11/lab-11/.project
@@ -0,0 +1,17 @@
+
+
+ lab-11
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/lab/lab-11/lab-11/.settings/org.eclipse.core.resources.prefs b/lab/lab-11/lab-11/.settings/org.eclipse.core.resources.prefs
new file mode 100644
index 0000000..99f26c0
--- /dev/null
+++ b/lab/lab-11/lab-11/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,2 @@
+eclipse.preferences.version=1
+encoding/=UTF-8
diff --git a/lab/lab-11/lab-11/.settings/org.eclipse.jdt.core.prefs b/lab/lab-11/lab-11/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..ccfd559
--- /dev/null
+++ b/lab/lab-11/lab-11/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,14 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=20
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=20
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
+org.eclipse.jdt.core.compiler.release=enabled
+org.eclipse.jdt.core.compiler.source=20
diff --git a/lab/lab-11/lab-11/src/Sorting.java b/lab/lab-11/lab-11/src/Sorting.java
new file mode 100644
index 0000000..4dd76f7
--- /dev/null
+++ b/lab/lab-11/lab-11/src/Sorting.java
@@ -0,0 +1,187 @@
+import java.util.Random;
+
+//------------------------------------------
+// Time Complexity of Sorting Algorithms
+// by SPJ (14/10/2014)
+//------------------------------------------
+public class Sorting {
+
+ public static void main(String[] args) throws Exception {
+ int repeat = 2; // repeat each experiment a couple of times and keep the best result (lowest time)
+
+ for (int initialOrder = 0; initialOrder <= 2; initialOrder++) { // 0 = sorted, 1 = random, 2 = reversed
+ System.out.printf("\nInitial state = %d\n", initialOrder);
+ System.out.printf("size \t time(ms) \n");
+ for (int n = 10000; n <= 160000; n *= 2) { // bubble, selection, insertion sort
+ //for (int n = 1000000; n <= 64000000; n *= 2) { // shell, heap, merge, quick sort
+ int[] d = new int[n];
+ switch (initialOrder) {
+ case 0: for (int i = 0; i < n; i++) d[i] = i; break;
+ case 1:
+ for (int i = 0; i < n; i++) d[i] = i;
+ Random rnd = new Random(1234567);
+ for (int i = d.length - 1; i > 0; i--) swap(d, i, rnd.nextInt(i));
+ break;
+ case 2: for (int i = 0; i < n; i++) d[i] = n-i;
+ }
+ long sum = 0;
+ long min = 0;
+ long time = 0;
+ for (int k = 0; k < repeat; k++) {
+ int[] t = (int[])d.clone();
+ long start = System.currentTimeMillis();
+//-------------------------------------------------
+ // bubbleSort(t);
+ selectionSort(t);
+ //insertionSort(t);
+ // shellSort(t);
+ // heapSort(t);
+ // mergeSort(t);
+ // quickSort(t);
+//-------------------------------------------------
+ time = System.currentTimeMillis()- start;
+ sum += time;
+ if (min == 0 || time < min) min = time;
+ checkSorted(t);
+ }
+ //System.out.printf("%d \t %f \n", n, (double) sum / repeat);
+ System.out.printf("%d \t %d \n", n, min);
+ }
+ }
+ System.out.println("Finished");
+ }
+
+ //-----------------------------------------------------------------
+ static void shellSort(int[] d) {
+ int h;
+ for (h = 1; h <= d.length / 4; h = 2 * h + 1);
+ for (; h > 0; h /= 2) {
+ for (int m = 0; m < h; m++) {
+ for (int i = m + h; i < d.length; i += h) {
+ int t = d[i];
+ int j = i - h;
+ while (j >= 0 && t < d[j]) {
+ d[j + h] = d[j];
+ j -= h;
+ }
+ d[j + h] = t;
+ }
+ }
+ }
+ }
+ //-----------------------------------------------------------------
+ public static void quickSort(int[] d) {
+ quickSortR(d, 0, d.length - 1);
+ }
+ private static void quickSortR(int[] d, int left, int right) {
+ if (left < right) {
+ int j = partition(d, left, right);
+ quickSortR(d, left, j - 1);
+ quickSortR(d, j + 1, right);
+ }
+ }
+ private static int partition(int[] d, int left, int right) {
+ int center = (left + right) / 2;
+ if ((d[left] < d[center])) swap(d, left, center);
+ if ((d[right] < d[center])) swap(d, center, right);
+ if ((d[right] < d[left])) swap(d, left, right);
+// int r = left + (int)(Math.random() * (right - left + 1));
+// swap(d, left, r);
+ int p = d[left];
+ int i = left, j = right + 1;
+ while (i < j) {
+ while (p < d[--j]);
+ while (d[++i] < p) {
+ if (i == right) break;
+ }
+ if (i < j) swap(d, i, j);
+ }
+ swap(d, left, j);
+ return j;
+ }
+ //-----------------------------------------------------------------
+ public static void mergeSort(int[] d) {
+ mergeSortR(d, 0, d.length - 1, d.clone());
+ }
+ private static
+ void mergeSortR(int[] d, int left, int right, int[] t) {
+ if (left < right) {
+ int m = left + (right - left) / 2;
+ mergeSortR(t, left, m, d);
+ mergeSortR(t, m + 1, right, d);
+ merge(t, left, m, right, d);
+ }
+ }
+ private static void merge(int[] d, int left, int mid,
+ int right, int[] t) {
+ int i = left, j = mid + 1;
+ int k = left;
+ while (i <= mid && j <= right) {
+ t[k++] = d[i] < d[j] ? d[i++] : d[j++];
+ }
+ while (i <= mid) {
+ t[k++] = d[i++];
+ }
+ while (j <= right) {
+ t[k++] = d[j++];
+ }
+ }
+ //-----------------------------------------------------------------
+ public static void heapSort(int[] d) {
+ int size = d.length;
+ for (int k = size / 2 - 1; k >= 0; k--) fixDown(d, size, k);
+ for (int k = size-1; k > 0; k--) {
+ swap(d, 0, k);
+ fixDown(d, --size, 0);
+ }
+ }
+ private static void fixDown(int[] d, int size, int p) {
+ int c;
+ while ((c = 2 * p + 1) < size) {
+ if (c < size-1 && (d[c] < d[c + 1])) c++;
+ if (d[c] <= d[p]) break;
+ swap(d, c, p);
+ p = c;
+ }
+ }
+ //--------------------------------------------------------
+ public static void insertionSort(int[] d) {
+ for (int k = 1; k < d.length; k++) {
+ int t = d[k];
+ int j = k - 1;
+ while (j >= 0) {
+ if (d[j] <= t) break;
+ d[j + 1] = d[j];
+ j--;
+ }
+ d[j + 1] = t;
+ }
+ }
+ //-----------------------------------------------------------------
+ public static void selectionSort(int[] d) {
+ for (int k = d.length - 1; k > 0; k--) {
+ int maxI = 0;
+ for (int i = 1; i <= k; i++) {
+ if (d[maxI] < d[i]) maxI = i;
+ }
+ swap(d, maxI, k);
+ }
+ }
+ //-----------------------------------------------------------------
+ public static void bubbleSort(int[] d) {
+ for (int i = d.length - 1; i > 0; i--) {
+ for (int j = 0; j < i; j++) {
+ if (d[j + 1] < d[j]) swap(d, j, j + 1);
+ }
+ }
+ }
+ //-----------------------------------------------------------------
+ private static void checkSorted(int[] d) {
+ for (int i = 1; i < d.length; i++) {
+ if (d[i - 1] > d[i]) throw new RuntimeException("WRONG");
+ }
+ }
+ private static void swap(int[] d, int i, int j) {
+ int t = d[i]; d[i] = d[j]; d[j] = t;
+ }
+}
diff --git a/lab/lab-12/.DS_Store b/lab/lab-12/.DS_Store
new file mode 100644
index 0000000..c475b3c
Binary files /dev/null and b/lab/lab-12/.DS_Store differ
diff --git a/lab/lab-12/6538075121_Lab12_Heap.jar b/lab/lab-12/6538075121_Lab12_Heap.jar
new file mode 100644
index 0000000..4f4ee81
Binary files /dev/null and b/lab/lab-12/6538075121_Lab12_Heap.jar differ
diff --git a/lab/lab-12/Lab12Heap_6262469839dfa/Heap12.pdf b/lab/lab-12/Lab12Heap_6262469839dfa/Heap12.pdf
new file mode 100644
index 0000000..245b1f5
Binary files /dev/null and b/lab/lab-12/Lab12Heap_6262469839dfa/Heap12.pdf differ
diff --git a/lab/lab-12/Lab12Heap_6262469839dfa/Lab12Student.jar b/lab/lab-12/Lab12Heap_6262469839dfa/Lab12Student.jar
new file mode 100644
index 0000000..3b68933
Binary files /dev/null and b/lab/lab-12/Lab12Heap_6262469839dfa/Lab12Student.jar differ
diff --git a/lab/lab-12/lab-12/.DS_Store b/lab/lab-12/lab-12/.DS_Store
new file mode 100644
index 0000000..9a874b5
Binary files /dev/null and b/lab/lab-12/lab-12/.DS_Store differ
diff --git a/lab/lab-12/lab-12/.classpath b/lab/lab-12/lab-12/.classpath
new file mode 100644
index 0000000..092bd63
--- /dev/null
+++ b/lab/lab-12/lab-12/.classpath
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/lab/lab-12/lab-12/.gitignore b/lab/lab-12/lab-12/.gitignore
new file mode 100644
index 0000000..ae3c172
--- /dev/null
+++ b/lab/lab-12/lab-12/.gitignore
@@ -0,0 +1 @@
+/bin/
diff --git a/lab/lab-12/lab-12/.project b/lab/lab-12/lab-12/.project
new file mode 100644
index 0000000..0afb2d6
--- /dev/null
+++ b/lab/lab-12/lab-12/.project
@@ -0,0 +1,17 @@
+
+
+ lab-12
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/lab/lab-12/lab-12/.settings/org.eclipse.core.resources.prefs b/lab/lab-12/lab-12/.settings/org.eclipse.core.resources.prefs
new file mode 100644
index 0000000..99f26c0
--- /dev/null
+++ b/lab/lab-12/lab-12/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,2 @@
+eclipse.preferences.version=1
+encoding/=UTF-8
diff --git a/lab/lab-12/lab-12/.settings/org.eclipse.jdt.core.prefs b/lab/lab-12/lab-12/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..ccfd559
--- /dev/null
+++ b/lab/lab-12/lab-12/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,14 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=20
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=20
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
+org.eclipse.jdt.core.compiler.release=enabled
+org.eclipse.jdt.core.compiler.source=20
diff --git a/lab/lab-12/lab-12/src/Heap.java b/lab/lab-12/lab-12/src/Heap.java
new file mode 100644
index 0000000..55c14c8
--- /dev/null
+++ b/lab/lab-12/lab-12/src/Heap.java
@@ -0,0 +1,79 @@
+public class Heap implements PriorityQ {
+ Object[] mData;
+ int size = 0;
+
+ public Heap() {
+ final int DEFAULT_INITIAL_CAPACITY = 11;
+ mData = new Comparable[DEFAULT_INITIAL_CAPACITY];
+ } // default constructor
+
+ public boolean isEmpty() {
+ return size == 0;
+ }
+
+ public void add(Object element) {
+
+ if (++size == mData.length) {
+ Object[] newHeap = new Object[2 * mData.length];
+ System.arraycopy(mData, 0, newHeap, 0, size);
+ mData = newHeap;
+ }
+ mData[size - 1] = element;
+ percolateUp();
+ }
+
+ protected void percolateUp() {
+ int parent;
+ int child = size - 1;
+ Comparable temp;
+ while (child > 0) {
+ parent = (child - 1) / 2;
+ if (((Comparable) mData[parent]).compareTo(mData[child]) <= 0)
+ break;
+ temp = (Comparable) mData[parent];
+ mData[parent] = mData[child];
+ mData[child] = temp;
+ child = parent;
+ }
+ }
+
+ public Object top() throws Exception {
+ if (size == 0)
+ throw new Exception("Empty");
+ return mData[0];
+ }
+
+ public Object pop() throws Exception {
+ if (size == 0)
+ throw new Exception("Priority queue empty.");
+ Object 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;
+ Object temp;
+ while (child < size) {
+ if (child < size - 1
+ && ((Comparable) mData[child]).compareTo(mData[child + 1]) > 0)
+ child++;
+ if (((Comparable) mData[parent]).compareTo(mData[child]) <= 0)
+ 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;
+ }
+
+}
diff --git a/lab/lab-12/lab-12/src/HeapMain.java b/lab/lab-12/lab-12/src/HeapMain.java
new file mode 100644
index 0000000..be938e9
--- /dev/null
+++ b/lab/lab-12/lab-12/src/HeapMain.java
@@ -0,0 +1,34 @@
+import java.util.Random;
+
+
+public class HeapMain {
+ public static void main(String args[]) throws Exception {
+ int[] reps = {100, 10000, 1000000};
+ long time;
+ long sum;
+
+ for (int rep: reps) {
+ Heap h = new Heap();
+ sum = 0;
+ Random r = new Random();
+ for (int i = 0; i < rep; i++) {
+ int x = Math.abs(r.nextInt());
+ long start = System.currentTimeMillis();
+ h.add(x);
+ time = System.currentTimeMillis()- start;
+ sum += time;
+ }
+ System.out.println(sum);
+ sum = 0;
+ for (int i = 0; i < rep; i++) {
+ long start = System.currentTimeMillis();
+ h.pop();
+ time = System.currentTimeMillis()- start;
+ sum += time;
+ }
+ System.out.println(sum);
+ }
+
+
+ }
+}
diff --git a/lab/lab-12/lab-12/src/MaxIntHeap.java b/lab/lab-12/lab-12/src/MaxIntHeap.java
new file mode 100644
index 0000000..7754e07
--- /dev/null
+++ b/lab/lab-12/lab-12/src/MaxIntHeap.java
@@ -0,0 +1,60 @@
+
+public class MaxIntHeap extends Heap{
+ public void add(Object element) {
+ // Add your code here
+
+ // Add new element as the last node
+ if (++size == mData.length) {
+ Object[] newHeap = new Object[2 * mData.length];
+ System.arraycopy(mData, 0, newHeap, 0, size);
+ mData = newHeap;
+ }
+ mData[size - 1] = element;
+
+ // Percolate up
+ int parent;
+ int child = size - 1;
+ Comparable temp;
+ while (child > 0) {
+ parent = (child - 1) / 2;
+ if (((Comparable) mData[parent]).compareTo(mData[child]) >= 0)
+ break;
+ temp = (Comparable) mData[parent];
+ mData[parent] = mData[child];
+ mData[child] = temp;
+ child = parent;
+ }
+
+ }
+ public Object pop() throws Exception {
+ // Add your code here
+
+ // Remove first node (max) and replace with last node
+ if (size == 0)
+ throw new Exception("Priority queue empty.");
+ Object maxElem = mData[0];
+ mData[0] = mData[size - 1];
+ size--;
+
+ // Percolate down
+ int parent = 0;
+ int child = 2 * parent + 1;
+ Object temp;
+ while (child < size) {
+ if (child < size - 1
+ && ((Comparable) mData[child]).compareTo(mData[child + 1]) < 0)
+ child++;
+ if (((Comparable) mData[parent]).compareTo(mData[child]) >= 0)
+ break;
+ temp = mData[child];
+ mData[child] = mData[parent];
+ mData[parent] = temp;
+ parent = child;
+ child = 2 * parent + 1;
+ }
+
+ // Return removed node
+ return maxElem;
+
+ }
+}
diff --git a/lab/lab-12/lab-12/src/PriorityQ.java b/lab/lab-12/lab-12/src/PriorityQ.java
new file mode 100644
index 0000000..6761b06
--- /dev/null
+++ b/lab/lab-12/lab-12/src/PriorityQ.java
@@ -0,0 +1,21 @@
+public interface PriorityQ {
+ // Postcondition: return the number of
+ // items in this priority queue.
+ int size();
+
+ // Postcondition: return true if this priority
+ // queue does not store any item, otherwise return false.
+ boolean isEmpty();
+
+ // Postcondition: element is
+ // added to priority queue.
+ void add(Object element) throws Exception;
+
+ // Throws NoSuchElementException if heap is empty.
+ // Postcondition: return the most important object.
+ public Object top() throws Exception;
+
+ // Throws NoSuchElementException if heap is empty.
+ // Postcondition: remove and return the most important object
+ public Object pop() throws Exception;
+}
diff --git a/lab/lab-12/lab-12/src/Screen Shot 2567-04-18 at 14.33.57.png b/lab/lab-12/lab-12/src/Screen Shot 2567-04-18 at 14.33.57.png
new file mode 100644
index 0000000..67ebdf2
Binary files /dev/null and b/lab/lab-12/lab-12/src/Screen Shot 2567-04-18 at 14.33.57.png differ
diff --git a/lab/lab-12/lab-12/src/TestMaxHeap.java b/lab/lab-12/lab-12/src/TestMaxHeap.java
new file mode 100644
index 0000000..e9a3d4a
--- /dev/null
+++ b/lab/lab-12/lab-12/src/TestMaxHeap.java
@@ -0,0 +1,18 @@
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class TestMaxHeap {
+
+ @Test
+ public void test() throws Exception {
+ int[] x = {20,40,10,5,100,79,26,30};
+ Heap h = new MaxIntHeap();
+ for(int i=0;i
+
+
+
+
+
+
+
+
+
+
diff --git a/lab/lab-13/lab-13/.gitignore b/lab/lab-13/lab-13/.gitignore
new file mode 100644
index 0000000..ae3c172
--- /dev/null
+++ b/lab/lab-13/lab-13/.gitignore
@@ -0,0 +1 @@
+/bin/
diff --git a/lab/lab-13/lab-13/.project b/lab/lab-13/lab-13/.project
new file mode 100644
index 0000000..4439281
--- /dev/null
+++ b/lab/lab-13/lab-13/.project
@@ -0,0 +1,17 @@
+
+
+ lab-13
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/lab/lab-13/lab-13/.settings/org.eclipse.core.resources.prefs b/lab/lab-13/lab-13/.settings/org.eclipse.core.resources.prefs
new file mode 100644
index 0000000..99f26c0
--- /dev/null
+++ b/lab/lab-13/lab-13/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,2 @@
+eclipse.preferences.version=1
+encoding/=UTF-8
diff --git a/lab/lab-13/lab-13/.settings/org.eclipse.jdt.core.prefs b/lab/lab-13/lab-13/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..ccfd559
--- /dev/null
+++ b/lab/lab-13/lab-13/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,14 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=20
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=20
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
+org.eclipse.jdt.core.compiler.release=enabled
+org.eclipse.jdt.core.compiler.source=20
diff --git a/lab/lab-13/lab-13/src/.DS_Store b/lab/lab-13/lab-13/src/.DS_Store
new file mode 100644
index 0000000..5008ddf
Binary files /dev/null and b/lab/lab-13/lab-13/src/.DS_Store differ
diff --git a/lab/lab-13/lab-13/src/.classpath b/lab/lab-13/lab-13/src/.classpath
new file mode 100644
index 0000000..e71bc81
--- /dev/null
+++ b/lab/lab-13/lab-13/src/.classpath
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
diff --git a/lab/lab-13/lab-13/src/.project b/lab/lab-13/lab-13/src/.project
new file mode 100644
index 0000000..cab59e3
--- /dev/null
+++ b/lab/lab-13/lab-13/src/.project
@@ -0,0 +1,17 @@
+
+
+ book_lab08_2017-2018_AVL_Student
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/lab/lab-13/lab-13/src/AVLNode.class b/lab/lab-13/lab-13/src/AVLNode.class
new file mode 100644
index 0000000..186d91a
Binary files /dev/null and b/lab/lab-13/lab-13/src/AVLNode.class differ
diff --git a/lab/lab-13/lab-13/src/AVLNode.java b/lab/lab-13/lab-13/src/AVLNode.java
new file mode 100644
index 0000000..b953c60
--- /dev/null
+++ b/lab/lab-13/lab-13/src/AVLNode.java
@@ -0,0 +1,47 @@
+public class AVLNode {
+ // cannot extend from BSTNode because left, right, parent will then be
+ // BSTNode!!
+ int data;
+ AVLNode left, right, parent;
+ int height;
+
+ public AVLNode(int data) {
+ this.data = data;
+ left = null;
+ right = null;
+ parent = null;
+ height = 0;
+ }
+
+ public AVLNode(int data, AVLNode left, AVLNode right, AVLNode parent, int height) {
+ this.data = data;
+ this.left = left;
+ this.right = right;
+ this.parent = parent;
+ this.height = height;
+ }
+
+ public static int getHeight(AVLNode n) {
+ // Need this to be static method because null does not have height to be
+ // checked!
+ return (n == null ? -1 : n.height);
+ }
+
+ // Assume that left and right child have correct height,
+ // we update the height value of n
+ public static void updateHeight(AVLNode n) {
+ if (n == null)
+ return;
+ int leftHeight = getHeight(n.left);
+ int rightHeight = getHeight(n.right);
+ n.height = 1 + (leftHeight < rightHeight ? rightHeight : leftHeight);
+ }
+
+ // Assume that left and right subtree have correct height.
+ // find which way the tree is more loaded.
+ public static int tiltDegree(AVLNode n) {
+ if (n == null)
+ return 0;
+ return getHeight(n.left) - getHeight(n.right);
+ }
+}
diff --git a/lab/lab-13/lab-13/src/AVLTree.class b/lab/lab-13/lab-13/src/AVLTree.class
new file mode 100644
index 0000000..e1514a1
Binary files /dev/null and b/lab/lab-13/lab-13/src/AVLTree.class differ
diff --git a/lab/lab-13/lab-13/src/AVLTree.java b/lab/lab-13/lab-13/src/AVLTree.java
new file mode 100644
index 0000000..83c1c74
--- /dev/null
+++ b/lab/lab-13/lab-13/src/AVLTree.java
@@ -0,0 +1,243 @@
+import lab13.src.AVLNode;
+import lab13.src.AVLTree;
+
+public class AVLTree {
+ AVLNode root;
+ int size;
+
+ public AVLTree() {
+ root = null;
+ size = 0;
+ }
+
+ public boolean isEmpty() {
+ return size == 0;
+ }
+
+ public void makeEmpty() {
+ root = null;
+ size = 0;
+ }
+
+ public Iterator findMin() {
+ return findMin(root);
+ }
+
+ public Iterator findMin(AVLNode n) {
+ if (n == null)
+ return null;
+ if (n.left == null) {
+ Iterator itr = new AVLTreeIterator(n);
+ return itr;
+ }
+ return findMin(n.left);
+ }
+
+ public Iterator findMax() {
+ return findMax(root);
+ }
+
+ public Iterator findMax(AVLNode n) {
+ if (n == null)
+ return null;
+ if (n.right == null) {
+ Iterator itr = new AVLTreeIterator(n);
+ return itr;
+ }
+ return findMax(n.right);
+ }
+
+ public Iterator find(int v) {
+ return find(v, root);
+ }
+
+ public Iterator find(int v, AVLNode n) {
+ if (n == null)
+ return null;
+ if (v == n.data)
+ return new AVLTreeIterator(n);
+ if (v < n.data)
+ return find(v, n.left);
+ else
+ return find(v, n.right);
+ }
+
+ public AVLNode insert(int v) {
+ return insert(v, root, null);
+ }
+
+ // return the node n after v was added into the tree
+ public AVLNode insert(int v, AVLNode n, AVLNode parent) {
+ if (n == null) {
+ n = new AVLNode(v, null, null, parent, 0);
+ size++;
+ } else if (v < n.data) {
+ n.left = insert(v, n.left, n);
+ } else if (v > n.data) {
+ n.right = insert(v, n.right, n);
+ }
+ n = rebalance(n);
+ return n;
+ }
+
+ public AVLNode insertNoBalance(int v) {
+ return insertNoBalance(v, root, null);
+ }
+
+ private AVLNode insertNoBalance(int v, AVLNode n, AVLNode parent) {
+ if (n == null) {
+ n = new AVLNode(v, null, null, parent, 0);
+ size++;
+ } else if (v < n.data) {
+ n.left = insertNoBalance(v, n.left, n);
+ } else if (v > n.data) {
+ n.right = insertNoBalance(v, n.right, n);
+ }
+ AVLNode.updateHeight(n);
+ return n;
+ }
+
+ public AVLNode remove(int v) {
+ return remove(v, root, null);
+ }
+
+ // return the node n after v was removed from the tree
+ public AVLNode remove(int v, AVLNode n, AVLNode parent) {
+ if (n == null)
+ ; // do nothing, there is nothing to be removed
+ else if (v < n.data) {
+ n.left = remove(v, n.left, n);
+ } else if (v > n.data) {
+ n.right = remove(v, n.right, n);
+ } else {
+ if (n.left == null && n.right == null) {
+ n = null;
+ size--;
+ } else if (n.left != null && n.right == null) {
+ n.left.parent = parent;
+ n = n.left;
+ size--;
+ } else if (n.right != null && n.left == null) {
+ n.right.parent = parent;
+ n = n.right;
+ size--;
+ } else {
+ AVLTreeIterator i = (AVLTreeIterator) findMin(n.right);
+ int minInRightSubtree = i.currentNode.data;
+ n.data = minInRightSubtree;
+ n.right = remove(minInRightSubtree, n.right, n);
+ }
+ }
+ n = rebalance(n);
+ return n;
+ }
+
+ public AVLNode rebalance(AVLNode n) {
+ if (n == null)
+ return n;
+ int balance = AVLNode.tiltDegree(n);
+ if (balance >= 2) {
+ if (AVLNode.tiltDegree(n.left) <= -1) // 3rd case
+ n.left = rotateRightChild(n.left);
+ n = rotateLeftChild(n); // 1st case
+ } else if (balance <= -2) {
+ if (AVLNode.tiltDegree(n.right) >= 1) // 4th case
+ n.right = rotateLeftChild(n.right);
+ n = rotateRightChild(n); // 2nd case
+ }
+ AVLNode.updateHeight(n);
+ return n;
+ }
+
+ public AVLNode rotateLeftChild(AVLNode n) {
+ AVLNode l = n.left;
+ AVLNode lr = n.left.right; // can be null
+ n.left = lr;
+ if (lr != null) {
+ lr.parent = n;
+ }
+ l.right = n;
+ l.parent = n.parent;
+ n.parent = l;
+
+ AVLNode.updateHeight(n);
+ AVLNode.updateHeight(l);
+ return l;
+ }
+
+ public AVLNode rotateRightChild(AVLNode n) {
+ AVLNode r = n.right;
+ AVLNode rl = n.right.left; // can be null
+ n.right = rl;
+ if (rl != null) {
+ rl.parent = n;
+ }
+ r.left = n;
+ r.parent = n.parent;
+ n.parent = r;
+
+ AVLNode.updateHeight(n);
+ AVLNode.updateHeight(r);
+ return r;
+ }
+
+ public void makeAVL() throws Exception {
+ //code this method
+ makeAVL(root);
+ }
+
+ public void makeAVL(AVLNode n) throws Exception {
+ //code this method
+ if(n == null) return;
+ n = rebalance(n);
+ makeAVL(n.left);
+ makeAVL(n.right);
+ }
+
+ public boolean isAVL() {
+ // code this method
+ return isAVL(root);
+ }
+
+ public boolean isAVL(AVLNode n) {
+ // code this method
+ if(n == null) return true;
+ if(n.tiltDegree(n) < -1 || n.tiltDegree(n) > 1) return false;
+ if(n.right == null && n.left == null) return true;
+ return (isAVL(n.right) && isAVL(n.left));
+ }
+
+
+ public static boolean same(AVLTree t1, AVLTree t2) {
+ //code this method
+ return same(t1.root, t2.root);
+ }
+
+ public static boolean same(AVLNode n1, AVLNode n2) {
+ //code this method
+ if(n1 == null && n2 == null) return true;
+ if(n1 == null && n2 != null) return false;
+ if(n1 != null && n2 == null) return false;
+ return (n1.data == n2.data) && same(n1.left, n2.left) && same(n1.right, n2.right);
+
+ }
+
+
+
+ public static void main(String[] args) throws Exception {
+ // example: print a tree
+
+ AVLTree t = new AVLTree();
+
+ t.root = t.insertNoBalance(33);
+ t.root = t.insertNoBalance(4);
+ t.root = t.insertNoBalance(1);
+ t.root = t.insertNoBalance(66);
+ t.root = t.insertNoBalance(2);
+ t.root = t.insertNoBalance(6);
+
+ BTreePrinter.printNode(t.root);
+
+ }
+
+}
diff --git a/lab/lab-13/lab-13/src/AVLTreeIterator.class b/lab/lab-13/lab-13/src/AVLTreeIterator.class
new file mode 100644
index 0000000..99253e8
Binary files /dev/null and b/lab/lab-13/lab-13/src/AVLTreeIterator.class differ
diff --git a/lab/lab-13/lab-13/src/AVLTreeIterator.java b/lab/lab-13/lab-13/src/AVLTreeIterator.java
new file mode 100644
index 0000000..4f2ee06
--- /dev/null
+++ b/lab/lab-13/lab-13/src/AVLTreeIterator.java
@@ -0,0 +1,113 @@
+
+
+import java.util.NoSuchElementException;
+
+public class AVLTreeIterator implements Iterator {
+
+ AVLNode currentNode;
+
+ public AVLTreeIterator(AVLNode currentNode) {
+ this.currentNode = currentNode;
+ }
+
+ @Override
+ public boolean hasNext() {
+ AVLNode temp = currentNode;
+
+ if (temp.right != null) {
+ return true;
+ }
+
+ AVLNode p = temp.parent;
+ while (p != null && p.right == temp) {
+ temp = p;
+ p = temp.parent;
+ }
+
+ if (p == null)
+ return false;
+ else
+ return true;
+ }
+
+ @Override
+ public boolean hasPrevious() {
+ AVLNode temp = currentNode;
+
+ if (temp.left != null) {
+ return true;
+ }
+
+ AVLNode p = temp.parent;
+ while (p != null && p.left == temp) {
+ temp = p;
+ p = temp.parent;
+ }
+
+ if (p == null)
+ return false;
+ else
+ return true;
+
+ }
+
+ @Override
+ public int next() throws Exception {
+ // Throw exception if the next data
+ // does not exist.
+ AVLNode temp = currentNode;
+
+ if (temp.right != null) {
+ temp = temp.right;
+ while (temp.left != null) {
+ temp = temp.left;
+ }
+ } else {
+ AVLNode p = temp.parent;
+ while (p != null && p.right == temp) {
+ temp = p;
+ p = temp.parent;
+ }
+ temp = p;
+ }
+
+ if (temp == null) // hasNext() == false
+ throw new NoSuchElementException();
+ currentNode = temp;
+ return currentNode.data;
+ }
+
+ @Override
+ public int previous() throws Exception {
+ // Throw exception if the previous data
+ // does not exist.
+ AVLNode temp = currentNode;
+ int d = currentNode.data;
+
+ if (temp.left != null) {
+ temp = temp.left;
+ while (temp.right != null) {
+ temp = temp.right;
+ }
+ } else {
+ AVLNode p = temp.parent;
+ while (p != null && p.left == temp) {
+ temp = p;
+ p = temp.parent;
+ }
+ temp = p;
+ }
+
+ if (temp == null) // hasPrevious() == false
+ throw new NoSuchElementException();
+ currentNode = temp;
+ return d;
+
+ }
+
+ @Override
+ public void set(int value) {
+ currentNode.data = value;
+ }
+
+}
diff --git a/lab/lab-13/lab-13/src/AVLTreeTest.class b/lab/lab-13/lab-13/src/AVLTreeTest.class
new file mode 100644
index 0000000..64b232a
Binary files /dev/null and b/lab/lab-13/lab-13/src/AVLTreeTest.class differ
diff --git a/lab/lab-13/lab-13/src/AVLTreeTest.java b/lab/lab-13/lab-13/src/AVLTreeTest.java
new file mode 100644
index 0000000..854ff91
--- /dev/null
+++ b/lab/lab-13/lab-13/src/AVLTreeTest.java
@@ -0,0 +1,190 @@
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class AVLTreeTest {
+
+ @Test
+ public void testMakeAVL() throws Exception {
+ AVLTree t = new AVLTree();
+ t.root = t.insertNoBalance(100);
+ t.root = t.insertNoBalance(50);
+ t.root = t.insertNoBalance(30);
+ t.root = t.insertNoBalance(40);
+ t.root = t.insertNoBalance(35);
+ t.root = t.insertNoBalance(32);
+
+ t.root = t.insertNoBalance(200);
+ t.root = t.insertNoBalance(150);
+ t.root = t.insertNoBalance(175);
+ t.root = t.insertNoBalance(190);
+ t.root = t.insertNoBalance(160);
+ t.root = t.insertNoBalance(155);
+ t.root = t.insertNoBalance(170);
+ t.root = t.insertNoBalance(172);
+ t.root = t.insertNoBalance(174);
+ t.root = t.insertNoBalance(165);
+ t.root = t.insertNoBalance(163);
+ t.root = t.insertNoBalance(164);
+ assertFalse(t.isAVL());
+ // BTreePrinter.printNode(t.root);
+
+ t.makeAVL();
+ assertTrue(t.isAVL());
+ // BTreePrinter.printNode(t.root);
+
+ }
+
+ @Test
+ public void testIsAVL() {
+ AVLTree t = new AVLTree();
+ // an empty tree is AVL
+ assertTrue(t.isAVL());
+
+ t.root = t.insertNoBalance(7);
+ t.root = t.insertNoBalance(2);
+ assertTrue(t.isAVL());
+
+ t.root = t.insertNoBalance(12);
+ assertTrue(t.isAVL());
+
+ t.root = t.insertNoBalance(20);
+ assertTrue(t.isAVL());
+
+ t.root = t.insertNoBalance(14);
+ assertFalse(t.isAVL());
+
+ t.root = t.insertNoBalance(9);
+ assertFalse(t.isAVL());
+
+ t.root = t.insertNoBalance(30);
+ assertFalse(t.isAVL());
+
+ t.root = t.insertNoBalance(-5);
+ assertTrue(t.isAVL());
+
+ t.root = t.insertNoBalance(-10);
+ assertFalse(t.isAVL());
+
+ t.root = t.insertNoBalance(4);
+ assertTrue(t.isAVL());
+
+ t.root = t.insertNoBalance(-8);
+ assertFalse(t.isAVL());
+
+ t.root = t.insertNoBalance(-1);
+ assertFalse(t.isAVL());
+
+ t.root = t.insertNoBalance(1);
+ assertFalse(t.isAVL());
+
+ t.root = t.insertNoBalance(3);
+ assertTrue(t.isAVL());
+
+ t.root = t.insertNoBalance(0);
+ assertFalse(t.isAVL());
+
+ t.root = t.insertNoBalance(5);
+ assertFalse(t.isAVL());
+
+ t.root = t.insertNoBalance(6);
+ assertFalse(t.isAVL());
+
+ t.root = t.insertNoBalance(16);
+ assertFalse(t.isAVL());
+
+ t.root = t.insertNoBalance(8);
+ assertFalse(t.isAVL());
+
+ t.root = t.insertNoBalance(-3);
+ assertTrue(t.isAVL());
+
+ t.root = t.insertNoBalance(10);
+ assertTrue(t.isAVL());
+
+ t.root = t.insertNoBalance(11);
+ assertTrue(t.isAVL());
+
+ t.root = t.insertNoBalance(18);
+ assertFalse(t.isAVL());
+
+ t.root = t.insertNoBalance(40);
+ assertFalse(t.isAVL());
+
+ t.root = t.insertNoBalance(15);
+ assertFalse(t.isAVL());
+
+ t.root = t.insertNoBalance(13);
+ assertTrue(t.isAVL());
+
+ }
+
+ @Test
+ public void testSame() {
+
+ AVLTree t1 = new AVLTree();
+ AVLTree t2 = new AVLTree();
+ assertTrue(AVLTree.same(t1, t2));
+
+ t1.root = t1.insertNoBalance(5);
+ t1.root = t1.insertNoBalance(2);
+ t1.root = t1.insertNoBalance(1);
+ t1.root = t1.insertNoBalance(3);
+ t1.root = t1.insertNoBalance(4);
+ t1.root = t1.insertNoBalance(7);
+ t1.root = t1.insertNoBalance(6);
+ t2.root = t2.insertNoBalance(5);
+ t2.root = t2.insertNoBalance(7);
+ t2.root = t2.insertNoBalance(6);
+ t2.root = t2.insertNoBalance(2);
+ t2.root = t2.insertNoBalance(3);
+ t2.root = t2.insertNoBalance(4);
+ t2.root = t2.insertNoBalance(1);
+ assertTrue(AVLTree.same(t1, t2));
+
+ t1 = new AVLTree();
+ t2 = new AVLTree();
+ t1.root = t1.insertNoBalance(4);
+ t1.root = t1.insertNoBalance(2);
+ t1.root = t1.insertNoBalance(6);
+ t1.root = t1.insertNoBalance(1);
+ t1.root = t1.insertNoBalance(3);
+ t1.root = t1.insertNoBalance(5);
+ t2.root = t2.insertNoBalance(4);
+ t2.root = t2.insertNoBalance(2);
+ t2.root = t2.insertNoBalance(5);
+ t2.root = t2.insertNoBalance(1);
+ t2.root = t2.insertNoBalance(3);
+ t2.root = t2.insertNoBalance(6);
+ assertFalse(AVLTree.same(t1, t2));
+ t2.root = t2.remove(5);
+ t2.root = t2.insert(5);
+ assertTrue(AVLTree.same(t1, t2));
+
+ t1 = new AVLTree();
+ t2 = new AVLTree();
+ t1.root = t1.insertNoBalance(6);
+ t1.root = t1.insertNoBalance(2);
+ t1.root = t1.insertNoBalance(7);
+ t1.root = t1.insertNoBalance(1);
+ t1.root = t1.insertNoBalance(5);
+ t1.root = t1.insertNoBalance(3);
+
+ t2.root = t2.insertNoBalance(6);
+ t2.root = t2.insertNoBalance(2);
+ t2.root = t2.insertNoBalance(7);
+ t2.root = t2.insertNoBalance(1);
+ t2.root = t2.insertNoBalance(5);
+ t2.root = t2.insertNoBalance(4);
+ assertFalse(AVLTree.same(t1, t2));
+
+ t2.root = t2.remove(4);
+ t2.root = t2.insertNoBalance(3);
+ //BTreePrinter.printNode(t1.root);
+ //BTreePrinter.printNode(t2.root);
+
+ assertTrue(AVLTree.same(t1, t2));
+
+ }
+
+}
diff --git a/lab/lab-13/lab-13/src/BTreePrinter.class b/lab/lab-13/lab-13/src/BTreePrinter.class
new file mode 100644
index 0000000..77077b8
Binary files /dev/null and b/lab/lab-13/lab-13/src/BTreePrinter.class differ
diff --git a/lab/lab-13/lab-13/src/BTreePrinter.java b/lab/lab-13/lab-13/src/BTreePrinter.java
new file mode 100644
index 0000000..d0dea65
--- /dev/null
+++ b/lab/lab-13/lab-13/src/BTreePrinter.java
@@ -0,0 +1,92 @@
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+
+class BTreePrinter {
+
+ public static void printNode(AVLNode root) {
+ int maxLevel = BTreePrinter.maxLevel(root);
+
+ printNodeInternal(Collections.singletonList(root), 1, maxLevel);
+ }
+
+ private static void printNodeInternal(List nodes, int level, int maxLevel) {
+ if (nodes.isEmpty() || BTreePrinter.isAllElementsNull(nodes))
+ return;
+
+ int floor = maxLevel - level;
+ int endgeLines = (int) Math.pow(2, (Math.max(floor - 1, 0)));
+ int firstSpaces = (int) Math.pow(2, (floor)) - 1;
+ int betweenSpaces = (int) Math.pow(2, (floor + 1)) - 1;
+
+ BTreePrinter.printWhitespaces(firstSpaces);
+
+ List newNodes = new ArrayList();
+ for (AVLNode node : nodes) {
+ if (node != null) {
+ System.out.print(node.data);
+ newNodes.add(node.left);
+ newNodes.add(node.right);
+ } else {
+ newNodes.add(null);
+ newNodes.add(null);
+ System.out.print(" ");
+ }
+
+ BTreePrinter.printWhitespaces(betweenSpaces);
+ }
+ System.out.println("");
+
+ for (int i = 1; i <= endgeLines; i++) {
+ for (int j = 0; j < nodes.size(); j++) {
+ BTreePrinter.printWhitespaces(firstSpaces - i);
+ if (nodes.get(j) == null) {
+ BTreePrinter.printWhitespaces(endgeLines + endgeLines + i + 1);
+ continue;
+ }
+
+ if (nodes.get(j).left != null)
+ System.out.print("/");
+ else
+ BTreePrinter.printWhitespaces(1);
+
+ BTreePrinter.printWhitespaces(i + i - 1);
+
+ if (nodes.get(j).right != null)
+ System.out.print("\\");
+ else
+ BTreePrinter.printWhitespaces(1);
+
+ BTreePrinter.printWhitespaces(endgeLines + endgeLines - i);
+ }
+
+ System.out.println("");
+ }
+
+ printNodeInternal(newNodes, level + 1, maxLevel);
+ }
+
+ private static void printWhitespaces(int count) {
+ for (int i = 0; i < count; i++)
+ System.out.print(" ");
+ }
+
+ private static int maxLevel(AVLNode node) {
+ if (node == null)
+ return 0;
+
+ return Math.max(BTreePrinter.maxLevel(node.left), BTreePrinter.maxLevel(node.right)) + 1;
+ }
+
+ private static boolean isAllElementsNull(List list) {
+ for (Object object : list) {
+ if (object != null)
+ return false;
+ }
+
+ return true;
+ }
+
+}
\ No newline at end of file
diff --git a/lab/lab-13/lab-13/src/Iterator.class b/lab/lab-13/lab-13/src/Iterator.class
new file mode 100644
index 0000000..9737245
Binary files /dev/null and b/lab/lab-13/lab-13/src/Iterator.class differ
diff --git a/lab/lab-13/lab-13/src/Iterator.java b/lab/lab-13/lab-13/src/Iterator.java
new file mode 100644
index 0000000..3b2365e
--- /dev/null
+++ b/lab/lab-13/lab-13/src/Iterator.java
@@ -0,0 +1,19 @@
+
+
+public interface Iterator {
+
+ public boolean hasNext();
+
+ public boolean hasPrevious();
+
+ public int next() throws Exception;
+ // move iterator to the next position,
+ // then returns the value at that position.
+
+ public int previous() throws Exception;
+ // return the value at current position,
+ // then move the iterator back one position.
+
+ public void set(int value);
+
+}
diff --git a/lab/lab-13/lab-13/src/META-INF/MANIFEST.MF b/lab/lab-13/lab-13/src/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..58630c0
--- /dev/null
+++ b/lab/lab-13/lab-13/src/META-INF/MANIFEST.MF
@@ -0,0 +1,2 @@
+Manifest-Version: 1.0
+
diff --git a/lecture/slide/~$Ch10_AVLTree_ENG-2018.pptx b/lecture/slide/~$Ch10_AVLTree_ENG-2018.pptx
new file mode 100644
index 0000000..51c206b
Binary files /dev/null and b/lecture/slide/~$Ch10_AVLTree_ENG-2018.pptx differ