Skip to main content
2 of 3
Updated #3 to not suggest a fix
mfulton26
  • 701
  • 3
  • 8
  1. private val items: MutableList<T?> = mutableListOf() can be written more succinctly as private val items = mutableListOf<T?>() (the type is inferred and then no longer repeated (mostly) in an explicit type definition and the factory method call).

  2. size can be backed by items.size:

     val size: Int
         get() = items.size
    
     fun push(item: T) {
         set(size + 1, item)
         siftUp(size - 1)
     }
    
     ...
    
     fun pop(): T? {
         ...
         set(0, get(size - 1))
         items.removeAt(items.lastIndex) // Remove original reference to the last item
         ...
     }
    
  3. siftDown throws an AssertionError assertion when size == 0.

mfulton26
  • 701
  • 3
  • 8