-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.go
More file actions
39 lines (30 loc) · 900 Bytes
/
graph.go
File metadata and controls
39 lines (30 loc) · 900 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package util
// The graph is a simple struct that manages connected nodes. There is no
// distinction made here on directed vs undirected (it's directed implicitly,
// though), link weights, or anything like that. This is as simple as it gets,
// and you can add on to it if you need/want to.
type GraphNode struct {
Value interface{}
Links [](*GraphNode)
currentChild int
}
func (this *GraphNode) LinkTo(other *GraphNode) {
this.Links = append(this.Links, other)
}
func (this *GraphNode) DoublyLinkTo(other *GraphNode) {
this.LinkTo(other)
other.LinkTo(this)
}
func (this *GraphNode) First() interface{} {
this.currentChild = 0
return this.Value
}
func (this *GraphNode) Next() interface{} {
if this.currentChild >= len(this.Links) {
return nil
}
thing := this.Links[this.currentChild]
this.currentChild += 1
return thing
}
// TODO: build a general BFS system