liveBook · Manning

Node

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

Node is a book-specific wrapper that stores a search state together with its parent, cost, and heuristic information.

A search state alone cannot tell you how you reached it. A `Node<T>` wraps the state with a parent link, a cost, and a heuristic. The initial node has no parent; nodes created during a search can point back toward it. section 2.2

class Node<T>: Comparable, Hashable {
    let state: T
    let parent: Node?
    let cost: Float
    let heuristic: Float
    init(state: T, parent: Node?, cost: Float = 0.0, heuristic: Float = 0.0) {
        self.state = state
        self.parent = parent
        self.cost = cost
        self.heuristic = heuristic
    }

    var hashValue: Int { return Int(cost + heuristic) }
}

func < <T>(lhs: Node<T>, rhs: Node<T>) -> Bool {
    return (lhs.cost + lhs.heuristic) < (rhs.cost + rhs.heuristic)
}

func == <T>(lhs: Node<T>, rhs: Node<T>) -> Bool {
    return lhs === rhs
}

The Swift definition compares nodes by cost plus heuristic, but tests equality by identity. section 2.2

Depth-first and breadth-first search keep nodes in their frontiers while tracking explored states separately. Once either search reaches the goal, `nodeToPath` follows parent links backward to recover the route. A* also uses each node’s cost and heuristic to prioritize its frontier. section 2.2

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

How deep it goes
Defines the generic Swift class, its fields and comparisons, then uses parent links to reconstruct a path.
Examples
Traces maze locations from a goal node back to the start and marks the resulting grid path.
What it assumes
Assumes familiarity with Swift classes, generics, and collections; constructs the maze locations and search frontiers.
What you can do after
Build it from scratch in Swift and reuse it across depth-first, breadth-first, and A* search.
How it connects
Connects depth-first search, breadth-first search, A*, priority queues, and heuristic functions.

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.