The following is a collection of helper functions for BinarySearch, AVL and RB.
(root, nilNode Node[T])
| 22 | // The following is a collection of helper functions for BinarySearch, AVL and RB. |
| 23 | |
| 24 | func accessNodeByLayerHelper[T constraints.Ordered](root, nilNode Node[T]) [][]T { |
| 25 | if root == nilNode { |
| 26 | return [][]T{} |
| 27 | } |
| 28 | var q []Node[T] |
| 29 | var n Node[T] |
| 30 | var idx = 0 |
| 31 | q = append(q, root) |
| 32 | var res [][]T |
| 33 | |
| 34 | for len(q) != 0 { |
| 35 | res = append(res, []T{}) |
| 36 | qLen := len(q) |
| 37 | for i := 0; i < qLen; i++ { |
| 38 | n, q = q[0], q[1:] |
| 39 | res[idx] = append(res[idx], n.Key()) |
| 40 | if n.Left() != nilNode { |
| 41 | q = append(q, n.Left()) |
| 42 | } |
| 43 | if n.Right() != nilNode { |
| 44 | q = append(q, n.Right()) |
| 45 | } |
| 46 | } |
| 47 | idx++ |
| 48 | } |
| 49 | return res |
| 50 | } |
| 51 | |
| 52 | func searchTreeHelper[T constraints.Ordered](node, nilNode Node[T], key T) (Node[T], bool) { |
| 53 | if node == nilNode { |