添加了树的接口

This commit is contained in:
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)}
}

154
tree/heap/heap.go Normal file
View File

@@ -0,0 +1,154 @@
package heap
import (
"github.com/474420502/focus/compare"
)
type Heap struct {
size int
elements []interface{}
Compare compare.Compare
}
func New(Compare compare.Compare) *Heap {
h := &Heap{Compare: Compare}
h.elements = make([]interface{}, 16, 16)
return h
}
func (h *Heap) Size() int {
return h.size
}
func (h *Heap) Values() []interface{} {
return h.elements[0:h.size]
}
func (h *Heap) grow() {
ecap := len(h.elements)
if h.size >= ecap {
ecap = ecap << 1
grow := make([]interface{}, ecap, ecap)
copy(grow, h.elements)
h.elements = grow
}
}
func (h *Heap) Empty() bool {
return h.size < 1
}
func (h *Heap) Clear() {
h.size = 0
}
func (h *Heap) Reborn() {
h.size = 0
h.elements = make([]interface{}, 16, 16)
}
func (h *Heap) Top() (interface{}, bool) {
if h.size != 0 {
return h.elements[0], true
}
return nil, false
}
func (h *Heap) Put(v interface{}) {
if v == nil {
return
}
h.grow()
curidx := h.size
h.size++
// up
for curidx != 0 {
pidx := (curidx - 1) >> 1
pvalue := h.elements[pidx]
if h.Compare(v, pvalue) > 0 {
h.elements[curidx] = pvalue
curidx = pidx
} else {
break
}
}
h.elements[curidx] = v
}
func (h *Heap) slimming() {
elen := len(h.elements)
if elen >= 32 {
ecap := elen >> 1
if h.size <= ecap {
ecap = elen - (ecap >> 1)
slimming := make([]interface{}, ecap, ecap)
copy(slimming, h.elements)
h.elements = slimming
}
}
}
func (h *Heap) Pop() (interface{}, bool) {
if h.size == 0 {
return nil, false
}
curidx := 0
top := h.elements[curidx]
h.size--
h.slimming()
if h.size == 0 {
return top, true
}
downvalue := h.elements[h.size]
var cidx, c1, c2 int
var cvalue1, cvalue2, cvalue interface{}
// down
for {
cidx = curidx << 1
c2 = cidx + 2
if c2 < h.size {
cvalue2 = h.elements[c2]
c1 = cidx + 1
cvalue1 = h.elements[c1]
if h.Compare(cvalue1, cvalue2) >= 0 {
cidx = c1
cvalue = cvalue1
} else {
cidx = c2
cvalue = cvalue2
}
} else {
c1 = cidx + 1
if c1 < h.size {
cvalue1 = h.elements[c1]
cidx = c1
cvalue = cvalue1
} else {
break
}
}
if h.Compare(cvalue, downvalue) > 0 {
h.elements[curidx] = cvalue
curidx = cidx
} else {
break
}
}
h.elements[curidx] = downvalue
return top, true
}

201
tree/heap/heap_test.go Normal file
View File

@@ -0,0 +1,201 @@
package heap
import (
"testing"
"github.com/474420502/focus/compare"
)
func TestHeapPushTopPop(t *testing.T) {
h := New(compare.Int)
l := []int{9, 5, 15, 2, 3}
ol := []int{15, 9, 5, 3, 2}
for _, v := range l {
h.Put(v)
}
for _, tv := range ol {
if v, isfound := h.Top(); isfound {
if !(isfound && v == tv) {
t.Error(v)
}
}
if v, isfound := h.Pop(); isfound {
if !(isfound && v == tv) {
t.Error(v)
}
}
}
if h.Size() != 0 {
t.Error("heap size is not equals to zero")
}
}
// func Int(k1, k2 interface{}) int {
// c1 := k1.(int)
// c2 := k2.(int)
// switch {
// case c1 > c2:
// return -1
// case c1 < c2:
// return 1
// default:
// return 0
// }
// }
// func TestPush(t *testing.T) {
// for i := 0; i < 1000000; i++ {
// h := New(Int)
// gods := binaryheap.NewWithIntComparator()
// for c := 0; c < 20; c++ {
// v := randomdata.Number(0, 100)
// h.Push(v)
// gods.Push(v)
// }
// r1 := spew.Sprint(h.Values())
// r2 := spew.Sprint(gods.Values())
// if r1 != r2 {
// t.Error(r1)
// t.Error(r2)
// break
// }
// }
// }
// func TestPop(t *testing.T) {
// for i := 0; i < 200000; i++ {
// h := New(Int)
// // m := make(map[int]int)
// gods := binaryheap.NewWithIntComparator()
// for c := 0; c < 40; c++ {
// v := randomdata.Number(0, 100)
// // if _, ok := m[v]; !ok {
// h.Push(v)
// gods.Push(v)
// // m[v] = v
// // }
// }
// // t.Error(h.Values())
// // t.Error(gods.Values())
// for c := 0; c < randomdata.Number(5, 10); c++ {
// v1, _ := h.Pop()
// v2, _ := gods.Pop()
// if v1 != v2 {
// t.Error(h.Values(), v1)
// t.Error(gods.Values(), v2)
// return
// }
// }
// r1 := spew.Sprint(h.Values())
// r2 := spew.Sprint(gods.Values())
// if r1 != r2 {
// t.Error(r1)
// t.Error(r2)
// break
// }
// }
// }
// func BenchmarkPush(b *testing.B) {
// l := loadTestData()
// b.ResetTimer()
// execCount := 50
// b.N = len(l) * execCount
// for c := 0; c < execCount; c++ {
// b.StopTimer()
// h := New(Int)
// b.StartTimer()
// for _, v := range l {
// h.Push(v)
// }
// }
// }
// func BenchmarkPop(b *testing.B) {
// h := New(Int)
// l := loadTestData()
// b.ResetTimer()
// execCount := 20
// b.N = len(l) * execCount
// for c := 0; c < execCount; c++ {
// b.StopTimer()
// for _, v := range l {
// h.Push(v)
// }
// b.StartTimer()
// for h.size != 0 {
// h.Pop()
// }
// }
// }
// func BenchmarkGodsPop(b *testing.B) {
// h := binaryheap.NewWithIntComparator()
// l := loadTestData()
// b.ResetTimer()
// execCount := 20
// b.N = len(l) * execCount
// for c := 0; c < execCount; c++ {
// b.StopTimer()
// for _, v := range l {
// h.Push(v)
// }
// b.StartTimer()
// for h.Size() != 0 {
// h.Pop()
// }
// }
// }
// func BenchmarkGodsPush(b *testing.B) {
// l := loadTestData()
// b.ResetTimer()
// execCount := 50
// b.N = len(l) * execCount
// for c := 0; c < execCount; c++ {
// b.StopTimer()
// h := binaryheap.NewWith(Int)
// b.StartTimer()
// for _, v := range l {
// h.Push(v)
// }
// }
// }
// func loadTestData() []int {
// data, err := ioutil.ReadFile("../l.log")
// if err != nil {
// log.Println(err)
// }
// var l []int
// decoder := gob.NewDecoder(bytes.NewReader(data))
// decoder.Decode(&l)
// return l
// }

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