Note: it does not appear that there is a good recursive breadth-first-search strategy: https://herringtondarkholme.github.io/2014/02/17/generator/ https://stackoverflow.com/questions/2549541/performing-breadth-first-search-recursively/2549825#2549825 WalkDownBreadth calls the given function on the n
(fun func(n Node) bool)
| 618 | // function returns [Break] and keeps walking if it returns [Continue]. It is |
| 619 | // non-recursive, but not safe for concurrent calling. |
| 620 | func (n *NodeBase) WalkDownBreadth(fun func(n Node) bool) { |
| 621 | start := n.This |
| 622 | |
| 623 | level := 0 |
| 624 | start.AsTree().depth = level |
| 625 | queue := make([]Node, 1) |
| 626 | queue[0] = start |
| 627 | |
| 628 | for { |
| 629 | if len(queue) == 0 { |
| 630 | break |
| 631 | } |
| 632 | cur := queue[0] |
| 633 | depth := cur.AsTree().depth |
| 634 | queue = queue[1:] |
| 635 | |
| 636 | if cur.AsTree().This != nil && fun(cur) { // false return means don't proceed |
| 637 | for _, cn := range cur.AsTree().Children { |
| 638 | if cn != nil && cn.AsTree().This != nil { |
| 639 | cn.AsTree().depth = depth + 1 |
| 640 | queue = append(queue, cn) |
| 641 | } |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | // Deep Copy: |
| 648 |