Search⌘ K
AI Features

RootishArrayStack: A Space-Efficient Array Stack

Explore the RootishArrayStack, a space-efficient array-based stack that stores elements in blocks with minimal unused space. Understand its structure, how to calculate element positions within blocks, and perform key operations such as get, set, add, and remove efficiently.

The data structures that store their data in one or two arrays and they avoid resizing these arrays too often, the arrays frequently are not very full. For example, immediately after a resize() operation on an ArrayStack, the backing array is only half full. Even worse, there are times when only one-third of it contains data.

Visual demonstration

A sequence of add(i, x) and remove(i) operations on a RootishArrayStack is illustrated below. Arrows denote elements being copied.

In this lesson, we discuss the RootishArrayStack data structure, that addresses the problem of wasted space. The RootishArrayStack stores nn elements using O(n)O(\sqrt n) arrays. In these arrays, at most O(n)O(\sqrt n) array locations are unused at any time. All remaining array locations are used to store data. Therefore, these data structures waste at most O(n)O(\sqrt n) space when storing nn elements.

A RootishArrayStack stores its elements in a list of rr arrays called blocks that are numbered 0,1,...,r10,1,...,r−1. See the above slides. Block bb contains b+1b + 1 elements. Therefore, all rr blocks contain a total of

1+2+3++r=r(r+1)/21 + 2 + 3 + \cdots + r = r(r + 1)/2 ...