public void CanDeleteNodes() { _root.FirstOrDefaultDescendant(n => n.Id == 6).IsExplicitlyIncluded = true; foreach (var i in new []{0, 3, 8, 9, 10}) { var capturedId = i; _root.FirstOrDefaultDescendant(n => n.Id == capturedId).IsImplicitlyIncluded = true; } _root = Node.BreadthFirstDeletion(_root, new HashSet<int>{0,3,6,8,9,10}, new []{6}); var removedNodes = new[] { 1, 2, 4, 5, 7 }; foreach (var id in removedNodes) { var capturedId = id; _root.FirstOrDefaultDescendant(n => n.Id == capturedId).Should().BeNull(); } var keptNodes = new[] { 0, 3, 6, 8, 9, 10 }; foreach (var id in keptNodes) { var capturedId = id; var match = _root.FirstOrDefaultDescendant(n => n.Id == capturedId); match.Should().NotBeNull(); match.IsExplicitlyIncluded.Should().Be(match.Id == 6); } }
private static void ProcessParents(Node template, Node clone, Node root) { var stack = new Stack<Node>(); foreach (var parent in template.Parents) { stack.Push(parent); } while (stack.Count != 0) { var current = stack.Pop(); Node targetParent; if (root == null) { targetParent = new Node {Id = current.Id}; } else { targetParent = root.FirstOrDefaultDescendant(n => n.Id == current.Id) ?? new Node { Id = current.Id }; } clone.Parents.Add(targetParent); targetParent.Children.Add(clone); ProcessParents(current, targetParent, root); } }