liveBook · Manning

Heap

As explained in Classic Computer Science Problems in Swift: Essential techniques for practicing programmers

A binary heap is a tree-shaped priority-queue implementation that maintains an ordering relationship between parents and children.

A binary heap is a tree-shaped implementation commonly used inside a priority queue. It keeps frontier elements ordered so popping returns the highest-priority item; in A*, that means the state with the lowest f(n) = g(n) + h(n). section 2.2

In the A* search setting, the heap-backed priority queue provides:

  • Priority-based removal of the next frontier state rather than stack-like or queue-like traversal. section 2.2
  • O(lg n) insertion and removal operations. section 2.2
  • Comparison through Swift's Comparable protocol, using each node's cost plus heuristic. section 2.2

A* maze search

A* places maze nodes in an ascending priority queue and uses Manhattan distance as the heuristic, causing the next pop to favor the lowest estimated total path cost. section 2.2

How Classic Computer Science Problems in Swift: Essential techniques for practicing programmers covers it

How deep it goes
Defines heap-backed priority queues and operation costs but defers the heap's internal implementation.
Examples
Demonstrates heap-backed ordering through Swift A* maze searches using node cost and Manhattan distance.
What it assumes
Assumes priority queues, comparable elements, graph search, path cost, and heuristic functions.
What you can do after
Apply it via SwiftPriorityQueue when implementing A* frontier selection.
How it connects
Connects binary heaps with priority queues, A* search, heuristics, Comparable, and frontier management.

Also explained in

Compare how 3 books explain heap →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Computer Science Problems in Swift: Essential techniques for practicing programmers. The section links point to the parts of the book it draws on.