forked from ancientlore/go-avltree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
54 lines (45 loc) · 957 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
48
49
50
51
52
53
54
package avltree
// Balance factor
const (
leftHigh = iota
equal
rightHigh
)
// treeNode is a node in the tree
type treeNode struct {
// left and right nodes
left, right *treeNode
// The contents of this node
value interface{}
// The balance factor of this node
bal byte
// The number of nodes in the subtree
size int
}
// Init initializes a node with the given value
func (n *treeNode) init(val interface{}) *treeNode {
n.left = nil
n.right = nil
n.bal = equal
n.size = 1
n.value = val
return n
}
// newNode returns an initialized treeNode
func newNode(val interface{}) *treeNode { return new(treeNode).init(val) }
// leftSize returns the size of the left subtree
// of the node
func (n *treeNode) leftSize() int {
if n.left != nil {
return n.left.size
}
return 0
}
// rightSize returns the size of the right subtree
// of the node
func (n treeNode) rightSize() int {
if n.right != nil {
return n.right.size
}
return 0
}