| 12 | } |
| 13 | |
| 14 | func (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 |