MCPcopy
hub / github.com/TheAlgorithms/Go / accessNodeByLayerHelper

Function accessNodeByLayerHelper

structure/tree/tree.go:24–50  ·  view source on GitHub ↗

The following is a collection of helper functions for BinarySearch, AVL and RB.

(root, nilNode Node[T])

Source from the content-addressed store, hash-verified

22// The following is a collection of helper functions for BinarySearch, AVL and RB.
23
24func 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
52func searchTreeHelper[T constraints.Ordered](node, nilNode Node[T], key T) (Node[T], bool) {
53 if node == nilNode {

Callers

nothing calls this directly

Calls 3

KeyMethod · 0.65
LeftMethod · 0.65
RightMethod · 0.65

Tested by

no test coverage detected