@@ -8,17 +8,13 @@ import (
88// Fulfil Item interface specified in bst.go
99type MyInt int
1010
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 ) }
2014
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 ) {
2218 var numInserted int
2319 for _ , v := range values {
2420 err := n .Insert (v )
@@ -35,33 +31,28 @@ func insertBulk(n *Node, values []MyInt) (int, error) { // todo Remove?
3531func main () {
3632 var list []MyInt
3733 list = []MyInt {1 , 2 , 0 , - 1 , 0 , 0 , 0 , 0 , 0 , 1000 , 9999 }
38-
3934 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
5036 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 )
5341 fmt .Println ("Height" , root .Height ())
42+ // Get max value
5443 if l , err := root .Max (); err != nil {
5544 fmt .Println (err )
5645 } else {
57- fmt .Println ("Max" , l )
46+ fmt .Println ("Max is " , l )
5847 }
48+ // Get min value
5949 if s , err := root .Min (); err != nil {
6050 fmt .Println (err )
6151 } else {
62- fmt .Println ("Min" , s )
52+ fmt .Println ("Min is " , s )
6353 }
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 ())
6758}
0 commit comments