public (List <Snapable>, List <Snapable>) Split(Snapable a, Snapable b) { Node nodeA = findInForest(a); Node nodeB = findInForest(b); Node parent; Node child; if (nodeA.GetParent() == nodeB) { parent = nodeB; child = nodeA; } else if (nodeB.GetParent() == nodeA) { parent = nodeA; child = nodeB; } else { throw new Exception("Not joined, split must fail!"); } parent.RemoveChild(child); return(FlattenFromAnywhere(parent), FlattenFromRoot(child)); }
private Node findInForest(Snapable needle) { foreach (var root in roots) { Node search = findInTree(needle, root); if (search != null) { return(search); } } return(null); }
public void Join(Snapable a, Snapable b) { Node possible_nodeA = findInForest(a); if (possible_nodeA == null) { roots.Add(new Node(a)); } Node possible_nodeB = findInForest(b); if (possible_nodeB == null) { roots.Add(new Node(b)); } Node nodeA = (Node)findInForest(a); Node nodeB = (Node)findInForest(b); roots.Remove(nodeB); nodeA.AddChild(nodeB); }
private Node findInTree(Snapable needle, Node node) { if (node == null) { return(null); } if (node.GetVal() == needle) { return(node); } foreach (Node child in node.GetChildren()) { Node search = findInTree(needle, child); if (search != null) { return(search); } } return(null); }
public Node(Snapable val) { this.val = val; }