private static TreeZipper FindNode(string value, TreeZipper zipper) { if (zipper == null || zipper.Focus.Value.CompareTo(value) == 0) { return(zipper); } return(FindNode(value, GoDown(zipper)) ?? FindNode(value, GoRight(zipper))); }
private static TreeZipper GoDown(TreeZipper zipper) { if (zipper == null || !zipper.Focus.Children.Any()) { return(null); } var focus = zipper.Focus; var children = focus.Children; var newCrumb = new TreeCrumb(focus.Value, Array.Empty <Tree>(), children.Skip(1).ToArray()); return(new TreeZipper(children.First(), new[] { newCrumb }.Concat(zipper.Crumbs))); }
private static TreeZipper GoRight(TreeZipper zipper) { if (zipper == null || !zipper.Crumbs.Any() || !zipper.Crumbs.First().Right.Any()) { return(null); } var crumbs = zipper.Crumbs; var firstCrumb = crumbs.First(); var newCrumb = new TreeCrumb(firstCrumb.Value, firstCrumb.Left.Concat(new[] { zipper.Focus }).ToArray(), firstCrumb.Right.Skip(1).ToArray()); return(new TreeZipper(firstCrumb.Right.First(), new[] { newCrumb }.Concat(crumbs.Skip(1)))); }
private static Tree ChangeParent(TreeZipper zipper) { if (zipper == null) { return(null); } if (!zipper.Crumbs.Any()) { return(zipper.Focus); } var firstCrumb = zipper.Crumbs.First(); var focus = zipper.Focus; var newZipper = new TreeZipper(new Tree(firstCrumb.Value, firstCrumb.Left.Concat(firstCrumb.Right).ToArray()), zipper.Crumbs.Skip(1)); var parentGraph = ChangeParent(newZipper); var ys = focus.Children.Concat(new[] { parentGraph }).ToArray(); return(new Tree(focus.Value, ys)); }
private static IEnumerable <string> ZipperToPath(TreeZipper zipper) { return(zipper?.Crumbs.Select(c => c.Value).Reverse().Concat(new[] { zipper.Focus.Value })); }