-
Notifications
You must be signed in to change notification settings - Fork 1
/
7.3.go
56 lines (48 loc) · 888 Bytes
/
7.3.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
package main
import "strconv"
type tree struct {
value int
left, right *tree
}
func Sort(values []int) {
var root *tree
for _, v := range values {
root = add(root, v)
}
appendValues(values[:0], root)
}
func appendValues(values []int, t *tree) []int {
if t != nil {
values = appendValues(values, t.left)
values = append(values, t.value)
values = appendValues(values, t.right)
}
return values
}
func add(t *tree, value int) *tree {
if t == nil {
t = new(tree)
t.value = value
return t
}
if value < t.value {
t.left = add(t.left, value)
} else {
t.right = add(t.right, value)
}
return t
}
func (t *tree) String() string {
if t == nil {
return ""
}
left := t.left.String()
right := t.right.String()
if left != "" {
left += " "
}
if right != "" {
right = " " + right
}
return left + strconv.FormatInt(int64(t.value), 10) + right
}