mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
feat: add BSTree which is binary search tree
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package datastructure
|
||||
|
||||
import "testing"
|
||||
|
||||
type intComparator struct{}
|
||||
|
||||
func (c *intComparator) Compare(v1, v2 any) int {
|
||||
val1, _ := v1.(int)
|
||||
val2, _ := v2.(int)
|
||||
|
||||
if val1 < val2 {
|
||||
return -1
|
||||
} else if val1 > val2 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func TestBSTree_Insert(t *testing.T) {
|
||||
bstree := NewBSTree(6)
|
||||
|
||||
comparator := &intComparator{}
|
||||
bstree.Insert(7, comparator)
|
||||
bstree.Insert(5, comparator)
|
||||
bstree.Insert(2, comparator)
|
||||
bstree.Insert(4, comparator)
|
||||
|
||||
bstree.Print()
|
||||
}
|
||||
Reference in New Issue
Block a user