This commit is contained in:
2022-06-04 13:30:20 +02:00
parent ac40ada302
commit 150eb7aa49
3 changed files with 70 additions and 35 deletions

View File

@ -24,16 +24,18 @@ public class Aufgabe8Test {
map.put(new NamedBody("Hygiea", 8.32E19, new Vector3(), new Vector3()), new Vector3()); map.put(new NamedBody("Hygiea", 8.32E19, new Vector3(), new Vector3()), new Vector3());
map.put(new NamedBody("Ceres", 9.394E20, new Vector3(), new Vector3()), new Vector3()); map.put(new NamedBody("Ceres", 9.394E20, new Vector3(), new Vector3()), new Vector3());
assertEquals(12, map.size());
MassiveIterator iterator = map.getKeys().iterator(); MassiveIterator iterator = map.getKeys().iterator();
int count = 0; int count;
while (iterator.hasNext()) { for (count = 0; iterator.hasNext(); count++) {
if (iterator.next().equals(mars)) { if (iterator.next().equals(mars)) {
iterator.remove(); iterator.remove();
} }
count++;
} }
assertEquals(12, count); assertEquals(12, count);
assertEquals(11, map.getKeys().size()); assertEquals(11, map.getKeys().size());
assertEquals(11, map.size());
assertFalse(map.getKeys().contains(mars)); assertFalse(map.getKeys().contains(mars));
assertThrows(NoSuchElementException.class, iterator::next); assertThrows(NoSuchElementException.class, iterator::next);

View File

@ -87,14 +87,14 @@ public class BodyForceTreeMap {
} }
private String toString(Item item) { private String toString(Item item) {
String s = ""; StringBuilder s = new StringBuilder();
if (item == null) { if (item == null) {
return s; return s.toString();
} }
s += this.toString(item.right); s.append(this.toString(item.right));
s += String.format("{%s: %s}\n", item.key, item.value); s.append(String.format("{%s: %s}\n", item.key, item.value));
s += this.toString(item.left); s.append(this.toString(item.left));
return s; return s.toString();
} }
/** /**

View File

@ -88,14 +88,14 @@ public class MassiveForceTreeMap implements MassiveSet {
} }
private String toString(Item item) { private String toString(Item item) {
String s = ""; StringBuilder s = new StringBuilder();
if (item == null) { if (item == null) {
return s; return s.toString();
} }
s += this.toString(item.right); s.append(this.toString(item.right));
s += String.format("{%s: %s}\n", item.key, item.value); s.append(String.format("{%s: %s}\n", item.key, item.value));
s += this.toString(item.left); s.append(this.toString(item.left));
return s; return s.toString();
} }
/** /**
@ -122,26 +122,36 @@ public class MassiveForceTreeMap implements MassiveSet {
@Override @Override
public MassiveIterator iterator() { public MassiveIterator iterator() {
return new MassiveIterator() { return new MassiveIterator() {
private Item last = null;
private Item next = root.getLeftLeaf(); private Item next = root.getLeftLeaf();
@Override @Override
public Massive next() { public Massive next() {
if (!hasNext()) throw new NoSuchElementException(); if (!hasNext()) throw new NoSuchElementException();
Massive m = next.key; last = next;
Item newNext = (next.right != null) ? next.right.getLeftLeaf() : next.parent; Item newNext = (next.right != null) ? next.right.getLeftLeaf() : next.parent;
while (newNext != null && newNext.right == next) { while (newNext != null && newNext.right == next) {
next = newNext; next = newNext;
newNext = newNext.parent; newNext = newNext.parent;
} }
next = newNext; next = newNext;
return m; return last.key;
} }
@Override @Override
public boolean hasNext() { public boolean hasNext() {
return next != null; return next != null;
} }
@Override
public void remove() {
if (last == null) throw new IllegalStateException();
removeItem(last);
last = null;
}
}; };
} }
@ -155,18 +165,7 @@ public class MassiveForceTreeMap implements MassiveSet {
Item item = root; Item item = root;
while (item != null) { while (item != null) {
if (item.key.equals(element)) { if (item.key.equals(element)) {
Item newP = null; removeItem(item);
if (item.left != null) {
newP = item.left.getRightLeaf();
} else if (item.right != null) {
newP = item.right.getLeftLeaf();
}
if (item.parent.left == item) {
item.parent.setLeft(newP);
} else {
item.parent.setRight(newP);
}
size--;
return; return;
} else if (item.key.mass() > element.mass()) { } else if (item.key.mass() > element.mass()) {
item = item.left; item = item.left;
@ -174,6 +173,44 @@ public class MassiveForceTreeMap implements MassiveSet {
item = item.right; item = item.right;
} }
} }
throw new NoSuchElementException();
}
private void removeItem(Item item) {
size--;
Item newP = null;
if (item.left != null) {
newP = item.left.getRightLeaf();
if (newP != item.left) {
newP.parent.setRight(newP.left);
newP.setLeft(item.left);
}
newP.setRight(item.right);
} else if (item.right != null) {
newP = item.right.getLeftLeaf();
if (newP != item.right) {
newP.parent.setLeft(newP.right);
newP.setRight(item.right);
}
newP.setLeft(item.left);
}
if (newP == null) {
root = null;
return;
}
if (item.parent != null) {
if (item.parent.left == item) {
item.parent.setLeft(newP);
} else {
item.parent.setRight(newP);
}
} else {
root = newP;
newP.parent = null;
}
} }
@Override @Override
@ -220,17 +257,13 @@ public class MassiveForceTreeMap implements MassiveSet {
public Item getLeftLeaf() { public Item getLeftLeaf() {
Item cur = this; Item cur = this;
while (cur.left != null) { while (cur.left != null) cur = cur.left;
cur = cur.left;
}
return cur; return cur;
} }
public Item getRightLeaf() { public Item getRightLeaf() {
Item cur = this; Item cur = this;
while (cur.right != null) { while (cur.right != null) cur = cur.right;
cur = cur.right;
}
return cur; return cur;
} }
} }