|
| 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