Chapters

Hide chapters

Data Structures & Algorithms in Swift

Fifth Edition · iOS 18 · Swift 6.0 · Xcode 16.2

22. Heaps
Written by Vincent Ngo

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Heaps are another classical tree-based data structure with special properties for making it great for quickly fetching the largest or smallest element.

In this chapter, you will focus on creating and manipulating heaps. You’ll see how convenient it is to fetch the minimum and maximum element of a collection.

What is a heap?

A heap is a complete binary tree, also known as a binary heap, that can be constructed using an array.

Note: Don’t confuse these heaps with memory heaps. The term heap is sometimes confusingly used in computer science to refer to a pool of memory. Memory heaps are a different concept and not what you are studying here.

Heaps come in two flavors:

  1. Max heap, in which elements with a higher value have a higher priority.
  2. Min heap, in which elements with a lower value have a higher priority.

The heap property

A heap has an essential characteristic that must always be satisfied. This characteristic is known as the heap invariant or heap property.

41 3 4 9 4 Dox Voub 5 4 0 3 8 Nuk Koer

8 97 9 3 5 Mixoj 6 Puzuy 4 Badam 9

Heap applications

Some practical applications of a heap include:

Common heap operations

Open the empty starter playground for this chapter. Start by defining the following basic Heap type:

How do you represent a heap?

Trees hold nodes that store references to their children. In the case of a binary tree, these are references to a left and right child. Heaps are indeed binary trees, but they can be represented with a simple array. This representation might seem like an unusual way to build a tree. But one of the benefits of this heap implementation is efficient time and space complexity, as the elements in a heap are all stored together in memory. You will see later on that swapping elements will play a big part in heap operations. This manipulation is also easier to do with an array than with a binary tree data structure. Take a look at how you can represent a heap using an array. Take the following binary heap:

Luzeg 1 Wemed 7 Menuh 6 Zolux 7 8 2 2 6 1 40 1 8 6 9 2 7 1 2 3 8

33 7 7 0 6 9 1 0 2 ezpud 4 2 4 0 1 8 5 pafuq 0 xejid 8 yogub 4 xuwub 5

38 3 6 2 7 5 2 5 0 amvuy 2 3 1 2 2 9 5 u = 3 Dixh: 1i + 0 = 9 a = 5 Xibxv: 1a + 9 = 1 Gusj: 4i + 5 = 8 a = 8 Rukzv: 4o + 8 = 8 u = 3

Removing from a heap

A basic remove operation removes the root node from the heap.

9 2 8 3 27 8 6 6 3 7 6 8 9 0 4 44

4 1 6 6 1 5 7 7 6 3 2 0 9 96 0

0 7 0 9 1 5 3 8 5 8 2 1 4 2

8 9 0 0 4 1 9 0 2 2 0 1 1 8

7 9 1 5 9 8 1

Implementation of remove

Add the following method to Heap:

Inserting into a heap

Let’s say you insert a value of 7 to the heap below:

4 2 5 7 1 3 6

2 3 8 2 8 2 7 9

5 1 2 0 0 0 4 9 4 6 4 6 7 3 2 9

6 9 9 6 0 6 8 2 7 8 6 9 2 1 8 5

2 1 9 7 6 1 5 0 1 1 9 8 4 5 1 9

Implementation of insert

Add the following method to Heap:

Removing from an arbitrary index

Add the following to Heap:

2 4 3 2 3 45 93 2 0 1 8 8 0 0 Yukuxi 7
Ymiwhitg oz xoho

5 0 1 9 9 Qayefi 6 6 17 5 7 75
Wsiwxacg loqq xeme

Searching for an element in a heap

To find the index of the element you wish to delete, you must perform a search on the heap. Unfortunately, heaps are not designed for fast searches. With a binary search tree, you can perform a search in O(log n) time, but since heaps are built using an array, and the node ordering in an array is different, you can’t even perform a binary search.

Building a heap

You now have all the necessary tools to represent a heap. To wrap up this chapter, you’ll build a heap from an existing array of elements and test it out. Update the initializer of Heap as follows:

6 5 6 3 0 3 9 0 Fekvew ew sufarjj = sawod kurler ec esoromdd /1 4 = 4 / 3

Testing

Time to try it out. Add the following to your playground:

Key points

  • Here is a summary of the algorithmic complexity of the heap operations you implemented in this chapter:
Ozixoneofp Haxe Jabnwiwifl fiwuwu 5(dun z) ukdepm 5(giq h) feathg 1(v) beiw 6(8) Keek Hoyi Wpmajlumi
Geuj ucoziyuen qopa tukhfotemf

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now