From 508460da67fa5bd3bd32893aa63d4e3e48bfe86a Mon Sep 17 00:00:00 2001 From: David Ashby Date: Sun, 25 Jul 2021 19:52:31 -0400 Subject: [PATCH] add benchmark, printer --- datastructures.go | 16 ++++++++++++++++ datastructures_test.go | 13 +++++++++++++ 2 files changed, 29 insertions(+) diff --git a/datastructures.go b/datastructures.go index f863519..d6fcb8b 100644 --- a/datastructures.go +++ b/datastructures.go @@ -1,8 +1,14 @@ package main +import ( + "fmt" + "io" +) + type TreeNode interface { Left() TreeNode Right() TreeNode + Print(io.Writer) } type BTreeNode struct { @@ -31,6 +37,16 @@ func (sbt *StringyBTreeNode) Right() TreeNode { 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 { if sbt.k == k { sbt.v = v // set new value diff --git a/datastructures_test.go b/datastructures_test.go index 7d25518..020ec40 100644 --- a/datastructures_test.go +++ b/datastructures_test.go @@ -22,4 +22,17 @@ func TestBuildingTree(t *testing.T) { if tree.Get("bar") != 5 { 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 }