-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
47 lines (38 loc) · 884 Bytes
/
node.go
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
40
41
42
43
44
45
46
47
package dstruct
type Node[T any] struct {
data *T
parent *Node[T]
children map[string]*Node[T]
}
func (n *Node[T]) AddNode(name string, data *T) {
n.children[name] = &Node[T]{
data: data,
children: make(map[string]*Node[T]),
parent: n,
}
}
func (n *Node[T]) DeleteNode(name string) {
delete(n.children, name)
}
func (n *Node[T]) GetNode(name string) *Node[T] {
return n.children[name]
}
func (n *Node[T]) HasChildren() bool {
return len(n.children) > 0
}
func (n *Node[T]) HasChild(name string) bool {
return n.children[name] != nil
}
func (n *Node[T]) Copy() *Node[T] {
newNode := &Node[T]{
data: new(T),
children: make(map[string]*Node[T]),
}
dataCopy := *n.data
*newNode.data = dataCopy
for name := range n.children {
newNode.children[name] = n.children[name].Copy()
newNode.children[name].parent = newNode
}
return newNode
}