@@ -8,17 +8,13 @@ import (
8
8
// Fulfil Item interface specified in bst.go
9
9
type MyInt int
10
10
11
- func (a MyInt ) Less (than Item ) bool {
12
- return a < than .(MyInt )
13
- }
14
- func (a MyInt ) Greater (than Item ) bool {
15
- return a > than .(MyInt )
16
- }
17
- func (a MyInt ) Equals (to Item ) bool {
18
- return a == to .(MyInt )
19
- }
11
+ func (a MyInt ) Less (than Item ) bool { return a < than .(MyInt ) }
12
+ func (a MyInt ) Greater (than Item ) bool { return a > than .(MyInt ) }
13
+ func (a MyInt ) Equals (to Item ) bool { return a == to .(MyInt ) }
20
14
21
- func insertBulk (n * Node , values []MyInt ) (int , error ) { // todo Remove?
15
+ // insertBulk is a utility function to add a slice of Items, in this case MyInt's.
16
+ // Used heavily in bst_test.go
17
+ func insertBulk (n * Node , values []MyInt ) (int , error ) {
22
18
var numInserted int
23
19
for _ , v := range values {
24
20
err := n .Insert (v )
@@ -35,33 +31,28 @@ func insertBulk(n *Node, values []MyInt) (int, error) { // todo Remove?
35
31
func main () {
36
32
var list []MyInt
37
33
list = []MyInt {1 , 2 , 0 , - 1 , 0 , 0 , 0 , 0 , 0 , 1000 , 9999 }
38
-
39
34
root := & Node {}
40
-
41
- //var cnt int
42
- //for _, v := range list {
43
- // err := root.Insert(v)
44
- // if err != nil {
45
- // fmt.Println(err)
46
- // } else {
47
- // cnt++
48
- // }
49
- //}
35
+ // Insert all the elements in list
50
36
cnt , err := insertBulk (root , list )
51
- _ = err
52
- fmt .Println ("added" , cnt , "values to" , root )
37
+ if err != nil {
38
+ fmt .Println ("Failed to insert" , len (list )- cnt , "Items." )
39
+ }
40
+ fmt .Println ("Added" , cnt , "values to" , root )
53
41
fmt .Println ("Height" , root .Height ())
42
+ // Get max value
54
43
if l , err := root .Max (); err != nil {
55
44
fmt .Println (err )
56
45
} else {
57
- fmt .Println ("Max" , l )
46
+ fmt .Println ("Max is " , l )
58
47
}
48
+ // Get min value
59
49
if s , err := root .Min (); err != nil {
60
50
fmt .Println (err )
61
51
} else {
62
- fmt .Println ("Min" , s )
52
+ fmt .Println ("Min is " , s )
63
53
}
64
- fmt .Println ("Count" , root .Count ())
65
- fmt .Println ("InOrder" , root .InOrder ())
66
-
54
+ // Get number of elements in the BST
55
+ fmt .Println ("Count is" , root .Count ())
56
+ // Get elements according to in-order traversal
57
+ fmt .Println ("InOrder traversal yields" , root .InOrder ())
67
58
}
0 commit comments