Binary Heap in Go

A binary heap is a complete binary tree that maintains the heap property: every parent is either less than or equal to its children (min-heap), or greater than or equal to its children (max-heap). Binary heaps are commonly used to implement priority queues (an abstract data type that finds its highest-priority item in O(1)O(1) time),… Continue reading Binary Heap in Go

Copying slices in Go

As we know, Go slices hover on top of fixed arrays; modifying elements in a slice also modifies the underlying array. In some scenarios, that’s unacceptable. Hence, we need slice copying! 1. Use slices.Clone slices.Clone is available from Go 1.21 onward. What’s cool about it is that it takes care of the pre-allocation of space… Continue reading Copying slices in Go

Stack & Queue in Go

1. Stack This is your good ol’ LIFO (last-in, first-out) structure. Just imagine a Pringles tube. A basic stack should have the following methods: In Go, you can use a slice as the underlying data structure. Notice how initiating the store slice in the NewStack[T any]() constructor is optional? This is because we have enough… Continue reading Stack & Queue in Go