Fixed AVLTrees pollFirst/pollLast

This commit is contained in:
Speiger 2021-07-23 22:04:59 +02:00
parent 4a3cc66401
commit 73916f4fd9
4 changed files with 14 additions and 16 deletions

View File

@ -3,6 +3,7 @@
### Version 0.3.3 ### Version 0.3.3
- Added: Flat/Mapping function for Iterables/Iterators to help avoid streams for cleaner looking code - Added: Flat/Mapping function for Iterables/Iterators to help avoid streams for cleaner looking code
- Fixed: AVLTrees pollFirst/Last is now keeping orders and is fixed
### Version 0.3.2 ### Version 0.3.2
- Fixed: Map.put wasn't referring to primitive variants. - Fixed: Map.put wasn't referring to primitive variants.

View File

@ -767,11 +767,11 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
entry.value = successor.value; entry.value = successor.value;
entry = successor; entry = successor;
} }
if(entry.previous() == null) first = entry.next();
if(entry.next() == null) last = entry.previous();
Entry KEY_VALUE_GENERIC_TYPE replacement = entry.left != null ? entry.left : entry.right; Entry KEY_VALUE_GENERIC_TYPE replacement = entry.left != null ? entry.left : entry.right;
if(replacement != null) { if(replacement != null) {
if(entry.replace(replacement)) tree = replacement; if(entry.replace(replacement)) tree = replacement;
if(entry == first) first = replacement;
if(entry == last) last = entry.right != null ? entry.right : replacement;
entry.left = entry.right = entry.parent = null; entry.left = entry.right = entry.parent = null;
fixAfterDeletion(replacement); fixAfterDeletion(replacement);
} }
@ -779,11 +779,6 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
else { else {
fixAfterDeletion(entry); fixAfterDeletion(entry);
entry.replace(null); entry.replace(null);
if(entry.parent != null) {
Entry KEY_VALUE_GENERIC_TYPE parent = entry.parent;
if(entry == first) first = parent.left != null ? parent.left : parent;
if(entry == last) last = entry.right != null ? parent.right : parent;
}
entry.parent = null; entry.parent = null;
} }
} }

View File

@ -514,11 +514,11 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
entry.key = successor.key; entry.key = successor.key;
entry = successor; entry = successor;
} }
if(entry.previous() == null) first = entry.next();
if(entry.next() == null) last = entry.previous();
Entry KEY_GENERIC_TYPE replacement = entry.left != null ? entry.left : entry.right; Entry KEY_GENERIC_TYPE replacement = entry.left != null ? entry.left : entry.right;
if(replacement != null) { if(replacement != null) {
if(entry.replace(replacement)) tree = replacement; if(entry.replace(replacement)) tree = replacement;
if(entry == first) first = replacement;
if(entry == last) last = entry.right != null ? entry.right : replacement;
entry.left = entry.right = entry.parent = null; entry.left = entry.right = entry.parent = null;
fixAfterDeletion(replacement); fixAfterDeletion(replacement);
} }
@ -526,11 +526,6 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
else { else {
fixAfterDeletion(entry); fixAfterDeletion(entry);
entry.replace(null); entry.replace(null);
if(entry.parent != null) {
Entry KEY_GENERIC_TYPE parent = entry.parent;
if(entry == first) first = parent.left != null ? parent.left : parent;
if(entry == last) last = entry.right != null ? parent.right : parent;
}
entry.parent = null; entry.parent = null;
} }
} }

View File

@ -49,8 +49,15 @@ public abstract class BaseIntSortedSetTest extends BaseIntCollectionTest
public void pollTest() { public void pollTest() {
if(getValidSortedSetTests().contains(SortedSetTest.POLL)) { if(getValidSortedSetTests().contains(SortedSetTest.POLL)) {
IntSortedSet set = create(TEST_ARRAY); IntSortedSet set = create(TEST_ARRAY);
Assert.assertEquals(set.pollFirstInt(), 0); for(int i = 0;i<100;i++)
Assert.assertEquals(set.pollLastInt(), 99); {
Assert.assertEquals(i, set.pollFirstInt());
}
set = create(TEST_ARRAY);
for(int i = 99;i>=0;i--)
{
Assert.assertEquals(i, set.pollLastInt());
}
} }
} }