Skip to content

Commit fc6fa88

Browse files
committed
Make node more extensible
1 parent a4eb6d3 commit fc6fa88

File tree

5 files changed

+279
-176
lines changed

5 files changed

+279
-176
lines changed

cmd/main.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@ import (
1010
func main() {
1111
tr := tree.New[string]()
1212

13-
tr.AddRoot(tree.NewNode(0, "0.0"))
13+
tr.AddRoot(tree.NewNodeWithID(0, "0.0"))
1414

15-
tr.Add(0, tree.NewNode(1, "0.1"))
16-
tr.Add(0, tree.NewNode(2, "0.2"))
15+
tr.Add(0, tree.NewNodeWithID(1, "0.1"))
16+
tr.Add(0, tree.NewNodeWithID(2, "0.2"))
1717

18-
tr.Add(1, tree.NewNode(3, "1.3"))
19-
tr.Add(1, tree.NewNode(4, "1.4"))
18+
tr.Add(1, tree.NewNodeWithID(3, "1.3"))
19+
tr.Add(1, tree.NewNodeWithID(4, "1.4"))
2020

21-
tr.Add(2, tree.NewNode(5, "2.5"))
22-
tr.Add(2, tree.NewNode(6, "2.6"))
21+
tr.Add(2, tree.NewNodeWithID(5, "2.5"))
22+
tr.Add(2, tree.NewNodeWithID(6, "2.6"))
2323

2424
root, ok := tr.GetRoot()
25-
fmt.Println(ok) // true
26-
fmt.Println(root.Get()) // 0, 0.0
25+
fmt.Println(ok) // true
26+
fmt.Println(root.GetData()) // 0.0
2727

2828
node, ok := tr.Get(3)
29-
fmt.Println(ok) // true
30-
fmt.Println(node.Get()) // 3, 1.3
29+
fmt.Println(ok) // true
30+
fmt.Println(node.GetData()) // 1.3
3131

3232
structure, ok := tr.GetStructure()
3333
fmt.Println(ok) // true
@@ -36,6 +36,6 @@ func main() {
3636
nodes, ok := tr.Backtrack(6)
3737
fmt.Println(ok) // true
3838
for _, node := range nodes {
39-
fmt.Println(node.Get()) // 6, 2.6; 2, 0.2; 0, 0.0
39+
fmt.Println(node.GetData()) // 2.6; 0.2; 0.0
4040
}
4141
}

tree/example_test.go

Lines changed: 59 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func ExampleNew() {
1111
tr := tree.New[string]()
1212

1313
// Add nodes do tree
14-
tr.AddRoot(tree.NewNode(0, "root"))
14+
tr.AddRoot(tree.NewNodeWithID(0, "root"))
1515

1616
// Do more things
1717
}
@@ -20,63 +20,63 @@ func ExampleNew() {
2020
func ExampleTree_AddRoot() {
2121
tr := tree.New[int]()
2222

23-
tr.AddRoot(tree.NewNode(0, 42))
23+
tr.AddRoot(tree.NewNodeWithID(0, 42))
2424

2525
// Do more things
2626
}
2727

2828
// ExampleTree_GetRoot demonstrates how to retrieve root node from tree.
2929
func ExampleTree_GetRoot() {
3030
tr := tree.New[float64]()
31-
tr.AddRoot(tree.NewNode(0, 3.14))
31+
tr.AddRoot(tree.NewNodeWithID(0, 3.14))
3232

3333
node, ok := tr.GetRoot()
3434
if !ok {
3535
return
3636
}
37-
fmt.Println(node.Get())
37+
fmt.Println(node.GetData())
3838

3939
// Do more things
4040
}
4141

4242
// ExampleTree_Add demonstrates how to add node to tree.
4343
func ExampleTree_Add() {
4444
tr := tree.New[bool]()
45-
tr.AddRoot(tree.NewNode(0, true))
45+
tr.AddRoot(tree.NewNodeWithID(0, true))
4646

47-
tr.Add(0, tree.NewNode(1, false))
47+
tr.Add(0, tree.NewNodeWithID(1, false))
4848

4949
// Do more things
5050
}
5151

5252
// ExampleTree_Get demonstrates how to retrieve node from tree.
5353
func ExampleTree_Get() {
5454
tr := tree.New[uint]()
55-
tr.AddRoot(tree.NewNode(0, uint(42)))
55+
tr.AddRoot(tree.NewNodeWithID(0, uint(42)))
5656

5757
node, ok := tr.Get(0)
5858
if !ok {
5959
return
6060
}
61-
fmt.Println(node.Get())
61+
fmt.Println(node.GetData())
6262

6363
// Do more things
6464
}
6565

6666
// ExampleTree_Backtrack demonstrates how to retrieve path of nodes from node to root.
6767
func ExampleTree_Backtrack() {
6868
tr := tree.New[string]()
69-
tr.AddRoot(tree.NewNode(0, "root"))
70-
tr.Add(0, tree.NewNode(1, "level1"))
71-
tr.Add(1, tree.NewNode(2, "level2"))
72-
tr.Add(2, tree.NewNode(3, "leaf"))
69+
tr.AddRoot(tree.NewNodeWithID(0, "root"))
70+
tr.Add(0, tree.NewNodeWithID(1, "level1"))
71+
tr.Add(1, tree.NewNodeWithID(2, "level2"))
72+
tr.Add(2, tree.NewNodeWithID(3, "leaf"))
7373

7474
nodes, ok := tr.Backtrack(3)
7575
if !ok {
7676
return
7777
}
7878
for _, node := range nodes {
79-
fmt.Println(node.Get())
79+
fmt.Println(node.GetData())
8080
}
8181

8282
// Do more things
@@ -85,10 +85,10 @@ func ExampleTree_Backtrack() {
8585
// ExampleTree_GetStructure demonstrates how to retrieve tree structure.
8686
func ExampleTree_GetStructure() {
8787
tr := tree.New[string]()
88-
tr.AddRoot(tree.NewNode(0, "root"))
89-
tr.Add(0, tree.NewNode(1, "level1"))
90-
tr.Add(1, tree.NewNode(2, "level2"))
91-
tr.Add(2, tree.NewNode(3, "leaf"))
88+
tr.AddRoot(tree.NewNodeWithID(0, "root"))
89+
tr.Add(0, tree.NewNodeWithID(1, "level1"))
90+
tr.Add(1, tree.NewNodeWithID(2, "level2"))
91+
tr.Add(2, tree.NewNodeWithID(3, "leaf"))
9292

9393
structure, ok := tr.GetStructure()
9494
if !ok {
@@ -101,30 +101,48 @@ func ExampleTree_GetStructure() {
101101
// Do more things
102102
}
103103

104+
// NewNodeWithID demonstrates how to create a node with ID.
105+
func ExampleNewNodeWithID() {
106+
n := tree.NewNodeWithID(0, "node")
107+
108+
n.GetData()
109+
110+
// Do more things
111+
}
112+
104113
// ExampleNewNode demonstrates how to create a node.
105114
func ExampleNewNode() {
106-
n := tree.NewNode(0, "node")
115+
n := tree.NewNode("node")
107116

108-
n.Get()
117+
n.GetData()
109118

110119
// Do more things
111120
}
112121

113-
// ExampleNode_Get demonstrates how to retrieve id and data from node.
114-
func ExampleNode_Get() {
115-
n := tree.NewNode(0, 3.14)
122+
// ExampleNode_GetData demonstrates how to retrieve data from node.
123+
func ExampleNode_GetData() {
124+
n := tree.NewNodeWithID(0, 3.14)
116125

117-
id, data := n.Get()
118-
fmt.Println(id)
126+
data := n.GetData()
119127
fmt.Println(data)
120128

121129
// Do more things
122130
}
123131

132+
// ExampleNode_GetID demonstrates how to retrieve id from node.
133+
func ExampleNode_GetID() {
134+
n := tree.NewNodeWithID(0, 3.14)
135+
136+
id := n.GetID()
137+
fmt.Println(id)
138+
139+
// Do more things
140+
}
141+
124142
// ExampleNode_GetNexts demonstrates how to retrieve next nodes from node.
125143
func ExampleNode_GetNexts() {
126-
root := tree.NewNode(0, "root")
127-
leaf := tree.NewNode(1, "leaf")
144+
root := tree.NewNodeWithID(0, "root")
145+
leaf := tree.NewNodeWithID(1, "leaf")
128146

129147
root.AddNext(leaf)
130148
nexts := root.GetNexts()
@@ -135,19 +153,19 @@ func ExampleNode_GetNexts() {
135153

136154
// ExampleNode_GetPrevious demonstrates how to retrieve next nodes from node.
137155
func ExampleNode_GetPrevious() {
138-
root := tree.NewNode(0, "root")
139-
leaf := tree.NewNode(1, "leaf")
156+
root := tree.NewNodeWithID(0, "root")
157+
leaf := tree.NewNodeWithID(1, "leaf")
140158

141159
root.AddNext(leaf)
142160
previous := leaf.GetPrevious()
143-
fmt.Println(previous.Get())
161+
fmt.Println(previous.GetData())
144162

145163
// Do more things
146164
}
147165

148166
// ExampleNode_IsRoot demonstrates how to retrieve info if node is root.
149167
func ExampleNode_IsRoot() {
150-
n := tree.NewNode(0, 'b')
168+
n := tree.NewNodeWithID(0, 'b')
151169

152170
root := n.IsRoot()
153171
fmt.Println(root)
@@ -157,8 +175,8 @@ func ExampleNode_IsRoot() {
157175

158176
// ExampleNode_IsLeaf demonstrates how to retrieve info if node is leaf.
159177
func ExampleNode_IsLeaf() {
160-
n1 := tree.NewNode(0, 'a')
161-
n2 := tree.NewNode(0, 'b')
178+
n1 := tree.NewNodeWithID(0, 'a')
179+
n2 := tree.NewNodeWithID(0, 'b')
162180

163181
n1.AddNext(n2)
164182

@@ -170,26 +188,26 @@ func ExampleNode_IsLeaf() {
170188

171189
// ExampleNode_Backtrack demonstrates how to retrieve the path between node to root.
172190
func ExampleNode_Backtrack() {
173-
n1 := tree.NewNode(0, 'a')
174-
n2 := tree.NewNode(0, 'b')
175-
n3 := tree.NewNode(0, 'c')
191+
n1 := tree.NewNodeWithID(0, 'a')
192+
n2 := tree.NewNodeWithID(0, 'b')
193+
n3 := tree.NewNodeWithID(0, 'c')
176194

177195
n1.AddNext(n2)
178196
n2.AddNext(n3)
179197

180198
nodes := n3.Backtrack()
181199
for _, node := range nodes {
182-
fmt.Println(node.Get())
200+
fmt.Println(node.GetData())
183201
}
184202

185203
// Do more things
186204
}
187205

188206
// ExampleNode_GetStructure demonstrates how to retrieve the tree structure from node.
189207
func ExampleNode_GetStructure() {
190-
n1 := tree.NewNode(0, 'a')
191-
n2 := tree.NewNode(0, 'b')
192-
n3 := tree.NewNode(0, 'c')
208+
n1 := tree.NewNodeWithID(0, 'a')
209+
n2 := tree.NewNodeWithID(0, 'b')
210+
n3 := tree.NewNodeWithID(0, 'c')
193211

194212
n1.AddNext(n2)
195213
n2.AddNext(n3)
@@ -204,8 +222,8 @@ func ExampleNode_GetStructure() {
204222

205223
// ExampleNode_AddNext demonstrates how to add a node to a parent.
206224
func ExampleNode_AddNext() {
207-
n1 := tree.NewNode(0, 'a')
208-
n2 := tree.NewNode(0, 'b')
225+
n1 := tree.NewNodeWithID(0, 'a')
226+
n2 := tree.NewNodeWithID(0, 'b')
209227

210228
n1.AddNext(n2)
211229

tree/node.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,28 @@ type Node[T any] struct {
1414
}
1515

1616
// NewNode creates a new node.
17-
func NewNode[T any](id int, data T) *Node[T] {
17+
func NewNode[T any](data T) *Node[T] {
18+
return &Node[T]{
19+
data: data,
20+
}
21+
}
22+
23+
// NewNodeWithID creates a new node with ID.
24+
func NewNodeWithID[T any](id int, data T) *Node[T] {
1825
return &Node[T]{
1926
id: id,
2027
data: data,
2128
}
2229
}
2330

24-
// Get retrieves id and data from node.
25-
func (n *Node[T]) Get() (int, T) {
26-
return n.id, n.data
31+
// GetData retrieves data from node.
32+
func (n *Node[T]) GetData() T {
33+
return n.data
34+
}
35+
36+
// GetID retrieves id from node.
37+
func (n *Node[T]) GetID() int {
38+
return n.id
2739
}
2840

2941
// GetPrevious retrieves the next nodes.

0 commit comments

Comments
 (0)