GraphEdge structures are almost complete! The last piece to the current Graph implementation is the ability to remove nodes.
Removing a node in a graph is a bit more complex as compared to removing one from a tree. We’ll need to:
- Remove it from the
- Remove any edges that contain the node from the
- Remove the node from any other nodes’s
Get started by creating a new
removeNode(_:) with an omitted argument label and an internal parameter
node of type
removeNode(_:), create an
if let constant
index equal to the first index inside
nodes that matches the node to remove. If a node is found, remove the node from the
nodes array using the
if let statement, assign
edges filtered where
GraphEdge.nodeTwo is not equal to the node to remove.
As a final step, we’ll need to remove the
node as a neighbor from each node in the
nodes array. Use a
nodes and call
removeNeighbor(node) on each element using shorthand argument syntax.