public static TreeNode BuildTree(Color[,] board) { MapNode head = BuildMap(board); TreeNode root = new TreeNode(null, head.Color); Queue<MapTreeKeyValuePair> frontLine = new Queue<MapTreeKeyValuePair>(); ISet<MapNode> visited = new HashSet<MapNode>(); frontLine.Enqueue(new MapTreeKeyValuePair{MapNode = head, TreeNode = root}); visited.Add(head); while (frontLine.Count > 0) { MapTreeKeyValuePair mapTree = frontLine.Dequeue(); foreach (MapNode neighbor in mapTree.MapNode.GetNeighbors()) { if(!visited.Contains(neighbor)) { TreeNode childTreeNode = new TreeNode(mapTree.TreeNode, neighbor.Color); //Claim this map node as your child mapTree.TreeNode.AddChildern(childTreeNode); //mark map node as visited, no one can claim this map node again visited.Add(neighbor); //queue it up to find it's children frontLine.Enqueue(new MapTreeKeyValuePair { MapNode = neighbor, TreeNode = childTreeNode }); } } } return root; }
public TreeNode Clone() { TreeNode clone = new TreeNode(Parent, Color); foreach(TreeNode child in _children) { clone.AddChildern(child.Clone()); } return clone; }
private int GetDepth(TreeNode node) { int depth = 0; TreeNode current = node; while (current.Parent != null) { depth++; current = current.Parent; } return depth; }
public void AddChildern(TreeNode node) { _children.Add(node); }
public TreeNode(TreeNode parent, Color color) { Color = color; Parent = parent; }