Fix SkipList remove operation (#3160)

This commit is contained in:
Artem Boiarshinov 2022-06-22 16:56:35 +03:00 committed by GitHub
parent e59568bc5e
commit d14a5d1eed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions

View File

@ -117,7 +117,9 @@ public class SkipList<E extends Comparable<E>> {
} }
for (int i = 0; i <= layer; i++) { for (int i = 0; i <= layer; i++) {
current.previous(i).setNext(i, current.next(i)); current.previous(i).setNext(i, current.next(i));
current.next(i).setPrevious(i, current.previous(i)); if (current.next(i) != null) {
current.next(i).setPrevious(i, current.previous(i));
}
} }
size--; size--;
} }

View File

@ -42,12 +42,26 @@ class SkipListTest {
} }
@Test @Test
void remove() { void removeFromHead() {
SkipList<String> skipList = createSkipList(); SkipList<String> skipList = createSkipList();
String mostLeftElement = skipList.get(0);
int initialSize = skipList.size(); int initialSize = skipList.size();
print(skipList); print(skipList);
skipList.remove("a"); skipList.remove(mostLeftElement);
print(skipList);
assertEquals(initialSize - 1, skipList.size());
}
@Test
void removeFromTail() {
SkipList<String> skipList = createSkipList();
String mostRightValue = skipList.get(skipList.size() - 1);
int initialSize = skipList.size();
print(skipList);
skipList.remove(mostRightValue);
print(skipList); print(skipList);
assertEquals(initialSize - 1, skipList.size()); assertEquals(initialSize - 1, skipList.size());