From 155f287ab0b1293d264369b65f06db31a6af7b0b Mon Sep 17 00:00:00 2001 From: dudaodong Date: Tue, 26 Apr 2022 11:04:04 +0800 Subject: [PATCH] refactor: add param comparator in NewBSTree func --- datastructure/queue/priorityqueue.go | 2 +- datastructure/tree/bstree.go | 16 +++--- datastructure/tree/bstree_test.go | 79 +++++++++++++--------------- 3 files changed, 46 insertions(+), 51 deletions(-) diff --git a/datastructure/queue/priorityqueue.go b/datastructure/queue/priorityqueue.go index a02347d..528446a 100644 --- a/datastructure/queue/priorityqueue.go +++ b/datastructure/queue/priorityqueue.go @@ -15,7 +15,7 @@ type PriorityQueue[T any] struct { } // 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] { return &PriorityQueue[T]{ items: make([]T, capacity+1), diff --git a/datastructure/tree/bstree.go b/datastructure/tree/bstree.go index 6801fcd..c4c1504 100644 --- a/datastructure/tree/bstree.go +++ b/datastructure/tree/bstree.go @@ -12,29 +12,31 @@ import ( // In BSTree: leftNode < rootNode < rightNode // type T should implements Compare function in lancetconstraints.Comparator interface. type BSTree[T any] struct { - root *datastructure.TreeNode[T] + root *datastructure.TreeNode[T] + comparator lancetconstraints.Comparator } // 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) - return &BSTree[T]{root} + return &BSTree[T]{root, comparator} } // 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 newNode := datastructure.NewTreeNode(data) if root == nil { t.root = newNode } else { - insertTreeNode(root, newNode, comparator) + insertTreeNode(root, newNode, t.comparator) } } // DeletetNode delete data into BSTree -func (t *BSTree[T]) DeletetNode(data T, comparator lancetconstraints.Comparator) { - deleteTreeNode(t.root, data, comparator) +func (t *BSTree[T]) DeletetNode(data T) { + deleteTreeNode(t.root, data, t.comparator) } // NodeLevel get node level in BSTree diff --git a/datastructure/tree/bstree_test.go b/datastructure/tree/bstree_test.go index 7613424..07da79e 100644 --- a/datastructure/tree/bstree_test.go +++ b/datastructure/tree/bstree_test.go @@ -21,13 +21,12 @@ func (c *intComparator) Compare(v1, v2 any) int { } func TestBSTree_InsertNode(t *testing.T) { - bstree := NewBSTree(6) + bstree := NewBSTree(6, &intComparator{}) - comparator := &intComparator{} - bstree.InsertNode(7, comparator) - bstree.InsertNode(5, comparator) - bstree.InsertNode(2, comparator) - bstree.InsertNode(4, comparator) + bstree.InsertNode(7) + bstree.InsertNode(5) + bstree.InsertNode(2) + bstree.InsertNode(4) bstree.Print() } @@ -35,13 +34,12 @@ func TestBSTree_InsertNode(t *testing.T) { func TestBSTree_PreOrderTraverse(t *testing.T) { assert := internal.NewAssert(t, "TestBSTree_PreOrderTraverse") - bstree := NewBSTree(6) + bstree := NewBSTree(6, &intComparator{}) - comparator := &intComparator{} - bstree.InsertNode(7, comparator) - bstree.InsertNode(5, comparator) - bstree.InsertNode(2, comparator) - bstree.InsertNode(4, comparator) + bstree.InsertNode(7) + bstree.InsertNode(5) + bstree.InsertNode(2) + bstree.InsertNode(4) acturl := bstree.PreOrderTraverse() t.Log(acturl) @@ -51,13 +49,12 @@ func TestBSTree_PreOrderTraverse(t *testing.T) { func TestBSTree_PostOrderTraverse(t *testing.T) { assert := internal.NewAssert(t, "TestBSTree_PostOrderTraverse") - bstree := NewBSTree(6) + bstree := NewBSTree(6, &intComparator{}) - comparator := &intComparator{} - bstree.InsertNode(7, comparator) - bstree.InsertNode(5, comparator) - bstree.InsertNode(2, comparator) - bstree.InsertNode(4, comparator) + bstree.InsertNode(7) + bstree.InsertNode(5) + bstree.InsertNode(2) + bstree.InsertNode(4) acturl := bstree.PostOrderTraverse() t.Log(acturl) @@ -67,13 +64,12 @@ func TestBSTree_PostOrderTraverse(t *testing.T) { func TestBSTree_InOrderTraverse(t *testing.T) { assert := internal.NewAssert(t, "TestBSTree_InOrderTraverse") - bstree := NewBSTree(6) + bstree := NewBSTree(6, &intComparator{}) - comparator := &intComparator{} - bstree.InsertNode(7, comparator) - bstree.InsertNode(5, comparator) - bstree.InsertNode(2, comparator) - bstree.InsertNode(4, comparator) + bstree.InsertNode(7) + bstree.InsertNode(5) + bstree.InsertNode(2) + bstree.InsertNode(4) acturl := bstree.InOrderTraverse() t.Log(acturl) @@ -83,13 +79,12 @@ func TestBSTree_InOrderTraverse(t *testing.T) { func TestBSTree_LevelOrderTraverse(t *testing.T) { assert := internal.NewAssert(t, "TestBSTree_LevelOrderTraverse") - bstree := NewBSTree(6) + bstree := NewBSTree(6, &intComparator{}) - comparator := &intComparator{} - bstree.InsertNode(7, comparator) - bstree.InsertNode(5, comparator) - bstree.InsertNode(2, comparator) - bstree.InsertNode(4, comparator) + bstree.InsertNode(7) + bstree.InsertNode(5) + bstree.InsertNode(2) + bstree.InsertNode(4) bstree.Print() @@ -101,17 +96,16 @@ func TestBSTree_LevelOrderTraverse(t *testing.T) { func TestBSTree_DeletetNode(t *testing.T) { assert := internal.NewAssert(t, "TestBSTree_DeletetNode") - bstree := NewBSTree(6) + bstree := NewBSTree(6, &intComparator{}) - comparator := &intComparator{} - bstree.InsertNode(7, comparator) - bstree.InsertNode(5, comparator) - bstree.InsertNode(2, comparator) - bstree.InsertNode(4, comparator) + bstree.InsertNode(7) + bstree.InsertNode(5) + bstree.InsertNode(2) + bstree.InsertNode(4) bstree.Print() - bstree.DeletetNode(4, comparator) + bstree.DeletetNode(4) bstree.Print() acturl1 := bstree.InOrderTraverse() t.Log(acturl1) @@ -128,13 +122,12 @@ func TestBSTree_DeletetNode(t *testing.T) { func TestBSTree_Depth(t *testing.T) { assert := internal.NewAssert(t, "TestBSTree_Depth") - bstree := NewBSTree(6) + bstree := NewBSTree(6, &intComparator{}) - comparator := &intComparator{} - bstree.InsertNode(7, comparator) - bstree.InsertNode(5, comparator) - bstree.InsertNode(2, comparator) - bstree.InsertNode(4, comparator) + bstree.InsertNode(7) + bstree.InsertNode(5) + bstree.InsertNode(2) + bstree.InsertNode(4) bstree.Print()