add benchmark, printer

This commit is contained in:
David 2021-07-25 19:52:31 -04:00
parent 7aece75825
commit 508460da67
2 changed files with 29 additions and 0 deletions

View File

@ -1,8 +1,14 @@
package main package main
import (
"fmt"
"io"
)
type TreeNode interface { type TreeNode interface {
Left() TreeNode Left() TreeNode
Right() TreeNode Right() TreeNode
Print(io.Writer)
} }
type BTreeNode struct { type BTreeNode struct {
@ -31,6 +37,16 @@ func (sbt *StringyBTreeNode) Right() TreeNode {
return sbt.right return sbt.right
} }
func (sbt *StringyBTreeNode) Print(w io.Writer) {
fmt.Printf("%+v\n", sbt)
if sbt.left != nil {
sbt.left.Print(w)
}
if sbt.right != nil {
sbt.right.Print(w)
}
}
func (sbt *StringyBTreeNode) Insert(k string, v interface{}) *StringyBTreeNode { func (sbt *StringyBTreeNode) Insert(k string, v interface{}) *StringyBTreeNode {
if sbt.k == k { if sbt.k == k {
sbt.v = v // set new value sbt.v = v // set new value

View File

@ -22,4 +22,17 @@ func TestBuildingTree(t *testing.T) {
if tree.Get("bar") != 5 { if tree.Get("bar") != 5 {
t.Fail() t.Fail()
} }
if tree.Get("zot") != nil {
t.Fail()
}
}
var r TreeNode
func BenchmarkInserts(b *testing.B) {
var result TreeNode
for n := 0; n < b.N; n++ {
NewStringyBTree("foo", 1).Insert("bar", 2).Insert("baz", 3).Insert("quuz", 4)
}
r = result
} }