-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
65 lines (56 loc) · 907 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
55
56
57
58
59
60
61
62
63
64
65
package bstree
type node struct {
key string
data interface{}
left *node
right *node
}
func (n *node) insert(newn *node) {
switch {
case n.key == newn.key:
*n = *newn
case n.key > newn.key:
if n.left == nil {
n.left = newn
} else {
n.left.insert(newn)
}
case n.key < newn.key:
if n.right == nil {
n.right = newn
} else {
n.right.insert(newn)
}
}
}
func (n *node) get(key string) interface{} {
if n == nil {
return nil
}
if n.key == key {
return n.data
}
if n.key > key {
return n.left.get(key)
}
if n.key < key {
return n.right.get(key)
}
return nil
}
func (n *node) ToSlice(datum *[]interface{}) {
if n == nil {
return
}
n.left.ToSlice(datum)
*datum = append(*datum, n.data)
n.right.ToSlice(datum)
}
func (n *node) Keys(keys *[]string) {
if n == nil {
return
}
n.left.Keys(keys)
*keys = append(*keys, n.key)
n.right.Keys(keys)
}