1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-03-01 00:35:28 +08:00

refactor: add param comparator in NewBSTree func

This commit is contained in:
dudaodong
2022-04-26 11:04:04 +08:00
parent 652916b7d7
commit 155f287ab0
3 changed files with 46 additions and 51 deletions

View File

@@ -15,7 +15,7 @@ type PriorityQueue[T any] struct {
} }
// NewPriorityQueue return a pointer of PriorityQueue // NewPriorityQueue return a pointer of PriorityQueue
// param `comparator` is used to compare valuse in the heap // param `comparator` is used to compare values in the queue
func NewPriorityQueue[T any](capacity int, comparator lancetconstraints.Comparator) *PriorityQueue[T] { func NewPriorityQueue[T any](capacity int, comparator lancetconstraints.Comparator) *PriorityQueue[T] {
return &PriorityQueue[T]{ return &PriorityQueue[T]{
items: make([]T, capacity+1), items: make([]T, capacity+1),

View File

@@ -12,29 +12,31 @@ import (
// In BSTree: leftNode < rootNode < rightNode // In BSTree: leftNode < rootNode < rightNode
// type T should implements Compare function in lancetconstraints.Comparator interface. // type T should implements Compare function in lancetconstraints.Comparator interface.
type BSTree[T any] struct { type BSTree[T any] struct {
root *datastructure.TreeNode[T] root *datastructure.TreeNode[T]
comparator lancetconstraints.Comparator
} }
// NewBSTree create a BSTree pointer // NewBSTree create a BSTree pointer
func NewBSTree[T any](rootData T) *BSTree[T] { // param `comparator` is used to compare values in the tree
func NewBSTree[T any](rootData T, comparator lancetconstraints.Comparator) *BSTree[T] {
root := datastructure.NewTreeNode(rootData) root := datastructure.NewTreeNode(rootData)
return &BSTree[T]{root} return &BSTree[T]{root, comparator}
} }
// InsertNode insert data into BSTree // InsertNode insert data into BSTree
func (t *BSTree[T]) InsertNode(data T, comparator lancetconstraints.Comparator) { func (t *BSTree[T]) InsertNode(data T) {
root := t.root root := t.root
newNode := datastructure.NewTreeNode(data) newNode := datastructure.NewTreeNode(data)
if root == nil { if root == nil {
t.root = newNode t.root = newNode
} else { } else {
insertTreeNode(root, newNode, comparator) insertTreeNode(root, newNode, t.comparator)
} }
} }
// DeletetNode delete data into BSTree // DeletetNode delete data into BSTree
func (t *BSTree[T]) DeletetNode(data T, comparator lancetconstraints.Comparator) { func (t *BSTree[T]) DeletetNode(data T) {
deleteTreeNode(t.root, data, comparator) deleteTreeNode(t.root, data, t.comparator)
} }
// NodeLevel get node level in BSTree // NodeLevel get node level in BSTree

View File

@@ -21,13 +21,12 @@ func (c *intComparator) Compare(v1, v2 any) int {
} }
func TestBSTree_InsertNode(t *testing.T) { func TestBSTree_InsertNode(t *testing.T) {
bstree := NewBSTree(6) bstree := NewBSTree(6, &intComparator{})
comparator := &intComparator{} bstree.InsertNode(7)
bstree.InsertNode(7, comparator) bstree.InsertNode(5)
bstree.InsertNode(5, comparator) bstree.InsertNode(2)
bstree.InsertNode(2, comparator) bstree.InsertNode(4)
bstree.InsertNode(4, comparator)
bstree.Print() bstree.Print()
} }
@@ -35,13 +34,12 @@ func TestBSTree_InsertNode(t *testing.T) {
func TestBSTree_PreOrderTraverse(t *testing.T) { func TestBSTree_PreOrderTraverse(t *testing.T) {
assert := internal.NewAssert(t, "TestBSTree_PreOrderTraverse") assert := internal.NewAssert(t, "TestBSTree_PreOrderTraverse")
bstree := NewBSTree(6) bstree := NewBSTree(6, &intComparator{})
comparator := &intComparator{} bstree.InsertNode(7)
bstree.InsertNode(7, comparator) bstree.InsertNode(5)
bstree.InsertNode(5, comparator) bstree.InsertNode(2)
bstree.InsertNode(2, comparator) bstree.InsertNode(4)
bstree.InsertNode(4, comparator)
acturl := bstree.PreOrderTraverse() acturl := bstree.PreOrderTraverse()
t.Log(acturl) t.Log(acturl)
@@ -51,13 +49,12 @@ func TestBSTree_PreOrderTraverse(t *testing.T) {
func TestBSTree_PostOrderTraverse(t *testing.T) { func TestBSTree_PostOrderTraverse(t *testing.T) {
assert := internal.NewAssert(t, "TestBSTree_PostOrderTraverse") assert := internal.NewAssert(t, "TestBSTree_PostOrderTraverse")
bstree := NewBSTree(6) bstree := NewBSTree(6, &intComparator{})
comparator := &intComparator{} bstree.InsertNode(7)
bstree.InsertNode(7, comparator) bstree.InsertNode(5)
bstree.InsertNode(5, comparator) bstree.InsertNode(2)
bstree.InsertNode(2, comparator) bstree.InsertNode(4)
bstree.InsertNode(4, comparator)
acturl := bstree.PostOrderTraverse() acturl := bstree.PostOrderTraverse()
t.Log(acturl) t.Log(acturl)
@@ -67,13 +64,12 @@ func TestBSTree_PostOrderTraverse(t *testing.T) {
func TestBSTree_InOrderTraverse(t *testing.T) { func TestBSTree_InOrderTraverse(t *testing.T) {
assert := internal.NewAssert(t, "TestBSTree_InOrderTraverse") assert := internal.NewAssert(t, "TestBSTree_InOrderTraverse")
bstree := NewBSTree(6) bstree := NewBSTree(6, &intComparator{})
comparator := &intComparator{} bstree.InsertNode(7)
bstree.InsertNode(7, comparator) bstree.InsertNode(5)
bstree.InsertNode(5, comparator) bstree.InsertNode(2)
bstree.InsertNode(2, comparator) bstree.InsertNode(4)
bstree.InsertNode(4, comparator)
acturl := bstree.InOrderTraverse() acturl := bstree.InOrderTraverse()
t.Log(acturl) t.Log(acturl)
@@ -83,13 +79,12 @@ func TestBSTree_InOrderTraverse(t *testing.T) {
func TestBSTree_LevelOrderTraverse(t *testing.T) { func TestBSTree_LevelOrderTraverse(t *testing.T) {
assert := internal.NewAssert(t, "TestBSTree_LevelOrderTraverse") assert := internal.NewAssert(t, "TestBSTree_LevelOrderTraverse")
bstree := NewBSTree(6) bstree := NewBSTree(6, &intComparator{})
comparator := &intComparator{} bstree.InsertNode(7)
bstree.InsertNode(7, comparator) bstree.InsertNode(5)
bstree.InsertNode(5, comparator) bstree.InsertNode(2)
bstree.InsertNode(2, comparator) bstree.InsertNode(4)
bstree.InsertNode(4, comparator)
bstree.Print() bstree.Print()
@@ -101,17 +96,16 @@ func TestBSTree_LevelOrderTraverse(t *testing.T) {
func TestBSTree_DeletetNode(t *testing.T) { func TestBSTree_DeletetNode(t *testing.T) {
assert := internal.NewAssert(t, "TestBSTree_DeletetNode") assert := internal.NewAssert(t, "TestBSTree_DeletetNode")
bstree := NewBSTree(6) bstree := NewBSTree(6, &intComparator{})
comparator := &intComparator{} bstree.InsertNode(7)
bstree.InsertNode(7, comparator) bstree.InsertNode(5)
bstree.InsertNode(5, comparator) bstree.InsertNode(2)
bstree.InsertNode(2, comparator) bstree.InsertNode(4)
bstree.InsertNode(4, comparator)
bstree.Print() bstree.Print()
bstree.DeletetNode(4, comparator) bstree.DeletetNode(4)
bstree.Print() bstree.Print()
acturl1 := bstree.InOrderTraverse() acturl1 := bstree.InOrderTraverse()
t.Log(acturl1) t.Log(acturl1)
@@ -128,13 +122,12 @@ func TestBSTree_DeletetNode(t *testing.T) {
func TestBSTree_Depth(t *testing.T) { func TestBSTree_Depth(t *testing.T) {
assert := internal.NewAssert(t, "TestBSTree_Depth") assert := internal.NewAssert(t, "TestBSTree_Depth")
bstree := NewBSTree(6) bstree := NewBSTree(6, &intComparator{})
comparator := &intComparator{} bstree.InsertNode(7)
bstree.InsertNode(7, comparator) bstree.InsertNode(5)
bstree.InsertNode(5, comparator) bstree.InsertNode(2)
bstree.InsertNode(2, comparator) bstree.InsertNode(4)
bstree.InsertNode(4, comparator)
bstree.Print() bstree.Print()