diff --git a/tree/avl/avl.go b/tree/avl/avl.go index 4fe37bd..56fadcc 100644 --- a/tree/avl/avl.go +++ b/tree/avl/avl.go @@ -1,8 +1,10 @@ package avl import ( - "github.com/474420502/focus/compare" "github.com/davecgh/go-spew/spew" + + "github.com/474420502/focus/compare" + "github.com/474420502/focus/tree" ) type Node struct { @@ -31,6 +33,10 @@ type Tree struct { iter *Iterator } +func assertImplementation() { + var _ tree.IBSTree = (*Tree)(nil) +} + func New(Compare compare.Compare) *Tree { return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)} } diff --git a/tree/avldup/avldup.go b/tree/avldup/avldup.go index 5c5203a..e913a6d 100644 --- a/tree/avldup/avldup.go +++ b/tree/avldup/avldup.go @@ -1,8 +1,10 @@ package avldup import ( - "github.com/474420502/focus/compare" "github.com/davecgh/go-spew/spew" + + "github.com/474420502/focus/compare" + "github.com/474420502/focus/tree" ) type Node struct { @@ -32,6 +34,10 @@ type Tree struct { iter *Iterator } +func assertImplementation() { + var _ tree.IBSTree = (*Tree)(nil) +} + func New(Compare compare.Compare) *Tree { return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)} } diff --git a/tree/avlkey/avlkey.go b/tree/avlkey/avlkey.go index c4dd2f4..a6f164b 100644 --- a/tree/avlkey/avlkey.go +++ b/tree/avlkey/avlkey.go @@ -1,8 +1,10 @@ package avlkey import ( - "github.com/474420502/focus/compare" "github.com/davecgh/go-spew/spew" + + "github.com/474420502/focus/compare" + "github.com/474420502/focus/tree" ) type Node struct { @@ -35,6 +37,10 @@ func New(Compare compare.Compare) *Tree { return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)} } +func assertImplementation() { + var _ tree.IBSTreeKey = (*Tree)(nil) +} + func (tree *Tree) String() string { if tree.size == 0 { return "" diff --git a/tree/avlkeydup/avlkeydup.go b/tree/avlkeydup/avlkeydup.go index d7bd156..ddd2dbc 100644 --- a/tree/avlkeydup/avlkeydup.go +++ b/tree/avlkeydup/avlkeydup.go @@ -1,8 +1,10 @@ package avlkeydup import ( - "github.com/474420502/focus/compare" "github.com/davecgh/go-spew/spew" + + "github.com/474420502/focus/compare" + "github.com/474420502/focus/tree" ) type Node struct { @@ -31,6 +33,10 @@ type Tree struct { iter *Iterator } +func assertImplementation() { + var _ tree.IBSTreeKey = (*Tree)(nil) +} + func New(Compare compare.Compare) *Tree { return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)} } diff --git a/heap/heap.go b/tree/heap/heap.go similarity index 98% rename from heap/heap.go rename to tree/heap/heap.go index b6564cd..b574056 100644 --- a/heap/heap.go +++ b/tree/heap/heap.go @@ -54,7 +54,7 @@ func (h *Heap) Top() (interface{}, bool) { return nil, false } -func (h *Heap) Push(v interface{}) { +func (h *Heap) Put(v interface{}) { if v == nil { return } diff --git a/heap/heap_test.go b/tree/heap/heap_test.go similarity index 99% rename from heap/heap_test.go rename to tree/heap/heap_test.go index bb9cb66..5d085df 100644 --- a/heap/heap_test.go +++ b/tree/heap/heap_test.go @@ -11,7 +11,7 @@ func TestHeapPushTopPop(t *testing.T) { l := []int{9, 5, 15, 2, 3} ol := []int{15, 9, 5, 3, 2} for _, v := range l { - h.Push(v) + h.Put(v) } for _, tv := range ol { diff --git a/tree/tree.go b/tree/tree.go new file mode 100644 index 0000000..6fd8c2c --- /dev/null +++ b/tree/tree.go @@ -0,0 +1,27 @@ +package tree + +// IBSTreeKey Compare函数可以自定义所以key不一定value, 可以是value结构体中一个属性 +type IBSTreeKey interface { + String() string + Size() int + Remove(key interface{}) (interface{}, bool) + Clear() + // Values 返回先序遍历的值 + Values() []interface{} + Get(key interface{}) (interface{}, bool) + Put(key, value interface{}) + Traversal(every func(k, v interface{}) bool, traversalMethod ...interface{}) +} + +// IBSTree +type IBSTree interface { + String() string + Size() int + Remove(key interface{}) (interface{}, bool) + Clear() + // Values 返回先序遍历的值 + Values() []interface{} + Get(key interface{}) (interface{}, bool) + Put(value interface{}) + Traversal(every func(v interface{}) bool, traversalMethod ...interface{}) +} diff --git a/tree/vbt/vbt.go b/tree/vbt/vbt.go index a703bd7..3532cb5 100644 --- a/tree/vbt/vbt.go +++ b/tree/vbt/vbt.go @@ -1,8 +1,10 @@ package vbt import ( - "github.com/474420502/focus/compare" "github.com/davecgh/go-spew/spew" + + "github.com/474420502/focus/compare" + "github.com/474420502/focus/tree" ) type Node struct { @@ -31,6 +33,10 @@ type Tree struct { iter *Iterator } +func assertImplementation() { + var _ tree.IBSTree = (*Tree)(nil) +} + func New(Compare compare.Compare) *Tree { return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)} } @@ -231,6 +237,11 @@ func (tree *Tree) Remove(key interface{}) (interface{}, bool) { return nil, false } +func (tree *Tree) Clear() { + tree.root = nil + tree.iter = NewIteratorWithCap(nil, 16) +} + // Values 返回先序遍历的值 func (tree *Tree) Values() []interface{} { mszie := 0 diff --git a/tree/vbtkey/vbtkey.go b/tree/vbtkey/vbtkey.go index 580b73e..a85d699 100644 --- a/tree/vbtkey/vbtkey.go +++ b/tree/vbtkey/vbtkey.go @@ -1,8 +1,10 @@ package vbtkey import ( - "github.com/474420502/focus/compare" "github.com/davecgh/go-spew/spew" + + "github.com/474420502/focus/compare" + "github.com/474420502/focus/tree" ) type Node struct { @@ -32,6 +34,10 @@ type Tree struct { iter *Iterator } +func assertImplementation() { + var _ tree.IBSTreeKey = (*Tree)(nil) +} + func New(Compare compare.Compare) *Tree { return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)} } @@ -232,6 +238,11 @@ func (tree *Tree) Remove(key interface{}) (interface{}, bool) { return nil, false } +func (tree *Tree) Clear() { + tree.root = nil + tree.iter = NewIteratorWithCap(nil, 16) +} + // Values 返回先序遍历的值 func (tree *Tree) Values() []interface{} { mszie := 0 @@ -239,7 +250,7 @@ func (tree *Tree) Values() []interface{} { mszie = tree.root.size } result := make([]interface{}, 0, mszie) - tree.Traversal(func(v interface{}) bool { + tree.Traversal(func(k, v interface{}) bool { result = append(result, v) return true }, LDR) @@ -500,8 +511,8 @@ const ( RLD ) -// Traversal 遍历的方法 默认是LDR 从小到大 comparator 为 l < r -func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...interface{}) { +// Traversal 遍历的方法 默认是LDR 从小到大 Compare 为 l < r +func (tree *Tree) Traversal(every func(k, v interface{}) bool, traversalMethod ...interface{}) { if tree.root == nil { return } @@ -518,7 +529,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i if cur == nil { return true } - if !every(cur.value) { + if !every(cur.key, cur.value) { return false } if !traverasl(cur.children[0]) { @@ -539,7 +550,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i if !traverasl(cur.children[0]) { return false } - if !every(cur.value) { + if !every(cur.key, cur.value) { return false } if !traverasl(cur.children[1]) { @@ -560,7 +571,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i if !traverasl(cur.children[1]) { return false } - if !every(cur.value) { + if !every(cur.key, cur.value) { return false } return true @@ -572,7 +583,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i if cur == nil { return true } - if !every(cur.value) { + if !every(cur.key, cur.value) { return false } if !traverasl(cur.children[0]) { @@ -593,7 +604,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i if !traverasl(cur.children[1]) { return false } - if !every(cur.value) { + if !every(cur.key, cur.value) { return false } if !traverasl(cur.children[0]) { @@ -614,7 +625,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i if !traverasl(cur.children[0]) { return false } - if !every(cur.value) { + if !every(cur.key, cur.value) { return false } return true diff --git a/tree/vbtkey/vbtkey_test.go b/tree/vbtkey/vbtkey_test.go index 1ea0c4b..9b5f2af 100644 --- a/tree/vbtkey/vbtkey_test.go +++ b/tree/vbtkey/vbtkey_test.go @@ -7,9 +7,9 @@ import ( "log" "testing" - "github.com/474420502/focus/compare" - "github.com/davecgh/go-spew/spew" + + "github.com/474420502/focus/compare" ) func loadTestData() []int { @@ -265,8 +265,8 @@ func TestTravalsal(t *testing.T) { i := 0 var result []interface{} - tree.Traversal(func(v interface{}) bool { - result = append(result, v) + tree.Traversal(func(k, v interface{}) bool { + result = append(result, k) i++ if i >= 10 { return false