Skip to content

Commit c4473d4

Browse files
committed
refactor!: ♻️ rename Delete API to the Remove API
I have renamed the `Delete` API to the `Remove` API. I made this change to make the APIs of my K-D Tree library consistent with those of existing ones written in Go. BREAKING CHANGE: With the rename of the `Delete` API to the `Remove` API in this commit, any code that uses the `Delete` API will no longer work with future releases of this library created after this commit. Affected codebases must migrate to the `Remove` API to be compatible with future releases of this library.
1 parent 422b80d commit c4473d4

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

kdtree.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ func (t *KDTree[T]) Insert(value T) {
121121
}
122122
}
123123

124-
func (t *KDTree[T]) Delete(value T) bool {
124+
func (t *KDTree[T]) Remove(value T) bool {
125125
ok := false
126-
t.root, ok = deleteNode(t.dimensions, value, 0, t.root)
126+
t.root, ok = removeNode(t.dimensions, value, 0, t.root)
127127
if ok {
128128
t.size--
129129
}
@@ -379,7 +379,7 @@ func insertAllNew[T Comparable[T]](vs []T, initialIndices [][]int, cd int) *kdNo
379379
return n
380380
}
381381

382-
func deleteNode[T Comparable[T]](d int, value T, cd int, r *kdNode[T]) (*kdNode[T], bool) {
382+
func removeNode[T Comparable[T]](d int, value T, cd int, r *kdNode[T]) (*kdNode[T], bool) {
383383
if r == nil {
384384
return nil, false
385385
}
@@ -389,18 +389,18 @@ func deleteNode[T Comparable[T]](d int, value T, cd int, r *kdNode[T]) (*kdNode[
389389
ok = true
390390
if r.right != nil {
391391
r.value = *findMin(d, cd, ncd, r.right)
392-
r.right, ok = deleteNode(d, r.value, ncd, r.right)
392+
r.right, ok = removeNode(d, r.value, ncd, r.right)
393393
} else if r.left != nil {
394394
r.value = *findMin(d, cd, ncd, r.left)
395-
r.right, ok = deleteNode(d, r.value, ncd, r.left)
395+
r.right, ok = removeNode(d, r.value, ncd, r.left)
396396
r.left = nil
397397
} else {
398398
r = nil
399399
}
400400
} else if value.Order(r.value, cd) < 0 {
401-
r.left, ok = deleteNode(d, value, ncd, r.left)
401+
r.left, ok = removeNode(d, value, ncd, r.left)
402402
} else {
403-
r.right, ok = deleteNode(d, value, ncd, r.right)
403+
r.right, ok = removeNode(d, value, ncd, r.right)
404404
}
405405
return r, ok
406406
}

kdtree_internal_2d_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
types "github.com/rishitc/go-kd-tree/internal/types"
77
)
88

9-
func Test2DDeleteAllNodesInTree(t *testing.T) {
9+
func Test2DRemoveAllNodesInTree(t *testing.T) {
1010
treeNodes := NewKDNode(types.Tensor2D{5, 6})
1111
tree := NewTestKDTree(2, treeNodes)
1212

@@ -20,15 +20,15 @@ func Test2DDeleteAllNodesInTree(t *testing.T) {
2020
},
2121
}
2222
for _, v := range testTable {
23-
ok := tree.Delete(v.input)
23+
ok := tree.Remove(v.input)
2424
if !ok || !IdenticalTrees(tree, v.expected) {
2525
t.Fatalf("Tree does not match expected tree structure\nExpected:\n%s\nGot:\n%s", v.expected, tree)
2626
}
2727
}
2828
}
2929

3030
// https://youtu.be/DkBNF98MV1Q?si=YhQLGxiH7BbG9D8s&t=37
31-
func Test2DDeleteLeafNode(t *testing.T) {
31+
func Test2DRemoveLeafNode(t *testing.T) {
3232
treeNodes := NewKDNode(types.Tensor2D{25, 50}).
3333
SetLeft(
3434
NewKDNode(types.Tensor2D{3, 25}),
@@ -58,15 +58,15 @@ func Test2DDeleteLeafNode(t *testing.T) {
5858
},
5959
}
6060
for _, v := range testTable {
61-
ok := tree.Delete(v.input)
61+
ok := tree.Remove(v.input)
6262
if !ok || !IdenticalTrees(tree, v.expected) {
6363
t.Fatalf("Tree does not match expected tree structure\nExpected:\n%s\nGot:\n%s", v.expected, tree)
6464
}
6565
}
6666
}
6767

6868
// https://youtu.be/DkBNF98MV1Q?si=-tQZZtNASyMXnhNc&t=90
69-
func Test2DDeleteNodeWithRightSubtree(t *testing.T) {
69+
func Test2DRemoveNodeWithRightSubtree(t *testing.T) {
7070
treeNodes := NewKDNode(types.Tensor2D{25, 50}).
7171
SetLeft(
7272
NewKDNode(types.Tensor2D{3, 25}).
@@ -108,15 +108,15 @@ func Test2DDeleteNodeWithRightSubtree(t *testing.T) {
108108
},
109109
}
110110
for _, v := range testTable {
111-
ok := tree.Delete(v.input)
111+
ok := tree.Remove(v.input)
112112
if !ok || !IdenticalTrees(tree, v.expected) {
113113
t.Fatalf("Tree does not match expected tree structure\nExpected:\n%s\nGot:\n%s", v.expected, tree)
114114
}
115115
}
116116
}
117117

118118
// https://youtu.be/DkBNF98MV1Q?si=v-TuZNV9YiTmCOFg&t=189
119-
func Test2DDeleteNodeWithLeftSubtreeOnly(t *testing.T) {
119+
func Test2DRemoveNodeWithLeftSubtreeOnly(t *testing.T) {
120120
treeNodes := NewKDNode(types.Tensor2D{25, 50}).
121121
SetLeft(
122122
NewKDNode(types.Tensor2D{3, 25}).
@@ -158,14 +158,14 @@ func Test2DDeleteNodeWithLeftSubtreeOnly(t *testing.T) {
158158
},
159159
}
160160
for _, v := range testTable {
161-
ok := tree.Delete(v.input)
161+
ok := tree.Remove(v.input)
162162
if !ok || !IdenticalTrees(tree, v.expected) {
163163
t.Fatalf("Tree does not match expected tree structure\nExpected:\n%s\nGot:\n%s", v.expected, tree)
164164
}
165165
}
166166
}
167167

168-
func Test2DDeleteNode1(t *testing.T) {
168+
func Test2DRemoveNode1(t *testing.T) {
169169
const dimensions = 2
170170
ps := []types.Tensor2D{
171171
{5, 6},
@@ -185,13 +185,13 @@ func Test2DDeleteNode1(t *testing.T) {
185185
},
186186
}
187187
for _, v := range testTable {
188-
ok := tree.Delete(v.input)
188+
ok := tree.Remove(v.input)
189189
if !ok || !IdenticalTrees(tree, v.expected) {
190190
t.Fatalf("Tree does not match expected tree structure\nExpected:\n%s\nGot:\n%s", v.expected, tree)
191191
}
192192
}
193193
}
194-
func Test2DDeleteNode2(t *testing.T) {
194+
func Test2DRemoveNode2(t *testing.T) {
195195
treeNodes := NewKDNode(types.Tensor2D{5, 6}).
196196
SetLeft(
197197
NewKDNode(types.Tensor2D{4, 10}).
@@ -215,7 +215,7 @@ func Test2DDeleteNode2(t *testing.T) {
215215
},
216216
}
217217
for _, v := range testTable {
218-
ok := tree.Delete(v.input)
218+
ok := tree.Remove(v.input)
219219
if !ok || !IdenticalTrees(tree, v.expected) {
220220
t.Fatalf("Tree does not match expected tree structure\nExpected:\n%s\nGot:\n%s", v.expected, tree)
221221
}
@@ -227,7 +227,7 @@ func Test2DDeleteNode2(t *testing.T) {
227227
// child of node (70, 20), and not the right child of node (70, 20).
228228
// However, the resultant tree shown in the video lecture is correct and has been used in this test case.
229229
// The below test case, has been created using the corrected valid KD-Tree as input
230-
func Test2DDeleteNode3(t *testing.T) {
230+
func Test2DRemoveNode3(t *testing.T) {
231231
treeNodes := NewKDNode(types.Tensor2D{35, 60}).
232232
SetLeft(
233233
NewKDNode(types.Tensor2D{20, 45}).
@@ -293,7 +293,7 @@ func Test2DDeleteNode3(t *testing.T) {
293293
},
294294
}
295295
for _, v := range testTable {
296-
ok := tree.Delete(v.input)
296+
ok := tree.Remove(v.input)
297297
if !ok || !IdenticalTrees(tree, v.expected) {
298298
t.Fatalf("Tree does not match expected tree structure\nExpected:\n%s\nGot:\n%s", v.expected, tree)
299299
}

0 commit comments

Comments
 (0)