MCPcopy Index your code
hub / github.com/TheAlgorithms/Go / BFSInsert

Method BFSInsert

project_euler/problem_18/tree.go:14–47  ·  view source on GitHub ↗
(value NodeValue)

Source from the content-addressed store, hash-verified

12}
13
14func (t *Tree) BFSInsert(value NodeValue) {
15 t.Nodes = make(map[int]struct{}) // Reset the nodes map
16
17 if t.Root == nil {
18 t.Root = &Root{NodeValue: value, ID: 0}
19 t.ID = 1
20 return
21 }
22
23 queue := make([]Node, 0)
24 queue = append(queue, t.Root)
25 t.isInQueue(t.Root.GetID())
26
27 head := 0
28
29 for head < len(queue) {
30 current := queue[head]
31 head++
32 childNode := current.CreateChild(value, t.ID)
33
34 if current.HasSpace() {
35 current.Insert(childNode)
36 t.ID++
37 return
38 }
39
40 if !t.isInQueue(current.Left().GetID()) { // Avoid duplicates
41 queue = append(queue, current.Left())
42 }
43 if !t.isInQueue(current.Right().GetID()) {
44 queue = append(queue, current.Right())
45 }
46 }
47}
48
49// MaxPathValueSearch is a method that searches the maximum path value in a tree
50// given a certain depth

Callers 1

Problem18Function · 0.95

Calls 7

isInQueueMethod · 0.95
GetIDMethod · 0.65
CreateChildMethod · 0.65
HasSpaceMethod · 0.65
InsertMethod · 0.65
LeftMethod · 0.65
RightMethod · 0.65

Tested by

no test coverage detected