-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (60 loc) · 1.53 KB
/
main.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 main
import (
"fmt"
"math/rand"
"sort"
"time"
avl_tree "github.com/yak2p/trees/avl-tree"
"github.com/yak2p/trees/binary-search-tree"
red_black_tree "github.com/yak2p/trees/red-black-tree"
)
type List struct {
list []int
}
func (l *List) Len() int {
return len(l.list)
}
func (l *List) Less(i, j int) bool {
return l.list[i] < l.list[j]
}
func (l *List) Swap(i, j int) {
tmp := l.list[i]
l.list[i] = l.list[j]
l.list[j] = tmp
}
func main() {
maxNum := 100000
list := rand.Perm(maxNum)
//list := make([]int, maxNum)
//for i := 0; i < maxNum; i++ {
// list[i] = i
//}
fmt.Printf("Insert %d nodes elapse:\n ", maxNum)
startTime := time.Now()
tree := &binary_search_tree.BinarySearchTree{}
for _, v := range list {
tree.Insert(v, 1)
}
fmt.Printf("\t BST Non-Recursive : \t\t%s\n", time.Now().Sub(startTime).String())
startTime = time.Now()
tree1 := &binary_search_tree.BinarySearchTree{}
for _, v := range list {
tree1.InsertRecursive(v, 1)
}
fmt.Printf("\t BST Recursive : \t\t%s\n", time.Now().Sub(startTime).String())
startTime = time.Now()
tree2 := &avl_tree.Tree{}
for _, v := range list {
tree2.Insert(v, 1)
}
fmt.Printf("\t AVL tree : \t\t\t%s\n", time.Now().Sub(startTime).String())
startTime = time.Now()
tree3 := &red_black_tree.Tree{}
for _, v := range list {
tree3.Insert(v, 1)
}
fmt.Printf("\t Red-Black tree : \t\t%s\n", time.Now().Sub(startTime).String())
startTime = time.Now()
sort.Sort(&List{list})
fmt.Printf("\t sort.Sort : \t\t\t%s\n", time.Now().Sub(startTime).String())
}