Skip to content

Commit 99bce9d

Browse files
authored
Improve examples (#6)
1 parent e09b1aa commit 99bce9d

File tree

4 files changed

+196
-10
lines changed

4 files changed

+196
-10
lines changed

README.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
[![Go Report Card](https://goreportcard.com/badge/github.com/johnfercher/tree)](https://goreportcard.com/report/github.com/johnfercher/tree)
55
[![CI](https://github.com/johnfercher/tree/actions/workflows/goci.yml/badge.svg)](https://github.com/johnfercher/tree/actions/workflows/goci.yml)
66
[![Lint](https://github.com/johnfercher/tree/actions/workflows/golangci-lint.yml/badge.svg)](https://github.com/johnfercher/tree/actions/workflows/golangci-lint.yml)
7-
[![Codecov](https://img.shields.io/codecov/c/github/johnfercher/tree)](https://codecov.io/gh/johnfercher/tree)
8-
7+
[![Codecov](https://codecov.io/gh/johnfercher/tree/branch/main/graph/badge.svg)](https://codecov.io/gh/johnfercher/tree)
98

109
A generic unbalanced tree implementation, where you can define which node will be added to each node.
1110

@@ -35,11 +34,11 @@ func main() {
3534

3635
root, ok := tr.GetRoot()
3736
fmt.Println(ok) // true
38-
fmt.Println(root.Get()) // 0.0
37+
fmt.Println(root.Get()) // 0, 0.0
3938

4039
node, ok := tr.Get(3)
4140
fmt.Println(ok) // true
42-
fmt.Println(node.Get()) // 1.3
41+
fmt.Println(node.Get()) // 3, 1.3
4342

4443
structure, ok := tr.GetStructure()
4544
fmt.Println(ok) // true
@@ -48,8 +47,7 @@ func main() {
4847
nodes, ok := tr.Backtrack(6)
4948
fmt.Println(ok) // true
5049
for _, node := range nodes {
51-
fmt.Println(node.Get()) // 2.6, 0.2, 0.0
50+
fmt.Println(node.Get()) // 6, 2.6; 2, 0.2; 0, 0.0
5251
}
5352
}
54-
5553
```

cmd/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ func main() {
2323

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

2828
node, ok := tr.Get(3)
2929
fmt.Println(ok) // true
30-
fmt.Println(node.Get()) // 1.3
30+
fmt.Println(node.Get()) // 3, 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()) // 2.6, 0.2, 0.0
39+
fmt.Println(node.Get()) // 6, 2.6; 2, 0.2; 0, 0.0
4040
}
4141
}

pkg/example_test.go

-1
This file was deleted.

pkg/tree/example_test.go

+189
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
package tree_test
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/johnfercher/tree/pkg/tree"
7+
)
8+
9+
// ExampleNew demonstrates how to create tree.
10+
func ExampleNew() {
11+
tr := tree.New[string]()
12+
13+
// Add nodes do tree
14+
tr.AddRoot(tree.NewNode(0, "root"))
15+
16+
// Do more things
17+
}
18+
19+
// ExampleTree_AddRoot demonstrates how to add root node to tree.
20+
func ExampleTree_AddRoot() {
21+
tr := tree.New[int]()
22+
23+
tr.AddRoot(tree.NewNode(0, 42))
24+
25+
// Do more things
26+
}
27+
28+
// ExampleTree_GetRoot demonstrates how to retrieve root node from tree.
29+
func ExampleTree_GetRoot() {
30+
tr := tree.New[float64]()
31+
tr.AddRoot(tree.NewNode(0, 3.14))
32+
33+
node, ok := tr.GetRoot()
34+
if !ok {
35+
return
36+
}
37+
fmt.Println(node.Get())
38+
39+
// Do more things
40+
}
41+
42+
// ExampleTree_Add demonstrates how to add node to tree.
43+
func ExampleTree_Add() {
44+
tr := tree.New[bool]()
45+
tr.AddRoot(tree.NewNode(0, true))
46+
47+
tr.Add(0, tree.NewNode(1, false))
48+
49+
// Do more things
50+
}
51+
52+
// ExampleTree_Get demonstrates how to retrieve node from tree.
53+
func ExampleTree_Get() {
54+
tr := tree.New[uint]()
55+
tr.AddRoot(tree.NewNode(0, uint(42)))
56+
57+
node, ok := tr.Get(0)
58+
if !ok {
59+
return
60+
}
61+
fmt.Println(node.Get())
62+
63+
// Do more things
64+
}
65+
66+
// ExampleTree_Backtrack demonstrates how to retrieve path of nodes from node to root.
67+
func ExampleTree_Backtrack() {
68+
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"))
73+
74+
nodes, ok := tr.Backtrack(3)
75+
if !ok {
76+
return
77+
}
78+
for _, node := range nodes {
79+
fmt.Println(node.Get())
80+
}
81+
82+
// Do more things
83+
}
84+
85+
// ExampleTree_GetStructure demonstrates how to retrieve tree structure.
86+
func ExampleTree_GetStructure() {
87+
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"))
92+
93+
structure, ok := tr.GetStructure()
94+
if !ok {
95+
return
96+
}
97+
for _, str := range structure {
98+
fmt.Println(str)
99+
}
100+
101+
// Do more things
102+
}
103+
104+
// ExampleNewNode demonstrates how to create a node.
105+
func ExampleNewNode() {
106+
n := tree.NewNode(0, "node")
107+
108+
n.Get()
109+
110+
// Do more things
111+
}
112+
113+
// ExampleNode_Get demonstrates how to retrieve id and data from node.
114+
func ExampleNode_Get() {
115+
n := tree.NewNode(0, 3.14)
116+
117+
id, data := n.Get()
118+
fmt.Println(id)
119+
fmt.Println(data)
120+
121+
// Do more things
122+
}
123+
124+
// ExampleNode_IsRoot demonstrates how to retrieve info if node is root.
125+
func ExampleNode_IsRoot() {
126+
n := tree.NewNode(0, 'b')
127+
128+
root := n.IsRoot()
129+
fmt.Println(root)
130+
131+
// Do more things
132+
}
133+
134+
// ExampleNode_IsLeaf demonstrates how to retrieve info if node is leaf.
135+
func ExampleNode_IsLeaf() {
136+
n1 := tree.NewNode(0, 'a')
137+
n2 := tree.NewNode(0, 'b')
138+
139+
n1.AddNext(n2)
140+
141+
leaf := n2.IsLeaf()
142+
fmt.Println(leaf)
143+
144+
// Do more things
145+
}
146+
147+
// ExampleNode_Backtrack demonstrates how to retrieve the path between node to root.
148+
func ExampleNode_Backtrack() {
149+
n1 := tree.NewNode(0, 'a')
150+
n2 := tree.NewNode(0, 'b')
151+
n3 := tree.NewNode(0, 'c')
152+
153+
n1.AddNext(n2)
154+
n2.AddNext(n3)
155+
156+
nodes := n3.Backtrack()
157+
for _, node := range nodes {
158+
fmt.Println(node.Get())
159+
}
160+
161+
// Do more things
162+
}
163+
164+
// ExampleNode_GetStructure demonstrates how to retrieve the tree structure from node.
165+
func ExampleNode_GetStructure() {
166+
n1 := tree.NewNode(0, 'a')
167+
n2 := tree.NewNode(0, 'b')
168+
n3 := tree.NewNode(0, 'c')
169+
170+
n1.AddNext(n2)
171+
n2.AddNext(n3)
172+
173+
structure := n3.GetStructure()
174+
for _, str := range structure {
175+
fmt.Println(str)
176+
}
177+
178+
// Do more things
179+
}
180+
181+
// ExampleNode_AddNext demonstrates how to add a node to a parent.
182+
func ExampleNode_AddNext() {
183+
n1 := tree.NewNode(0, 'a')
184+
n2 := tree.NewNode(0, 'b')
185+
186+
n1.AddNext(n2)
187+
188+
// Do more things
189+
}

0 commit comments

Comments
 (0)