diff options
| author | Paul Buetow <git@mx.buetow.org> | 2020-11-05 09:42:08 +0000 |
|---|---|---|
| committer | Paul Buetow <git@mx.buetow.org> | 2020-11-05 09:42:08 +0000 |
| commit | 24c2952ab16680383864625c8cc8cd73bfd94849 (patch) | |
| tree | 39fe704420a995fbe98dad450d47094c5a54db34 | |
| parent | 93009d0246a19b6500afd52da4023debe1e8c99d (diff) | |
BST basic unit test works
| -rw-r--r-- | set/bst.go (renamed from set/tree.go) | 38 | ||||
| -rw-r--r-- | set/set_test.go | 10 |
2 files changed, 31 insertions, 17 deletions
@@ -7,38 +7,42 @@ type node struct { right *node } -type Tree struct { +type BST struct { root *node } -func NewTree() *Tree { - return &Tree{} +func NewBST() *BST { + return &BST{} } -func (t *Tree) Empty() bool { +func (t *BST) Empty() bool { return t.root == nil } -func (t *Tree) Set(key, val int) { +func (t *BST) Set(key, val int) { if t.Empty() { t.root = &node{key, val, nil, nil} return } ptr, _, err := t.search(&t.root, key) - if err != nil { - // key already in the tree as node found + switch err { + case nil: + // key already in the tree + return + case NotFound: + *ptr = &node{key, val, nil, nil} return + default: + panic(err) } - - *ptr = &node{key, val, nil, nil} } -func (t *Tree) Get(key int) (int, error) { +func (t *BST) Get(key int) (int, error) { _, n, err := t.search(&t.root, key) return n.val, err } -func (t *Tree) Del(key int) (int, error) { +func (t *BST) Del(key int) (int, error) { ptr, n, err := t.search(&t.root, key) if err != nil { return 0, err @@ -79,10 +83,10 @@ func (t *Tree) Del(key int) (int, error) { } } -func (t *Tree) search(ptr **node, key int) (**node, *node, error) { +func (t *BST) search(ptr **node, key int) (**node, *node, error) { n := *ptr if n == nil { - return nil, nil, NotFound + return ptr, nil, NotFound } switch { @@ -95,7 +99,7 @@ func (t *Tree) search(ptr **node, key int) (**node, *node, error) { } } -func (t *Tree) deleteMin(ptr **node) (*node, error) { +func (t *BST) deleteMin(ptr **node) (*node, error) { ptr, n, err := t.min(ptr) if err != nil { return nil, err @@ -106,7 +110,7 @@ func (t *Tree) deleteMin(ptr **node) (*node, error) { return n, nil } -func (t *Tree) min(ptr **node) (**node, *node, error) { +func (t *BST) min(ptr **node) (**node, *node, error) { n := *ptr if n == nil { return nil, nil, NotFound @@ -121,7 +125,8 @@ func (t *Tree) min(ptr **node) (**node, *node, error) { } } -func (t *Tree) max(ptr **node) (**node, *node, error) { +/* +func (t *BST) max(ptr **node) (**node, *node, error) { n := *ptr if n == nil { return nil, nil, NotFound @@ -135,3 +140,4 @@ func (t *Tree) max(ptr **node) (**node, *node, error) { n = *ptr } } +*/ diff --git a/set/set_test.go b/set/set_test.go index 16d3e8c..cd44e24 100644 --- a/set/set_test.go +++ b/set/set_test.go @@ -11,17 +11,25 @@ const factor int = 10 const minLength int = 1 const maxLength int = 10 -func TestSet(t *testing.T) { +func TestElementary(t *testing.T) { s := NewElementary() for i := minLength; i <= maxLength; i *= factor { test(s, i, t) } } +func TestBST(t *testing.T) { + s := NewBST() + for i := minLength; i <= maxLength; i *= factor { + test(s, i, t) + } +} + func test(s Set, l int, t *testing.T) { cb := func(t *testing.T) { list := ds.NewRandomArrayList(l, -1) for i, a := range list { + t.Log("Inserting", a, i, s) s.Set(a, i) } for i, a := range list { |
