From e3bcf8388798da85fceade381fac9370e43d7b99 Mon Sep 17 00:00:00 2001 From: Speiger Date: Sat, 9 Jan 2021 18:27:44 +0100 Subject: [PATCH] Fixing TreeSet Removal Bugs -Fixed: RB&AVL TreeSet Removal & Higher Bugs. -Fixed: Paths thanks to OvermindDL1 -Removed: JavaTests Class that was testing for finding the bugs. --- .../src/builder/example/TestBuilder.java | 2 +- .../templates/sets/AVLTreeSet.template | 52 +++++++++--------- .../templates/sets/RBTreeSet.template | 54 ++++++++++--------- .../src/collections/ints/utils/JavaTests.java | 29 ---------- 4 files changed, 56 insertions(+), 81 deletions(-) delete mode 100644 src/test/java/speiger/src/collections/ints/utils/JavaTests.java diff --git a/src/main/java/speiger/src/builder/example/TestBuilder.java b/src/main/java/speiger/src/builder/example/TestBuilder.java index 03b0ed1..1ac536e 100644 --- a/src/main/java/speiger/src/builder/example/TestBuilder.java +++ b/src/main/java/speiger/src/builder/example/TestBuilder.java @@ -22,7 +22,7 @@ public class TestBuilder extends TemplateProcessor public TestBuilder() { - super(Paths.get("src\\main\\resources\\speiger\\assets\\collections\\templates\\"), Paths.get("src\\main\\java\\speiger\\src\\collections\\"), Paths.get("src\\main\\resources\\speiger\\assets\\collections\\")); + super(Paths.get("src/main/resources/speiger/assets/collections/templates/"), Paths.get("src/main/java/speiger/src/collections/"), Paths.get("src/main/resources/speiger/assets/collections/")); } @Override diff --git a/src/main/resources/speiger/assets/collections/templates/sets/AVLTreeSet.template b/src/main/resources/speiger/assets/collections/templates/sets/AVLTreeSet.template index a6f9b72..15419df 100644 --- a/src/main/resources/speiger/assets/collections/templates/sets/AVLTreeSet.template +++ b/src/main/resources/speiger/assets/collections/templates/sets/AVLTreeSet.template @@ -225,6 +225,32 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE } protected Entry KEY_GENERIC_TYPE findCeilingNode(KEY_TYPE e) { + Entry KEY_GENERIC_TYPE entry = tree; + int compare; + while(entry != null) { + if((compare = compare(e, entry.key)) < 0) { + if(entry.left == null) break; + entry = entry.left; + continue; + } + else if(compare > 0) { + if(entry.right != null) entry = entry.right; + else { + Entry KEY_GENERIC_TYPE parent = entry.parent; + while(parent != null && parent.right == entry) { + entry = parent; + parent = parent.parent; + } + return parent; + } + continue; + } + break; + } + return entry; + } + + protected Entry KEY_GENERIC_TYPE findHigherNode(KEY_TYPE e) { Entry KEY_GENERIC_TYPE entry = tree; while(entry != null) { if(compare(e, entry.key) < 0) { @@ -246,32 +272,6 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE return null; } - protected Entry KEY_GENERIC_TYPE findHigherNode(KEY_TYPE e) { - Entry KEY_GENERIC_TYPE entry = tree; - int compare; - while(entry != null) { - if((compare = compare(e, entry.key)) < 0) { - if(entry.left == null) break; - entry = entry.left; - continue; - } - else if(compare < 0) { - if(entry.right != null) entry = entry.right; - else { - Entry KEY_GENERIC_TYPE parent = entry.parent; - while(parent != null && parent.right == entry) { - entry = parent; - parent = parent.parent; - } - return parent; - } - continue; - } - break; - } - return entry; - } - #if !TYPE_OBJECT @Override public boolean contains(KEY_TYPE e) { diff --git a/src/main/resources/speiger/assets/collections/templates/sets/RBTreeSet.template b/src/main/resources/speiger/assets/collections/templates/sets/RBTreeSet.template index 068dc59..a3aca05 100644 --- a/src/main/resources/speiger/assets/collections/templates/sets/RBTreeSet.template +++ b/src/main/resources/speiger/assets/collections/templates/sets/RBTreeSet.template @@ -225,6 +225,32 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE } protected Entry KEY_GENERIC_TYPE findCeilingNode(KEY_TYPE e) { + Entry KEY_GENERIC_TYPE entry = tree; + int compare; + while(entry != null) { + if((compare = compare(e, entry.key)) < 0) { + if(entry.left == null) break; + entry = entry.left; + continue; + } + else if(compare > 0) { + if(entry.right != null) entry = entry.right; + else { + Entry KEY_GENERIC_TYPE parent = entry.parent; + while(parent != null && parent.right == entry) { + entry = parent; + parent = parent.parent; + } + return parent; + } + continue; + } + break; + } + return entry; + } + + protected Entry KEY_GENERIC_TYPE findHigherNode(KEY_TYPE e) { Entry KEY_GENERIC_TYPE entry = tree; while(entry != null) { if(compare(e, entry.key) < 0) { @@ -246,31 +272,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE return null; } - protected Entry KEY_GENERIC_TYPE findHigherNode(KEY_TYPE e) { - Entry KEY_GENERIC_TYPE entry = tree; - int compare; - while(entry != null) { - if((compare = compare(e, entry.key)) < 0) { - if(entry.left == null) break; - entry = entry.left; - continue; - } - else if(compare < 0) { - if(entry.right != null) entry = entry.right; - else { - Entry KEY_GENERIC_TYPE parent = entry.parent; - while(parent != null && parent.right == entry) { - entry = parent; - parent = parent.parent; - } - return parent; - } - continue; - } - break; - } - return entry; - } + #if !TYPE_OBJECT @Override @@ -934,6 +936,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE if(current == previous) index--; updateNext(); updatePrevious(); + if(current.needsSuccessor()) next = current; set.removeNode(current); current = null; } @@ -1015,6 +1018,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE if(current == previous) index--; updateNext(); updatePrevious(); + if(current.needsSuccessor()) next = current; removeNode(current); current = null; } diff --git a/src/test/java/speiger/src/collections/ints/utils/JavaTests.java b/src/test/java/speiger/src/collections/ints/utils/JavaTests.java deleted file mode 100644 index 64a1d5e..0000000 --- a/src/test/java/speiger/src/collections/ints/utils/JavaTests.java +++ /dev/null @@ -1,29 +0,0 @@ -package speiger.src.collections.ints.utils; - -import java.util.Arrays; -import java.util.TreeSet; -import java.util.stream.IntStream; - -import org.junit.Assert; -import org.junit.Test; - -public class JavaTests -{ - protected static final Integer[] CONTAINS_ARRAY = new Integer[]{23, 45, 63, 89, 32}; - protected static final Integer[] TEST_ARRAY = IntStream.range(0, 100).mapToObj(Integer::valueOf).toArray(Integer[]::new); - - private TreeSet create(Integer[] array) - { - TreeSet tree = new TreeSet(); - tree.addAll(Arrays.asList(array)); - return tree; - } - - @Test - public void simpleTest() - { - TreeSet collection = create(TEST_ARRAY); - Assert.assertTrue(collection.removeAll(create(CONTAINS_ARRAY))); - Assert.assertFalse(collection.removeAll(create(CONTAINS_ARRAY))); - } -}