Add the retrieval of minimum and maximum element from stack at O(1) (#5714)

This commit is contained in:
S M Jishanul Islam
2024-10-23 00:36:14 +06:00
committed by GitHub
parent 87030aff1e
commit 0f8cda987d
4 changed files with 287 additions and 0 deletions

View File

@@ -0,0 +1,69 @@
package com.thealgorithms.stacks;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.NoSuchElementException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class SmallestElementConstantTimeTest {
private SmallestElementConstantTime sect;
@BeforeEach
public void setSect() {
sect = new SmallestElementConstantTime();
}
@Test
public void testMinAtFirst() {
sect.push(1);
sect.push(10);
sect.push(20);
sect.push(5);
assertEquals(1, sect.getMinimumElement());
}
@Test
public void testMinTwo() {
sect.push(5);
sect.push(10);
sect.push(20);
sect.push(1);
assertEquals(1, sect.getMinimumElement());
sect.pop();
assertEquals(5, sect.getMinimumElement());
}
@Test
public void testNullMin() {
sect.push(10);
sect.push(20);
sect.pop();
sect.pop();
assertNull(sect.getMinimumElement());
}
@Test
public void testBlankHandle() {
sect.push(10);
sect.push(1);
sect.pop();
sect.pop();
assertThrows(NoSuchElementException.class, () -> sect.pop());
}
@Test
public void testPushPopAfterEmpty() {
sect.push(10);
sect.push(1);
sect.pop();
sect.pop();
sect.push(5);
assertEquals(5, sect.getMinimumElement());
sect.push(1);
assertEquals(1, sect.getMinimumElement());
}
}