-
Notifications
You must be signed in to change notification settings - Fork 4
/
node.go
76 lines (65 loc) · 1.41 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package mbtree
import (
"fmt"
)
type Node struct {
// node id
Id int64
// node tag
Tag string
// parent id of the node
Pid int64
// child id list
ChildIds []int64
// node data
Data interface{}
}
const defaultRootId = 0
// NewNode new node
func NewNode(id int64, data interface{}) *Node {
return &Node{
Id: id,
Pid: 0,
ChildIds: make([]int64, 0),
Data: data,
Tag: buildNodeTag(id),
}
}
// NewRootNode new root node
func NewRootNode(data interface{}) *Node {
return NewNode(defaultRootId, data)
}
// IsLeaf return true if current node has no children
func (n *Node) IsLeaf() bool {
return len(n.ChildIds) == 0
}
// HasChildren return true if current node has children
func (n *Node) HasChildren() bool {
return !n.IsLeaf()
}
// updateChildren update the children list with different modes:
// addition (Node.ADD or Node.INSERT) and deletion (Node.DELETE).
func (n *Node) updateChildren(childId int64, action Action) {
// ignore invalid childId
if childId <= 0 {
return
}
switch action {
case ADD:
n.ChildIds = append(n.ChildIds, childId)
case DELETE:
for i, id := range n.ChildIds {
if id == childId {
n.ChildIds = append(n.ChildIds[:i], n.ChildIds[i+1:]...)
}
}
}
}
// updateParent Update the parent id
func (n *Node) updateParent(id int64) {
n.Pid = id
}
// buildNodeTag 构造默认的节点tag
func buildNodeTag(id int64) string {
return fmt.Sprintf("%d", id)
}