Skip to content

Commit 419006b

Browse files
committed
return node on Search
1 parent 9abcbe7 commit 419006b

File tree

2 files changed

+7
-23
lines changed

2 files changed

+7
-23
lines changed

bst.go

+4-20
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,6 @@ func (n *Node) Insert(v Item) error {
5757
return n.right.Insert(v)
5858
}
5959

60-
//// Insert multiple nodes. If duplicate values specified, will keep inserting and return a single Error
61-
//func (n *Node) insertBulk(values []Item) (int, error) { // todo Remove?
62-
// var numInserted int
63-
// for _, v := range values {
64-
// err := n.Insert(v)
65-
// if err == nil {
66-
// numInserted += 1
67-
// }
68-
// }
69-
// if numInserted != len(values) {
70-
// return numInserted, errors.New("failed trying to Insert duplicate value(s)")
71-
// }
72-
// return numInserted, nil
73-
//}
74-
7560
// Get number of nodes from Node n (inclusive)
7661
func (n *Node) Count() int {
7762
if !n.isReady {
@@ -140,12 +125,12 @@ func (n *Node) InOrder() []Item { // TODO take func as arg?
140125
}
141126

142127
// Search for a value starting at Node n (inclusive)
143-
func (n *Node) Search(searchVal Item) (bool, error) {
128+
func (n *Node) Search(searchVal Item) (bool, error, *Node) {
144129
if !n.isReady {
145-
return false, errors.New("cannot search empty tree")
130+
return false, errors.New("cannot search empty tree"), nil
146131
}
147132
if searchVal.Equals(n.val) {
148-
return true, nil
133+
return true, nil, n
149134
}
150135
if searchVal.Less(n.val) {
151136
// search left
@@ -157,8 +142,7 @@ func (n *Node) Search(searchVal Item) (bool, error) {
157142
if n.right != nil {
158143
return n.right.Search(searchVal)
159144
}
160-
//
161-
return false, nil
145+
return false, nil, nil
162146
}
163147

164148
// Remove node with specified value. Returns a new root node.

bst_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func TestSearch(t *testing.T) {
211211
test.tree = &Node{}
212212
_, _ = insertBulk(test.tree, test.input)
213213
// Test Search
214-
wasFound, err := test.tree.Search(test.searchVal)
214+
wasFound, err, _ := test.tree.Search(test.searchVal) // todo check returned node
215215
if (err != nil) && len(test.input) > 0 {
216216
t.Errorf("search %v failed on %v, wanted %v. got %v. %v", test.searchVal, test.input, test.wantFound, wasFound, err)
217217
}
@@ -249,7 +249,7 @@ func TestRemove(t *testing.T) {
249249
test.tree = test.tree.Remove(test.removeVal)
250250
// test that only the specified node was removed
251251
for _, inputVal := range test.input {
252-
stillPresent, _ := test.tree.Search(inputVal)
252+
stillPresent, _, _ := test.tree.Search(inputVal) // TODO check returned node?
253253
if stillPresent && inputVal == test.removeVal {
254254
t.Errorf("Remove(%v) failed on %v. wanted %v. got stillPresent=%v", test.removeVal, test.input, test.wantSucceed, stillPresent)
255255
}
@@ -277,7 +277,7 @@ func TestRemove(t *testing.T) {
277277
}
278278
}
279279

280-
// Utility max/min function for myInt
280+
// Utility max/min function for mysInt
281281
func maxMyInt(x ...MyInt) MyInt {
282282
max := x[0]
283283
for _, v := range x {

0 commit comments

Comments
 (0)