0007 edited[changed getKeyAsInt to getKey with cast to int] added[Recursive Bubblesort which is not working]
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
package MyList;
|
||||
|
||||
public class MyList {
|
||||
public MyListElement first;
|
||||
private int size = 0;
|
||||
@@ -7,82 +8,82 @@ public class MyList {
|
||||
first = null;
|
||||
}
|
||||
|
||||
//Adds elem to list.
|
||||
public void add(MyPair pair) {
|
||||
// Adds elem to list.
|
||||
public void add(MyPair<Integer, Integer> pair) {
|
||||
MyListElement elem = new MyListElement(pair, null);
|
||||
//Case 1: List is empty
|
||||
// Case 1: List is empty
|
||||
if (this.first == null) {
|
||||
this.first = elem;
|
||||
//Case 2: List is not empty
|
||||
// Case 2: List is not empty
|
||||
} else {
|
||||
MyListElement node = this.first;
|
||||
while (node.next != null) {
|
||||
node = node.next;
|
||||
while (node.getNext() != null) {
|
||||
node = node.getNext();
|
||||
}
|
||||
node.next = elem;
|
||||
node.setNext(elem);
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
//Inserts elem at index.
|
||||
public void insert(MyPair pair, int index) {
|
||||
// Inserts elem at index.TODO only works for 0 atm
|
||||
public void insert(MyPair<Integer, Integer> pair, int index) {
|
||||
MyListElement node = this.first;
|
||||
MyListElement pre = this.first;
|
||||
if (index == 0) {
|
||||
this.first = new MyListElement(pair, node);
|
||||
} else {
|
||||
int i = 1;
|
||||
while ((index != i) && (node.next != null)) {
|
||||
while ((index != i) && (node.getNext() != null)) {
|
||||
pre = node;
|
||||
node = node.next;
|
||||
node = node.getNext();
|
||||
i++;
|
||||
}
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
//Removes element with key from list.
|
||||
// Removes element with key from list.
|
||||
public void remove(int key) {
|
||||
//Case 1: Element to remove is first
|
||||
if (this.first.data.key == key) {
|
||||
this.first = this.first.next;
|
||||
//Case 2: Element to remove is not first
|
||||
// Case 1: Element to remove is first
|
||||
if (this.first.getData().getKey() == key) {
|
||||
this.first = this.first.getNext();
|
||||
// Case 2: Element to remove is not first
|
||||
} else {
|
||||
MyListElement node = this.first;
|
||||
MyListElement pre = this.first;
|
||||
while ((node.next != null) && (node.data.key != key)) {
|
||||
while ((node.getNext() != null) && (node.getData().getKey() != key)) {
|
||||
pre = node;
|
||||
node = node.next;
|
||||
if (node.data.key == key) {
|
||||
pre.next = node.next;
|
||||
node = node.getNext();
|
||||
if (node.getData().getKey() == key) {
|
||||
pre.setNext(node.getNext());
|
||||
}
|
||||
}
|
||||
}
|
||||
size--;
|
||||
}
|
||||
|
||||
//Return MyList as String.
|
||||
// Return MyList as String.
|
||||
@Override
|
||||
public String toString() {
|
||||
String listAsString = "";
|
||||
MyListElement node = this.first;
|
||||
while (node != null) {
|
||||
listAsString += (node.data.toString() + ", ");
|
||||
node = node.next;
|
||||
listAsString += (node.getData().toString() + ", ");
|
||||
node = node.getNext();
|
||||
}
|
||||
return listAsString;
|
||||
}
|
||||
|
||||
public MyPair[] toArray() {
|
||||
MyPair[] listAsArray = new MyPair[this.size];
|
||||
public MyPair<Integer, Integer>[] toArray() {
|
||||
MyPair<Integer, Integer>[] listAsArray = new MyPair[this.size];
|
||||
MyListElement node = this.first;
|
||||
int i = 0;
|
||||
while (node.next != null) {
|
||||
listAsArray[i] = node.data;
|
||||
node = node.next;
|
||||
while (node.getNext() != null) {
|
||||
listAsArray[i] = node.getData();
|
||||
node = node.getNext();
|
||||
i++;
|
||||
}
|
||||
listAsArray[i] = node.data;
|
||||
listAsArray[i] = node.getData();
|
||||
return listAsArray;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
package MyList;
|
||||
|
||||
public class MyListElement {
|
||||
public MyPair data;
|
||||
public MyListElement next;
|
||||
private MyPair<Integer, Integer> data;
|
||||
private MyListElement next;
|
||||
|
||||
public MyListElement() {
|
||||
data = null;
|
||||
next = null;
|
||||
}
|
||||
|
||||
public MyListElement(MyPair data, MyListElement next) {
|
||||
public MyListElement(MyPair<Integer, Integer> data, MyListElement next) {
|
||||
this.data = data;
|
||||
this.next = next;
|
||||
}
|
||||
@@ -17,4 +18,20 @@ public class MyListElement {
|
||||
public String toString() {
|
||||
return this.data.toString();
|
||||
}
|
||||
|
||||
public MyPair<Integer, Integer> getData() {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
public MyListElement getNext() {
|
||||
return this.next;
|
||||
}
|
||||
|
||||
public void setData(MyPair<Integer, Integer> data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public void setNext(MyListElement next) {
|
||||
this.next = next;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,24 +1,32 @@
|
||||
package MyList;
|
||||
public class MyPair {
|
||||
public int key;
|
||||
public Object data;
|
||||
|
||||
public MyPair() {
|
||||
key = -1;
|
||||
data = new Object();
|
||||
}
|
||||
public class MyPair<K, V> {
|
||||
private K key;
|
||||
private V value;
|
||||
|
||||
public MyPair(int key, Object data) {
|
||||
public MyPair(K key, V value) {
|
||||
this.key = key;
|
||||
this.data = data;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ("(" + Integer.toString(this.key) + "," + this.data.toString() + ")");
|
||||
return ("(" + this.key + "," + this.value + ")");
|
||||
}
|
||||
|
||||
public int getKeyAsInt() {
|
||||
return (int)this.key;
|
||||
public K getKey() {
|
||||
return this.key;
|
||||
}
|
||||
|
||||
public V getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public void setKey(K key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public void setValue(V value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,11 +11,11 @@ public class MySearchAlgorithms {
|
||||
public static MyPair sequentielSearchList(int oKey, MyList oList) {
|
||||
MyPair oData = null;
|
||||
MyListElement elem = oList.first;
|
||||
while ((elem.next != null) && (elem.data.key != oKey)) {
|
||||
elem = elem.next;
|
||||
while ((elem.getNext() != null) && (elem.getData().getKey() != oKey)) {
|
||||
elem = elem.getNext();
|
||||
}
|
||||
if (elem.data.key == oKey) {
|
||||
oData = elem.data;
|
||||
if (elem.getData().getKey() == oKey) {
|
||||
oData = elem.getData();
|
||||
}
|
||||
return oData;
|
||||
}
|
||||
@@ -30,9 +30,9 @@ public class MySearchAlgorithms {
|
||||
private static MyPair helpBinarySearch(Object oKey, int iLeft, int iRight, MyPair[] oArray) {
|
||||
MyPair oData = null;
|
||||
int m = (int)(iLeft + iRight) / 2;
|
||||
if (oArray[m].getKeyAsInt() > (int)oKey) {
|
||||
if ((int)(oArray[m].getKey()) > (int)oKey) {
|
||||
oData = helpBinarySearch(oKey, iLeft, (m - 1), oArray);
|
||||
} else if (oArray[m].getKeyAsInt() < (int)oKey) {
|
||||
} else if ((int)(oArray[m].getKey()) < (int)oKey) {
|
||||
oData = helpBinarySearch(oKey, (m + 1), iRight, oArray);
|
||||
} else {
|
||||
oData = oArray[m];
|
||||
@@ -48,10 +48,10 @@ public class MySearchAlgorithms {
|
||||
int iR = 0;
|
||||
for (int i = 0; i < oArray.length; i++) {
|
||||
int iIndex = ((int)Math.pow(2, i));
|
||||
if ((int)oArray[iIndex].key > (int)oKey) {
|
||||
if ((int)oArray[iIndex].getKey() > (int)oKey) {
|
||||
iR = iIndex;
|
||||
break;
|
||||
} else if ((int)oArray[iIndex].key == (int)oKey) {
|
||||
} else if ((int)oArray[iIndex].getKey() == (int)oKey) {
|
||||
oData = oArray[iIndex];
|
||||
bSuccess = true;
|
||||
break;
|
||||
|
||||
@@ -20,7 +20,7 @@ public class MySortAlgorithms {
|
||||
public static void myBubbleSortInSitu(MyPair[] arr) {
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
for (int j = 0; (j + 1) < arr.length - i; j++) {
|
||||
if (arr[j].getKeyAsInt() > arr[j + 1].getKeyAsInt()) {
|
||||
if ((int)(arr[j].getKey()) > (int)(arr[j + 1].getKey())) {
|
||||
MyPair store = arr[j];
|
||||
arr[j] = arr[j + 1];
|
||||
arr[j + 1] = store;
|
||||
@@ -42,7 +42,7 @@ public class MySortAlgorithms {
|
||||
//Sorting starts here
|
||||
for (int i = 0; i < sortedSequence.length; i++) {
|
||||
for (int j = 0; (j + 1) < sortedSequence.length - i; j++) {
|
||||
if (sortedSequence[j].getKeyAsInt() > sortedSequence[j + 1].getKeyAsInt()) {
|
||||
if ((int)(sortedSequence[j].getKey()) > (int)(sortedSequence[j + 1].getKey())) {
|
||||
MyPair store = sortedSequence[j];
|
||||
sortedSequence[j] = sortedSequence[j + 1];
|
||||
sortedSequence[j + 1] = store;
|
||||
@@ -52,6 +52,32 @@ public class MySortAlgorithms {
|
||||
return sortedSequence;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO
|
||||
* @param arr
|
||||
*/
|
||||
public static void myBubbleSortRecursiveInSitu(MyPair[] arr, int iStartIndex) {
|
||||
if (iStartIndex + 1 < arr.length) {
|
||||
myBubbleSortRecursiveInSitu(arr, iStartIndex + 1);
|
||||
|
||||
if ((int)(arr[iStartIndex].getKey()) > (int)(arr[iStartIndex + 1].getKey())) {
|
||||
myBubbleSortRecursiveInSituSwap(arr, iStartIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param arr
|
||||
* @param i
|
||||
* @param j
|
||||
*/
|
||||
private static void myBubbleSortRecursiveInSituSwap(MyPair[] arr, int i) {
|
||||
MyPair store = arr[i];
|
||||
arr[i] = arr[i + 1];
|
||||
arr[i + 1] = store;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param arr
|
||||
@@ -59,11 +85,11 @@ public class MySortAlgorithms {
|
||||
public static void mySelectionSortInSitu(MyPair[] arr) {
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
int smallestIndex = arr.length - 1;
|
||||
int smallestKey = arr[arr.length - 1].getKeyAsInt();
|
||||
int smallestKey = (int)(arr[arr.length - 1].getKey());
|
||||
for (int j = (arr.length - 1); j >= i; j--) {
|
||||
if (arr[j].getKeyAsInt() < smallestKey) {
|
||||
if ((int)(arr[j].getKey()) < smallestKey) {
|
||||
smallestIndex = j;
|
||||
smallestKey = arr[j].getKeyAsInt();
|
||||
smallestKey = (int)(arr[j].getKey());
|
||||
}
|
||||
}
|
||||
MyPair store = arr[i];
|
||||
@@ -86,11 +112,11 @@ public class MySortAlgorithms {
|
||||
//Sorting starts here
|
||||
for (int i = 0; i < sortedSequence.length; i++) {
|
||||
int smallestIndex = sortedSequence.length - 1;
|
||||
int smallestKey = sortedSequence[sortedSequence.length - 1].getKeyAsInt();
|
||||
int smallestKey = (int)(sortedSequence[sortedSequence.length - 1].getKey());
|
||||
for (int j = (sortedSequence.length - 1); j >= i; j--) {
|
||||
if (sortedSequence[j].getKeyAsInt() < smallestKey) {
|
||||
if ((int)(sortedSequence[j].getKey()) < smallestKey) {
|
||||
smallestIndex = j;
|
||||
smallestKey = sortedSequence[j].getKeyAsInt();
|
||||
smallestKey = (int)(sortedSequence[j].getKey());
|
||||
}
|
||||
}
|
||||
MyPair store = sortedSequence[i];
|
||||
@@ -115,7 +141,7 @@ public class MySortAlgorithms {
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
int index = 0;
|
||||
for (int j = 0; j <= i; j++) {
|
||||
if (arr[j].getKeyAsInt() >= arr[i].getKeyAsInt()) {
|
||||
if ((int)(arr[j].getKey()) >= (int)(arr[i].getKey())) {
|
||||
index = j;
|
||||
}
|
||||
}
|
||||
@@ -153,7 +179,7 @@ public class MySortAlgorithms {
|
||||
int unpartitionedAreaLeft = left;
|
||||
int pivot = right;
|
||||
for (int k = 0; k < arr.length; k++) {
|
||||
if (arr[unpartitionedAreaLeft].getKeyAsInt() <= arr[pivot].getKeyAsInt()) {
|
||||
if ((int)(arr[unpartitionedAreaLeft].getKey()) <= (int)(arr[pivot].getKey())) {
|
||||
MyPair store = arr[unpartitionedAreaLeft];
|
||||
arr[unpartitionedAreaLeft] = arr[smallerPivotIndexRight + 1];
|
||||
arr[smallerPivotIndexRight + 1] = store;
|
||||
|
||||
@@ -10,14 +10,14 @@ public class MyTestList {
|
||||
@Test
|
||||
public void testToArray() {
|
||||
MyList list = new MyList();
|
||||
list.add(new MyPair(0,0));
|
||||
list.add(new MyPair(1,1));
|
||||
list.add(new MyPair(2,2));
|
||||
list.add(new MyPair(3,3));
|
||||
list.add(new MyPair(4,4));
|
||||
MyPair[] listAsArray = list.toArray();
|
||||
list.add(new MyPair<Integer, Integer>(0, 0));
|
||||
list.add(new MyPair<Integer, Integer>(1, 1));
|
||||
list.add(new MyPair<Integer, Integer>(2, 2));
|
||||
list.add(new MyPair<Integer, Integer>(3, 3));
|
||||
list.add(new MyPair<Integer, Integer>(4, 4));
|
||||
MyPair<Integer, Integer>[] listAsArray = list.toArray();
|
||||
for (int i = 0; i < listAsArray.length; i++) {
|
||||
assertEquals(i, listAsArray[i].key);
|
||||
assertEquals(i, (int) listAsArray[i].getKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,36 +12,26 @@ public class MyTestSearch {
|
||||
@Test
|
||||
public void testSequentialSearch() {
|
||||
MyList list = new MyList();
|
||||
list.add(new MyPair(1,3));
|
||||
list.add(new MyPair(2,4));
|
||||
|
||||
assertEquals(3, MySearchAlgorithms.sequentielSearchList(1, list).data);
|
||||
assertEquals(4, MySearchAlgorithms.sequentielSearchList(2, list).data);
|
||||
list.add(new MyPair(1, 3));
|
||||
list.add(new MyPair(2, 4));
|
||||
|
||||
assertEquals(3, MySearchAlgorithms.sequentielSearchList(1, list).getValue());
|
||||
assertEquals(4, MySearchAlgorithms.sequentielSearchList(2, list).getValue());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testBinarySearch() {
|
||||
MyPair[] pairs = {new MyPair(1,3), new MyPair(2,4)};
|
||||
MyPair[] pairs = { new MyPair(1, 3), new MyPair(2, 4) };
|
||||
|
||||
assertEquals(3, MySearchAlgorithms.binarySearch(1, pairs).data);
|
||||
assertEquals(4, MySearchAlgorithms.binarySearch(2, pairs).data);
|
||||
assertEquals(3, MySearchAlgorithms.binarySearch(1, pairs).getValue());
|
||||
assertEquals(4, MySearchAlgorithms.binarySearch(2, pairs).getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExponentialSearch() {
|
||||
MyPair[] pairs = {new MyPair(1,3), new MyPair(2,4)};
|
||||
MyPair[] pairs = { new MyPair(1, 3), new MyPair(2, 4) };
|
||||
|
||||
assertEquals(3, MySearchAlgorithms.exponentialSearch(1, pairs).data);
|
||||
assertEquals(4, MySearchAlgorithms.exponentialSearch(2, pairs).data);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBubbleSortInSitu() {
|
||||
//Test BubbleSortInSitu
|
||||
MyPair[] arrPairs0 = {new MyPair(2,2), new MyPair(0,0), new MyPair(1,1), new MyPair(3,3)};
|
||||
MySortAlgorithms.myBubbleSortInSitu(arrPairs0);
|
||||
for (int i = 0; i < arrPairs0.length; i++) {
|
||||
assertEquals(i, arrPairs0[i].getKeyAsInt());
|
||||
}
|
||||
assertEquals(3, MySearchAlgorithms.exponentialSearch(1, pairs).getValue());
|
||||
assertEquals(4, MySearchAlgorithms.exponentialSearch(2, pairs).getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,15 @@ import MyTrees.MyTreeNode;
|
||||
|
||||
public class MyTestSort {
|
||||
|
||||
@Test
|
||||
public void testBubbleSortRecursiveInSitu() {
|
||||
//Test BubbleSortNotInSitu
|
||||
MyPair[] arrPairs0 = {new MyPair(2,2), new MyPair(0,0), new MyPair(1,1), new MyPair(3,3)};
|
||||
MySortAlgorithms.myBubbleSortRecursiveInSitu(arrPairs0, 0);
|
||||
for (int i = 0; i < arrPairs0.length; i++) {
|
||||
assertEquals(i, (int)(arrPairs0[i].getKey()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBubbleSortNotInSitu() {
|
||||
@@ -19,7 +28,7 @@ public class MyTestSort {
|
||||
MyPair[] arrPairs0 = {new MyPair(2,2), new MyPair(0,0), new MyPair(1,1), new MyPair(3,3)};
|
||||
MyPair[] arrPairs1 = MySortAlgorithms.myBubbleSortNotInSitu(arrPairs0);
|
||||
for (int i = 0; i < arrPairs1.length; i++) {
|
||||
assertEquals(i, arrPairs1[i].getKeyAsInt());
|
||||
assertEquals(i, (int)(arrPairs1[i].getKey()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +38,7 @@ public class MyTestSort {
|
||||
MyPair[] arrPairs0 = {new MyPair(2,2), new MyPair(0,0), new MyPair(1,1), new MyPair(3,3)};
|
||||
MySortAlgorithms.mySelectionSortInSitu(arrPairs0);
|
||||
for (int i = 0; i < arrPairs0.length; i++) {
|
||||
assertEquals(i, arrPairs0[i].getKeyAsInt());
|
||||
assertEquals(i, (int)(arrPairs0[i].getKey()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,11 +48,18 @@ public class MyTestSort {
|
||||
MyPair[] arrPairs0 = {new MyPair(2,2), new MyPair(0,0), new MyPair(1,1), new MyPair(3,3)};
|
||||
MyPair[] arrPairs1 = MySortAlgorithms.mySelectionSortNotInSitu(arrPairs0);
|
||||
for (int i = 0; i < arrPairs0.length; i++) {
|
||||
assertEquals(i, arrPairs1[i].getKeyAsInt());
|
||||
assertEquals(i, (int)(arrPairs1[i].getKey()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void testBubbleSortInSitu() {
|
||||
// Test BubbleSortInSitu
|
||||
MyPair[] arrPairs0 = { new MyPair<Integer, Integer>(2, 2), new MyPair<Integer, Integer>(0, 0),
|
||||
new MyPair<Integer, Integer>(1, 1), new MyPair<Integer, Integer>(3, 3) };
|
||||
MySortAlgorithms.myBubbleSortInSitu(arrPairs0);
|
||||
for (int i = 0; i < arrPairs0.length; i++) {
|
||||
assertEquals(i, (int) arrPairs0[i].getKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,12 +20,12 @@ public class MyBinarySearchTree extends MyBinaryTree {
|
||||
public Object lookup(MyTreeNode node, Object key) {
|
||||
Object value = null;
|
||||
if (node != null) {
|
||||
if ((int)key < node.getKeyAsInt()) {
|
||||
if ((int) key < node.getKey()) {
|
||||
value = lookup(node.getLeftChild(), key);
|
||||
} else if ((int)key > node.getKeyAsInt()) {
|
||||
} else if ((int) key > node.getKey()) {
|
||||
value = lookup(node.getRightChild(), key);
|
||||
} else {
|
||||
value = node.getPair().data;
|
||||
value = node.getPair().getValue();
|
||||
}
|
||||
}
|
||||
return value;
|
||||
@@ -34,16 +34,17 @@ public class MyBinarySearchTree extends MyBinaryTree {
|
||||
/**
|
||||
* TODO doesnt work???
|
||||
* Inserts/replaces new pair into tree specified by root.
|
||||
*
|
||||
* @param node (root) startingpoint to search for node specified by pair.key
|
||||
* @param pair datatype which contains key and data of new inserted node.
|
||||
*/
|
||||
public void insert(MyTreeNode node, MyPair pair) {
|
||||
if (node.getKeyAsInt() == -1) {
|
||||
node = new MyTreeNode(pair.key, pair.data);
|
||||
public void insert(MyTreeNode node, MyPair<Integer, Integer> pair) {
|
||||
if (node.getKey() == -1) {
|
||||
node = new MyTreeNode(pair.getKey(), pair.getValue());
|
||||
} else {
|
||||
if (node.getKeyAsInt() == pair.getKeyAsInt()) {
|
||||
if (node.getKey() == pair.getKey()) {
|
||||
node.setPair(pair);
|
||||
} else if (node.getKeyAsInt() < pair.getKeyAsInt()) {
|
||||
} else if (node.getKey() < pair.getKey()) {
|
||||
insert(node.getLeftChild(), pair);
|
||||
} else {
|
||||
insert(node.getRightChild(), pair);
|
||||
@@ -53,43 +54,46 @@ public class MyBinarySearchTree extends MyBinaryTree {
|
||||
|
||||
/**
|
||||
* Removes node specified by key.
|
||||
*
|
||||
* @param node starting point of search for node to remove.
|
||||
* @param key specifies node to remove.
|
||||
* @param key specifies node to remove.
|
||||
*/
|
||||
public void remove(MyTreeNode node, Object key) {
|
||||
if (node != null) {
|
||||
if (node.getKeyAsInt() < (int)key) {
|
||||
if (node.getKey() < (int) key) {
|
||||
remove(node.getLeftChild(), key);
|
||||
} else if (node.getKeyAsInt() > (int)key) {
|
||||
} else if (node.getKey() > (int) key) {
|
||||
remove(node.getRightChild(), key);
|
||||
} else {
|
||||
//If node has no childs, set node to null
|
||||
// If node has no childs, set node to null
|
||||
if ((node.getLeftChild() == null) && (node.getRightChild() == null)) {
|
||||
node = null;
|
||||
//If node has only left child, set node to left child
|
||||
// If node has only left child, set node to left child
|
||||
} else if ((node.getLeftChild() != null) && (node.getRightChild() == null)) {
|
||||
node = node.getLeftChild();
|
||||
//If node has only right child, set node to right child
|
||||
// If node has only right child, set node to right child
|
||||
} else if ((node.getLeftChild() == null) && (node.getRightChild() != null)) {
|
||||
node = node.getRightChild();
|
||||
//If node has two childs
|
||||
// If node has two childs
|
||||
} else {
|
||||
//Get right parttree
|
||||
// Get right parttree
|
||||
MyTreeNode nnode = node.getRightChild();
|
||||
//Move left through right parttree till node before symmetric follower.
|
||||
// Move left through right parttree till node before symmetric follower.
|
||||
while (nnode.getLeftChild().getLeftChild() != null) {
|
||||
nnode = nnode.getLeftChild();
|
||||
}
|
||||
//Store data pair of symmetric follower
|
||||
MyPair symmetricFollowerPair = nnode.getLeftChild().getPair();
|
||||
//If symmetric follower has right child: replace symmetric follower with right child
|
||||
// Store data pair of symmetric follower
|
||||
MyPair<Integer, Integer> symmetricFollowerPair = nnode.getLeftChild().getPair();
|
||||
// If symmetric follower has right child: replace symmetric follower with right
|
||||
// child
|
||||
if (nnode.getLeftChild().getRightChild() != null) {
|
||||
nnode.setLeftChild(nnode.getLeftChild().getRightChild());
|
||||
//If symmetric follower has no childs: set symmetric follower node to null (removed).
|
||||
// If symmetric follower has no childs: set symmetric follower node to null
|
||||
// (removed).
|
||||
} else {
|
||||
nnode.setLeftChild(null);
|
||||
}
|
||||
//Replace data of node with data of symmetric follower.
|
||||
// Replace data of node with data of symmetric follower.
|
||||
node.setPair(symmetricFollowerPair);
|
||||
}
|
||||
}
|
||||
@@ -98,11 +102,12 @@ public class MyBinarySearchTree extends MyBinaryTree {
|
||||
|
||||
/**
|
||||
* Searches minimum key.
|
||||
*
|
||||
* @param node root of tree to search at.
|
||||
* @return pair of most left node of tree.
|
||||
*/
|
||||
public MyPair minimum(MyTreeNode node) {
|
||||
MyPair min = null;
|
||||
public MyPair<Integer, Integer> minimum(MyTreeNode node) {
|
||||
MyPair<Integer, Integer> min = null;
|
||||
MyTreeNode nnode = node;
|
||||
if (node != null) {
|
||||
while (nnode.getLeftChild() != null) {
|
||||
@@ -115,11 +120,12 @@ public class MyBinarySearchTree extends MyBinaryTree {
|
||||
|
||||
/**
|
||||
* Searches maximum key.
|
||||
*
|
||||
* @param node root of tree to search at.
|
||||
* @return pair of most right node of tree.
|
||||
*/
|
||||
public MyPair maximum(MyTreeNode node) {
|
||||
MyPair max = null;
|
||||
public MyPair<Integer, Integer> maximum(MyTreeNode node) {
|
||||
MyPair<Integer, Integer> max = null;
|
||||
MyTreeNode nnode = node;
|
||||
if (node != null) {
|
||||
while (nnode.getRightChild() != null) {
|
||||
|
||||
@@ -5,23 +5,21 @@ import MyList.MyPair;
|
||||
public class MyTreeNode {
|
||||
private MyTreeNode leftChild;
|
||||
private MyTreeNode rightChild;
|
||||
private MyPair pair;
|
||||
private MyPair<Integer, Integer> pair;
|
||||
|
||||
public MyTreeNode() {
|
||||
this.leftChild = null;
|
||||
this.rightChild = null;
|
||||
this.pair = new MyPair();
|
||||
this.pair = new MyPair<Integer, Integer>(null, null);
|
||||
}
|
||||
|
||||
public MyTreeNode(int key, Object data) {
|
||||
public MyTreeNode(Integer key, Integer value) {
|
||||
this.leftChild = null;
|
||||
this.rightChild = null;
|
||||
this.pair = new MyPair();
|
||||
this.pair.key = key;
|
||||
this.pair.data = data;
|
||||
this.pair = new MyPair<Integer, Integer>(key, value);
|
||||
}
|
||||
|
||||
//David Setter Methods
|
||||
// David Setter Methods
|
||||
public void setLeftChild(MyTreeNode leftNode) {
|
||||
this.leftChild = leftNode;
|
||||
}
|
||||
@@ -30,11 +28,11 @@ public class MyTreeNode {
|
||||
this.rightChild = rightNode;
|
||||
}
|
||||
|
||||
public void setPair(MyPair pair) {
|
||||
this.pair = new MyPair(pair.key, pair.data);
|
||||
public void setPair(MyPair<Integer, Integer> pair) {
|
||||
this.pair = new MyPair<Integer, Integer>(pair.getKey(), pair.getValue());
|
||||
}
|
||||
|
||||
//David Getter Methods
|
||||
// David Getter Methods
|
||||
public MyTreeNode getLeftChild() {
|
||||
return this.leftChild;
|
||||
}
|
||||
@@ -43,11 +41,11 @@ public class MyTreeNode {
|
||||
return this.rightChild;
|
||||
}
|
||||
|
||||
public MyPair getPair() {
|
||||
public MyPair<Integer, Integer> getPair() {
|
||||
return this.pair;
|
||||
}
|
||||
|
||||
public int getKeyAsInt() {
|
||||
return this.pair.getKeyAsInt();
|
||||
public Integer getKey() {
|
||||
return this.pair.getKey();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user