liveBook · Manning

Chapter 3: An immutable deque

What you will learn in chapter 3 of Fabulous Adventures in Data Structures and Algorithms

This chapter introduces immutable deques, motivates replacing naïve linked-list approaches with finger trees, and previews sublinear deque concatenation.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces immutable deques, motivates replacing naïve linked-list approaches with finger trees, and previews sublinear deque concatenation.

Key terms: immutable double-ended queue, deque, single-ended queue, finger tree, fingers, root, recursively, O(log n), O(n), Hughes list

3.1 An immutable deque abstract data type

Defines the immutable deque interface as an enumerable double-ended sequence with cheap operations at both ends.

Key terms: deque, PushLeft, PushRight, PopLeft, PopRight, Left, immutable types, new object, empty, cheap

3.2 A bad naïve implementation

This section presents a plausible recursive immutable deque implementation and shows that, despite cheap-looking end access, it rebuilds the whole structure and performs disastrously.

Key terms: deque, empty deque, single-item deque, left item, middle deque, right item, persistent, immutable data structure, reuse, push

3.3 A finger tree

This section introduces the finger-tree idea as a way to make immutable deque operations near either end cheap by keeping the ends close to the root and making costly overflows less frequent.

Key terms: finger tree, fingers, root, leftmost node, rightmost node, deque, overflow, middle, left end, right end

3.3.1 The mini-deque

This subsection defines the bounded immutable mini-deque helper used to create space at both ends of the finger-tree deque.

Key terms: mini-deque, mini, deque, immutable, one-item mini, four-item mini, pop, push, error, interface

3.3.2 A new definition of a deque

This section redefines an immutable deque as edge mini-deques around a recursive middle deque of chunks, and explains the push and pop cases that preserve its chunk invariant.

Key terms: deque of items, mini, middle deque of minis, EmptyDeque, SingleDeque, Threes, deque of minis, chunk, persistence, O(1)

3.4 Visualizing the data structure

This section visualizes how an immutable finger-tree deque grows at its edges by overflowing fixed-size minis into recursively deeper middle deques.

Key terms: Four, Three, mini, middle, EmptyDeque, SingleDeque, finger tree, deque of minis, Three of Threes, level

3.5 Amortized performance of the deque

This section analyzes the finger-tree deque’s update-cost distribution to show constant amortized operations, logarithmic worst cases and depth, reasonable storage overhead, and efficient persistence.

Key terms: left push, pop, level, left mini, middle, Three, amortized cost, worst case, O(1), O(log3 n)

3.6 Are we abusing the type system?

This section explains why the finger tree’s increasingly nested generic types are safe and practical in C# and Java.

Key terms: middle, IDeque, IMini, generic types, templates, O(log3 n)

3.7 Concatenation of deques

This section adds efficient concatenation to immutable finger-tree deques by allowing middle deques to contain Two and Three mini-deques and recursively joining their middles.

Key terms: Concatenate, EmptyDeque, SingleDeque, mini-deque, Two, Three, middle deque, invariant, Deque, middle

3.8 Performance after adding concatenation

This section shows that allowing Twos in a finger tree’s middle preserves efficient deque operations while enabling logarithmic-time concatenation.

Key terms: invariant, Twos, middle deque, O(log3 n), O(log2 n), amortized cost, concatenation, TwosOrThrees, PushRightMany, intermediary deque

3.9 Why is this tree called a finger tree?

This section identifies the chapter’s deque as a finger tree and explains why fingers at both ends make it suitable for efficient sequence operations.

Key terms: finger tree, fingers, amortized cost, leftmost node, rightmost node, double-ended queue, persistent, immutable, priority queues, interval trees

3.10 Summary

This summary recaps how finger trees provide an efficient persistent immutable deque, the design techniques behind them, and their cheap concatenation.

Where this is used later

Concepts explained in this chapter

← Chapter 2: Immutable stacks and queues Chapter 4: Memoizing immutable quadtrees to make a better Life →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.