// Build a spanning tree with the indicated root node. private void FindSpanningTree(MazeNode root) { Random rand = new Random(); // Set the root node's predecessor so we know it's in the tree. root.Predecessor = root; // Make a list of candidate links. List <MazeLink> links = new List <MazeLink>(); // Add the root's links to the links list. foreach (MazeNode neighbor in root.Neighbors) { if (neighbor != null) { links.Add(new MazeLink(root, neighbor)); } } // Add the other nodes to the tree. while (links.Count > 0) { // Pick a random link. int link_num = rand.Next(0, links.Count); MazeLink link = links[link_num]; links.RemoveAt(link_num); // Add this link to the tree. MazeNode to_node = link.ToNode; link.ToNode.Predecessor = link.FromNode; // Remove any links from the list that point // to nodes that are already in the tree. // (That will be the newly added node.) for (int i = links.Count - 1; i >= 0; i--) { if (links[i].ToNode.Predecessor != null) { links.RemoveAt(i); } } // Add to_node's links to the links list. foreach (MazeNode neighbor in to_node.Neighbors) { if ((neighbor != null) && (neighbor.Predecessor == null)) { links.Add(new MazeLink(to_node, neighbor)); } } } }
public MazeLink(MazeNode from_node, MazeNode to_node) { FromNode = from_node; ToNode = to_node; }