Skip to content

Commit 70a63b3

Browse files
committed
Commit pre-defined node value comparator functions
1 parent cb550a1 commit 70a63b3

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

binarytree.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,24 @@ type FindFunc[T any] func(node *Node[T]) bool
6060
// (BST).
6161
type ComparatorFunc[T any] func(a, b T) int
6262

63+
// IntComparator is a comparator function for comparing integer node
64+
// values.
65+
func IntComparator(a, b int) int {
66+
if a < b {
67+
return -1
68+
} else if a > b {
69+
return 1
70+
}
71+
72+
return 0
73+
}
74+
75+
// StringComparator is a comparator function for comparing string node
76+
// values.
77+
func StringComparator(a, b string) int {
78+
return strings.Compare(a, b)
79+
}
80+
6381
// Node represents a node from a binary tree
6482
type Node[T any] struct {
6583
// Value is the value of the node

0 commit comments

Comments
 (0)