diff --git a/src/Aufgabe8Test.java b/src/Aufgabe8Test.java index 501923f..7dc1331 100644 --- a/src/Aufgabe8Test.java +++ b/src/Aufgabe8Test.java @@ -24,16 +24,18 @@ public class Aufgabe8Test { 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()); + assertEquals(12, map.size()); + MassiveIterator iterator = map.getKeys().iterator(); - int count = 0; - while (iterator.hasNext()) { + int count; + for (count = 0; iterator.hasNext(); count++) { if (iterator.next().equals(mars)) { iterator.remove(); } - count++; } assertEquals(12, count); assertEquals(11, map.getKeys().size()); + assertEquals(11, map.size()); assertFalse(map.getKeys().contains(mars)); assertThrows(NoSuchElementException.class, iterator::next); diff --git a/src/BodyForceTreeMap.java b/src/BodyForceTreeMap.java index a0d894b..5ce1cf4 100644 --- a/src/BodyForceTreeMap.java +++ b/src/BodyForceTreeMap.java @@ -87,14 +87,14 @@ public class BodyForceTreeMap { } private String toString(Item item) { - String s = ""; + StringBuilder s = new StringBuilder(); if (item == null) { - return s; + return s.toString(); } - s += this.toString(item.right); - s += String.format("{%s: %s}\n", item.key, item.value); - s += this.toString(item.left); - return s; + s.append(this.toString(item.right)); + s.append(String.format("{%s: %s}\n", item.key, item.value)); + s.append(this.toString(item.left)); + return s.toString(); } /** diff --git a/src/MassiveForceTreeMap.java b/src/MassiveForceTreeMap.java index 523fdb1..a15a240 100644 --- a/src/MassiveForceTreeMap.java +++ b/src/MassiveForceTreeMap.java @@ -88,14 +88,14 @@ public class MassiveForceTreeMap implements MassiveSet { } private String toString(Item item) { - String s = ""; + StringBuilder s = new StringBuilder(); if (item == null) { - return s; + return s.toString(); } - s += this.toString(item.right); - s += String.format("{%s: %s}\n", item.key, item.value); - s += this.toString(item.left); - return s; + s.append(this.toString(item.right)); + s.append(String.format("{%s: %s}\n", item.key, item.value)); + s.append(this.toString(item.left)); + return s.toString(); } /** @@ -122,26 +122,36 @@ public class MassiveForceTreeMap implements MassiveSet { @Override public MassiveIterator iterator() { return new MassiveIterator() { + private Item last = null; private Item next = root.getLeftLeaf(); @Override public Massive next() { if (!hasNext()) throw new NoSuchElementException(); - Massive m = next.key; + last = next; Item newNext = (next.right != null) ? next.right.getLeftLeaf() : next.parent; + while (newNext != null && newNext.right == next) { next = newNext; newNext = newNext.parent; } + next = newNext; - return m; + return last.key; } @Override public boolean hasNext() { 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; while (item != null) { if (item.key.equals(element)) { - Item newP = null; - 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--; + removeItem(item); return; } else if (item.key.mass() > element.mass()) { item = item.left; @@ -174,6 +173,44 @@ public class MassiveForceTreeMap implements MassiveSet { 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 @@ -220,17 +257,13 @@ public class MassiveForceTreeMap implements MassiveSet { public Item getLeftLeaf() { Item cur = this; - while (cur.left != null) { - cur = cur.left; - } + while (cur.left != null) cur = cur.left; return cur; } public Item getRightLeaf() { Item cur = this; - while (cur.right != null) { - cur = cur.right; - } + while (cur.right != null) cur = cur.right; return cur; } }