添加了树的接口

This commit is contained in:
eson 2019-07-29 02:18:22 +08:00
parent 4a58bd283e
commit 3ade1566c2
10 changed files with 94 additions and 21 deletions

View File

@ -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)}
}

View File

@ -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)}
}

View File

@ -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 ""

View File

@ -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)}
}

View File

@ -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
}

View File

@ -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 {

27
tree/tree.go Normal file
View File

@ -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{})
}

View File

@ -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

View File

@ -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

View File

@ -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