From 9263c035250f071b92407257ad62e72efd200b8f Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Sun, 21 Jul 2019 04:10:08 +0800 Subject: [PATCH 1/4] =?UTF-8?q?TODO:=20=E4=BF=AE=E6=94=B9Iterator=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=80=E4=B8=AA=E6=BB=91=E5=8A=A8=E5=88=B0?= =?UTF-8?q?=E8=BE=B9=E7=95=8C=E7=9A=84=E6=96=B9=E6=B3=95.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- set/treeset/treeset.go | 12 ++- set/treeset/treeset_test.go | 156 ++++++++++++++++++++++++++++++++++++ tree/avldup/avldup.go | 8 +- 4 files changed, 170 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 7321b14..d1eefd6 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ func main() { values3 := pq.GetAround(5) // values3 = [, 5, 4] log.Println(values3) - iter := pq.Iterator() // Next 小到大 + iter := pq.Iterator() // Next 小到大 从root节点起始 // log.Println(iter.Value()) 直接使用会报错, iter.Next() // Next 从小到大 log.Println(iter.Value()) // 起始最大值. true 5 diff --git a/set/treeset/treeset.go b/set/treeset/treeset.go index af76bf3..b489ac8 100644 --- a/set/treeset/treeset.go +++ b/set/treeset/treeset.go @@ -4,8 +4,8 @@ import ( "fmt" "strings" - "github.com/474420502/focus/tree/avldup" "github.com/474420502/focus/compare" + "github.com/474420502/focus/tree/avldup" ) // TreeSet @@ -70,9 +70,15 @@ func (set *TreeSet) Size() int { return set.tree.Size() } +// Iterator avl Iterator +func (set *TreeSet) Iterator() *avldup.Iterator { + return set.tree.Iterator() +} + // String func (set *TreeSet) String() string { - content := "HashSet\n" + // content := "HashSet\n" + var content = "" items := []string{} set.tree.Traversal(func(k interface{}) bool { @@ -80,6 +86,6 @@ func (set *TreeSet) String() string { return true }) - content += strings.Join(items, ", ") + content += "(" + strings.Join(items, ", ") + ")" return content } diff --git a/set/treeset/treeset_test.go b/set/treeset/treeset_test.go index 3e1768e..6e3671b 100644 --- a/set/treeset/treeset_test.go +++ b/set/treeset/treeset_test.go @@ -1 +1,157 @@ package treeset + +import ( + "strings" + "testing" + + "github.com/davecgh/go-spew/spew" + + "github.com/474420502/focus/compare" + "github.com/474420502/focus/tree/avldup" +) + +func TestTreeSet_Add(t *testing.T) { + type fields struct { + tree *avldup.Tree + } + type args struct { + items []interface{} + } + tests := []struct { + name string + result string + fields fields + args args + }{ + {name: "add int", result: "(1, 3, 5)", args: args{items: []interface{}{1, 5, 3, 3, 5}}}, + {name: "add -int", result: "(-5, 1, 5, 3132)", args: args{items: []interface{}{-5, -5, 3132, 3132, 5, 1, 1, 1}}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + set := New(compare.Int) + set.Add(tt.args.items...) + if set.String() != tt.result { + t.Error(set.String(), " != ", tt.result) + } + }) + } + + tests2 := []struct { + name string + result string + fields fields + args args + }{ + {name: "add String 1", result: "(1, 3, 5)", args: args{items: []interface{}{"1", "5", "3", "3", "5"}}}, + {name: "add String 2", result: "(-5, 1, 3132, 5)", args: args{items: []interface{}{"-5", "-5", "3132", "3132", "5", "1", "1", "1"}}}, + {name: "add String 3", result: "(a, aa, b, bc)", args: args{items: []interface{}{"a", "b", "aa", "aa", "bc"}}}, + {name: "add String 4", result: "(他, 你, 我, 我我)", args: args{items: []interface{}{"我", "你", "他", "我", "我我"}}}, + } + for _, tt := range tests2 { + t.Run(tt.name, func(t *testing.T) { + set := New(compare.String) + set.Add(tt.args.items...) + if set.String() != tt.result { + t.Error(set.String(), " != ", tt.result) + } + + vstr := spew.Sprint(set.Values()) + + if vstr[1:len(vstr)-1] != strings.ReplaceAll(tt.result[1:len(tt.result)-1], ",", "") { + t.Error(vstr[1:len(vstr)-1], tt.result[1:len(tt.result)-1]) + } + }) + } +} + +func TestTreeSet_Remove(t *testing.T) { + type fields struct { + tree *avldup.Tree + } + type args struct { + addItems []interface{} + removeItems []interface{} + } + tests := []struct { + name string + result string + fields fields + args args + }{ + // TODO: Add test cases. + {name: "remove 1", result: "()", + args: args{ + addItems: []interface{}{5, 7, 5, 3, 2}, + removeItems: []interface{}{5, 7, 3, 2}}, + }, + + {name: "remove 2", result: "(5)", + args: args{ + addItems: []interface{}{5, 7, 5, 3, 2}, + removeItems: []interface{}{7, 3, 2}}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + set := New(compare.Int) + set.Add(tt.args.addItems...) + set.Remove(tt.args.removeItems...) + + if set.String() != tt.result { + t.Error(set.String(), " != ", tt.result) + } + }) + } +} + +func TestTreeSet_Base(t *testing.T) { + set := New(compare.Int) + set.Add(5, 4, 3, 5) + + if !set.Contains(5) { + t.Error("Contains 5 is true") + } + + set.Clear() + + if set.Contains(5) { + t.Error("Contains 5 is false") + } + + if !set.Empty() { + t.Error("should be Empty") + } + + set.Add(1, 1, 1, 2, 2, 3, 7, 9, 10) + + var results []interface{} + results = set.GetRange(4, 10) + if spew.Sprint(results) != "[7 9 10]" { + t.Error(results) + } + + results = set.GetRange(1, 10) + if spew.Sprint(results) != "[1 2 3 7 9 10]" { + t.Error(results) + } + + results3 := set.GetAround(3) + if spew.Sprint(results3) != "[2 3 7]" { + t.Error(results3) + } + + results3 = set.GetAround(1) + if spew.Sprint(results3) != "[ 1 2]" { + t.Error(results3) + } +} + +func TestTreeSet_Iterator(t *testing.T) { + set := New(compare.Int) + set.Add(5, 4, 3, 5) + + iter := set.Iterator() + for iter.Prev() { + t.Error(iter.Value()) + } +} diff --git a/tree/avldup/avldup.go b/tree/avldup/avldup.go index 8347241..5c5203a 100644 --- a/tree/avldup/avldup.go +++ b/tree/avldup/avldup.go @@ -46,14 +46,14 @@ func (tree *Tree) String() string { return str } -func (tree *Tree) Iterator() *Iterator { - return initIterator(tree) -} - func (tree *Tree) Size() int { return tree.size } +func (tree *Tree) Iterator() *Iterator { + return initIterator(tree) +} + func (tree *Tree) Remove(key interface{}) (interface{}, bool) { if n, ok := tree.GetNode(key); ok { From 76f81b99201011a1cbc11998f6a16684c609de65 Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Mon, 22 Jul 2019 02:02:05 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=8F=91=E7=8E=B0=E5=B7=B2=E7=BB=8F?= =?UTF-8?q?=E5=81=9A=E4=BA=86=E4=BD=8D=E7=BD=AE=E5=A4=84=E7=90=86,=20?= =?UTF-8?q?=E4=B8=8D=E9=9C=80=E8=A6=81=E5=86=8D=E5=8E=BB=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 ++- list/linked_list/iterator.go | 4 +- list/priority_list/iterator.go | 8 +-- list/priority_list/priority_list_test.go | 2 +- priority_queue/iterator.go | 36 +++++------ priority_queue/priority_queue.go | 9 ++- priority_queue/priority_queue_test.go | 29 +++++++++ priority_queuekey/iterator.go | 36 +++++------ priority_queuekey/priority_queuekey.go | 9 ++- priority_queuekey/priority_queuekey_test.go | 69 ++++++++++++++++++++- set/treeset/treeset_test.go | 24 ++++++- tree/avl/avl_test.go | 37 +++++++++++ tree/avl/iterator.go | 28 +++------ tree/avldup/iterator.go | 28 +++------ tree/avlkey/iterator.go | 28 +++------ tree/avlkeydup/iterator.go | 28 +++------ tree/vbt/iterator.go | 28 +++------ tree/vbtkey/iterator.go | 28 +++------ 18 files changed, 272 insertions(+), 167 deletions(-) diff --git a/README.md b/README.md index d1eefd6..4bd7dc9 100644 --- a/README.md +++ b/README.md @@ -41,11 +41,13 @@ func main() { iter := pq.Iterator() // Next 小到大 从root节点起始 // log.Println(iter.Value()) 直接使用会报错, - iter.Next() // Next 从小到大 + iter.ToHead() log.Println(iter.Value()) // 起始最大值. true 5 + log.Println(iter.Prev(), iter.Value()) // false 5 - log.Println(iter.Next(), iter.Value()) // false 5 // Prev 大到小 - log.Println(iter.Prev(), iter.Value()) // true 4 + log.Println(iter.Next(), iter.Value()) // true 4 + + } ``` diff --git a/list/linked_list/iterator.go b/list/linked_list/iterator.go index 0b12219..2024612 100644 --- a/list/linked_list/iterator.go +++ b/list/linked_list/iterator.go @@ -25,11 +25,11 @@ func (iter *Iterator) Next() bool { return iter.cur != iter.ll.tail } -func (iter *Iterator) MoveToHead() { +func (iter *Iterator) ToHead() { iter.cur = iter.ll.head } -func (iter *Iterator) MoveToTail() { +func (iter *Iterator) ToTail() { iter.cur = iter.ll.tail } diff --git a/list/priority_list/iterator.go b/list/priority_list/iterator.go index cc1738d..44a3543 100644 --- a/list/priority_list/iterator.go +++ b/list/priority_list/iterator.go @@ -25,11 +25,11 @@ func (iter *Iterator) Next() bool { return iter.cur != iter.pl.tail } -func (iter *Iterator) MoveToHead() { +func (iter *Iterator) ToHead() { iter.cur = iter.pl.head } -func (iter *Iterator) MoveToTail() { +func (iter *Iterator) ToTail() { iter.cur = iter.pl.tail } @@ -78,10 +78,10 @@ func (iter *CircularIterator) Next() bool { return true } -func (iter *CircularIterator) MoveToHead() { +func (iter *CircularIterator) ToHead() { iter.cur = iter.pl.head } -func (iter *CircularIterator) MoveToTail() { +func (iter *CircularIterator) ToTail() { iter.cur = iter.pl.tail } diff --git a/list/priority_list/priority_list_test.go b/list/priority_list/priority_list_test.go index 2ea0129..4c5d0e9 100644 --- a/list/priority_list/priority_list_test.go +++ b/list/priority_list/priority_list_test.go @@ -102,7 +102,7 @@ func TestCircularIterator(t *testing.T) { } } - iter.MoveToTail() + iter.ToTail() for i := 0; i != 10; i++ { iter.Prev() if iter.Value() != i { diff --git a/priority_queue/iterator.go b/priority_queue/iterator.go index 41dcfee..a8feea8 100644 --- a/priority_queue/iterator.go +++ b/priority_queue/iterator.go @@ -30,6 +30,16 @@ func NewIteratorWithCap(n *Node, cap int) *Iterator { return iter } +func (iter *Iterator) ToHead() { + for iter.Prev() { + } +} + +func (iter *Iterator) ToTail() { + for iter.Next() { + } +} + func (iter *Iterator) GetNode() *Node { return iter.cur } @@ -44,25 +54,7 @@ func (iter *Iterator) Value() interface{} { return iter.cur.value } -func (iter *Iterator) Left() bool { - if iter.cur.children[0] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[0] - return true - } - return false -} - -func (iter *Iterator) Right() bool { - if iter.cur.children[1] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[1] - return true - } - return false -} - -func (iter *Iterator) GetNext(cur *Node, idx int) *Node { +func (iter *Iterator) GetPrev(cur *Node, idx int) *Node { // iter := NewIterator(cur) iter.SetNode(cur) @@ -93,7 +85,7 @@ func (iter *Iterator) GetNext(cur *Node, idx int) *Node { return cur } -func (iter *Iterator) Next() (result bool) { +func (iter *Iterator) Prev() (result bool) { if iter.dir > -1 { if iter.dir == 1 && iter.cur != nil { @@ -121,7 +113,7 @@ func (iter *Iterator) Next() (result bool) { return false } -func (iter *Iterator) GetPrev(cur *Node, idx int) *Node { +func (iter *Iterator) GetNext(cur *Node, idx int) *Node { // iter := NewIterator(cur) iter.SetNode(cur) @@ -152,7 +144,7 @@ func (iter *Iterator) GetPrev(cur *Node, idx int) *Node { return cur } -func (iter *Iterator) Prev() (result bool) { +func (iter *Iterator) Next() (result bool) { if iter.dir < 1 { // 非 1(next 方向定义 -1 为 prev) if iter.dir == -1 && iter.cur != nil { // 如果上次为prev方向, 则清空辅助计算的栈 diff --git a/priority_queue/priority_queue.go b/priority_queue/priority_queue.go index 400b7d8..ce52fe6 100644 --- a/priority_queue/priority_queue.go +++ b/priority_queue/priority_queue.go @@ -1,6 +1,9 @@ package pqueue -import "github.com/474420502/focus/compare" +import ( + "github.com/474420502/focus/compare" + "github.com/davecgh/go-spew/spew" +) type PriorityQueue struct { queue *vbTree @@ -78,3 +81,7 @@ func (pq *PriorityQueue) Remove(key interface{}) (interface{}, bool) { func (pq *PriorityQueue) Values() []interface{} { return pq.queue.Values() } + +func (pq *PriorityQueue) String() string { + return spew.Sprint(pq.queue.Values()) +} diff --git a/priority_queue/priority_queue_test.go b/priority_queue/priority_queue_test.go index c3bf3f5..b168708 100644 --- a/priority_queue/priority_queue_test.go +++ b/priority_queue/priority_queue_test.go @@ -242,6 +242,35 @@ func TestQueueIndex(t *testing.T) { } } +func TestPriorityQueue_Iterator(t *testing.T) { + pq := New(compare.Int) + for i := 0; i < 5; i++ { + pq.Push(i) + } + + pq.Push(-1) + pq.Push(10) + + result := pq.String() + if result != "[10 4 3 2 1 0 -1]" { + t.Error("should be [10 4 3 2 1 0 -1]") + } + + iter := pq.Iterator() + iter.ToHead() + + values := pq.Values() + for i := 0; ; i++ { + if values[i] != iter.Value() { + t.Error(values[i], " != ", iter.Value()) + } + + if !iter.Next() { + break + } + } +} + // func BenchmarkQueueGet(b *testing.B) { // l := loadTestData() diff --git a/priority_queuekey/iterator.go b/priority_queuekey/iterator.go index 4481862..4b56206 100644 --- a/priority_queuekey/iterator.go +++ b/priority_queuekey/iterator.go @@ -30,6 +30,16 @@ func NewIteratorWithCap(n *Node, cap int) *Iterator { return iter } +func (iter *Iterator) ToHead() { + for iter.Prev() { + } +} + +func (iter *Iterator) ToTail() { + for iter.Next() { + } +} + func (iter *Iterator) GetNode() *Node { return iter.cur } @@ -44,25 +54,7 @@ func (iter *Iterator) Value() interface{} { return iter.cur.value } -func (iter *Iterator) Left() bool { - if iter.cur.children[0] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[0] - return true - } - return false -} - -func (iter *Iterator) Right() bool { - if iter.cur.children[1] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[1] - return true - } - return false -} - -func (iter *Iterator) GetNext(cur *Node, idx int) *Node { +func (iter *Iterator) GetPrev(cur *Node, idx int) *Node { // iter := NewIterator(cur) iter.SetNode(cur) @@ -93,7 +85,7 @@ func (iter *Iterator) GetNext(cur *Node, idx int) *Node { return cur } -func (iter *Iterator) Next() (result bool) { +func (iter *Iterator) Prev() (result bool) { if iter.dir > -1 { if iter.dir == 1 && iter.cur != nil { @@ -121,7 +113,7 @@ func (iter *Iterator) Next() (result bool) { return false } -func (iter *Iterator) GetPrev(cur *Node, idx int) *Node { +func (iter *Iterator) GetNext(cur *Node, idx int) *Node { // iter := NewIterator(cur) iter.SetNode(cur) @@ -152,7 +144,7 @@ func (iter *Iterator) GetPrev(cur *Node, idx int) *Node { return cur } -func (iter *Iterator) Prev() (result bool) { +func (iter *Iterator) Next() (result bool) { if iter.dir < 1 { // 非 1(next 方向定义 -1 为 prev) if iter.dir == -1 && iter.cur != nil { // 如果上次为prev方向, 则清空辅助计算的栈 diff --git a/priority_queuekey/priority_queuekey.go b/priority_queuekey/priority_queuekey.go index 0954043..b3d3779 100644 --- a/priority_queuekey/priority_queuekey.go +++ b/priority_queuekey/priority_queuekey.go @@ -1,6 +1,9 @@ package pqueuekey -import "github.com/474420502/focus/compare" +import ( + "github.com/474420502/focus/compare" + "github.com/davecgh/go-spew/spew" +) type PriorityQueue struct { queue *vbTree @@ -78,3 +81,7 @@ func (pq *PriorityQueue) Remove(key interface{}) (interface{}, bool) { func (pq *PriorityQueue) Values() []interface{} { return pq.queue.Values() } + +func (pq *PriorityQueue) String() string { + return spew.Sprint(pq.queue.Values()) +} diff --git a/priority_queuekey/priority_queuekey_test.go b/priority_queuekey/priority_queuekey_test.go index f91d378..1776d7d 100644 --- a/priority_queuekey/priority_queuekey_test.go +++ b/priority_queuekey/priority_queuekey_test.go @@ -1,11 +1,11 @@ package pqueuekey import ( + "log" "testing" - "github.com/davecgh/go-spew/spew" - "github.com/474420502/focus/compare" + "github.com/davecgh/go-spew/spew" ) func TestQueuePush(t *testing.T) { @@ -345,3 +345,68 @@ func TestQueueIndex(t *testing.T) { // pq.Pop() // } // } + +func TestPriorityQueue_Iterator(t *testing.T) { + pq := New(compare.Int) + for i := 0; i < 5; i++ { + pq.Push(i, i) + } + + pq.Push(-1, -1) + pq.Push(10, 10) + + result := pq.String() + if result != "[10 4 3 2 1 0 -1]" { + t.Error("should be [10 4 3 2 1 0 -1]") + } + + iter := pq.Iterator() + iter.ToHead() + + values := pq.Values() + for i := 0; ; i++ { + if values[i] != iter.Value() { + t.Error(values[i], " != ", iter.Value()) + } + + if !iter.Next() { + break + } + } +} + +func TestMain(t *testing.T) { + pq := New(compare.Int) + pq.Push(1, 1) + pq.Push(4, 4) + pq.Push(5, 5) + pq.Push(6, 6) + pq.Push(2, 2) // pq.Values() = [6 5 4 2 1] + log.Println(pq.Values()) + value, _ := pq.Pop() // value = 6 + log.Println(value) + value, _ = pq.Get(1) // value = 1 pq.Values() = [5 4 2 1] + log.Println(value) + value, _ = pq.Get(0) // value = nil , Get equal to Seach Key + log.Println(value) + value, _ = pq.Index(0) // value = 5, compare.Int the order from big to small + log.Println(value) + values := pq.GetRange(2, 5) // values = [2 4 5] + log.Println(values) + values = pq.GetRange(5, 2) // values = [5 4 2] + log.Println(values) + values = pq.GetRange(100, 2) // values = [5 4 2] + log.Println(values) + values3 := pq.GetAround(5) // values3 = [, 5, 4] + log.Println(values3) + + iter := pq.Iterator() // Next 大到小 从root节点起始 + log.Println(pq.String()) + // log.Println(iter.Value()) 直接使用会报错, + iter.ToHead() + log.Println(iter.Value()) // 起始最大值. true 5 + log.Println(iter.Prev(), iter.Value()) // false 5 + + // Prev 大到小 + log.Println(iter.Next(), iter.Value()) // true 4 +} diff --git a/set/treeset/treeset_test.go b/set/treeset/treeset_test.go index 6e3671b..5c2254a 100644 --- a/set/treeset/treeset_test.go +++ b/set/treeset/treeset_test.go @@ -151,7 +151,29 @@ func TestTreeSet_Iterator(t *testing.T) { set.Add(5, 4, 3, 5) iter := set.Iterator() - for iter.Prev() { + iter.ToHead() + // 3 4 5 + if iter.Value() != 3 { + t.Error(iter.Value()) + } + + iter.Next() + if iter.Value() != 4 { + t.Error(iter.Value()) + } + + iter.Next() + if iter.Value() != 5 { + t.Error(iter.Value()) + } + + iter.ToTail() + if iter.Value() != 5 { + t.Error(iter.Value()) + } + + iter.Prev() + if iter.Value() != 4 { t.Error(iter.Value()) } } diff --git a/tree/avl/avl_test.go b/tree/avl/avl_test.go index 154d5ec..0192966 100644 --- a/tree/avl/avl_test.go +++ b/tree/avl/avl_test.go @@ -22,6 +22,43 @@ func loadTestData() []int { return l } +func TestIteratorHeadTail(t *testing.T) { + tree := New(compare.Int) + for _, v := range []int{1, 2, 7, 4, 5, 6, 7, 14, 15, 20, 30, 21, 3} { + tree.Put(v) + } + // ` AVLTree + // │ ┌── 30 + // │ │ └── 21 + // │ ┌── 20 + // │ │ └── 15 + // └── 14 + // │ ┌── 7 + // │ ┌── 7 + // │ │ └── 6 + // └── 5 + // │ ┌── 4 + // │ │ └── 3 + // └── 2 + // └── 1` + + iter := tree.Iterator() + iter.Prev() + if iter.Value() != 14 { + t.Error("iter.Value() != ", 14, " value =", iter.Value()) + } + + iter.ToHead() + if iter.Value() != 1 { + t.Error("iter.Value() != ", 14, " value =", iter.Value()) + } + + iter.ToTail() + if iter.Value() != 30 { + t.Error("iter.Value() != ", 30, " value =", iter.Value()) + } +} + func TestIterator(t *testing.T) { tree := New(compare.Int) for _, v := range []int{1, 2, 7, 4, 5, 6, 7, 14, 15, 20, 30, 21, 3} { diff --git a/tree/avl/iterator.go b/tree/avl/iterator.go index 6436a96..5884f1d 100644 --- a/tree/avl/iterator.go +++ b/tree/avl/iterator.go @@ -30,6 +30,16 @@ func NewIteratorWithCap(n *Node, cap int) *Iterator { return iter } +func (iter *Iterator) ToHead() { + for iter.Prev() { + } +} + +func (iter *Iterator) ToTail() { + for iter.Next() { + } +} + func (iter *Iterator) GetNode() *Node { return iter.cur } @@ -44,24 +54,6 @@ func (iter *Iterator) Value() interface{} { return iter.cur.value } -func (iter *Iterator) Left() bool { - if iter.cur.children[0] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[0] - return true - } - return false -} - -func (iter *Iterator) Right() bool { - if iter.cur.children[1] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[1] - return true - } - return false -} - func (iter *Iterator) GetNext(cur *Node, idx int) *Node { // iter := NewIterator(cur) diff --git a/tree/avldup/iterator.go b/tree/avldup/iterator.go index 1ef37ea..1ec0716 100644 --- a/tree/avldup/iterator.go +++ b/tree/avldup/iterator.go @@ -30,6 +30,16 @@ func NewIteratorWithCap(n *Node, cap int) *Iterator { return iter } +func (iter *Iterator) ToHead() { + for iter.Prev() { + } +} + +func (iter *Iterator) ToTail() { + for iter.Next() { + } +} + func (iter *Iterator) GetNode() *Node { return iter.cur } @@ -44,24 +54,6 @@ func (iter *Iterator) Value() interface{} { return iter.cur.value } -func (iter *Iterator) Left() bool { - if iter.cur.children[0] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[0] - return true - } - return false -} - -func (iter *Iterator) Right() bool { - if iter.cur.children[1] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[1] - return true - } - return false -} - func (iter *Iterator) GetNext(cur *Node, idx int) *Node { // iter := NewIterator(cur) diff --git a/tree/avlkey/iterator.go b/tree/avlkey/iterator.go index 1de0bc2..ac6306b 100644 --- a/tree/avlkey/iterator.go +++ b/tree/avlkey/iterator.go @@ -30,6 +30,16 @@ func NewIteratorWithCap(n *Node, cap int) *Iterator { return iter } +func (iter *Iterator) ToHead() { + for iter.Prev() { + } +} + +func (iter *Iterator) ToTail() { + for iter.Next() { + } +} + func (iter *Iterator) GetNode() *Node { return iter.cur } @@ -44,24 +54,6 @@ func (iter *Iterator) Value() interface{} { return iter.cur.value } -func (iter *Iterator) Left() bool { - if iter.cur.children[0] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[0] - return true - } - return false -} - -func (iter *Iterator) Right() bool { - if iter.cur.children[1] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[1] - return true - } - return false -} - func (iter *Iterator) GetNext(cur *Node, idx int) *Node { // iter := NewIterator(cur) diff --git a/tree/avlkeydup/iterator.go b/tree/avlkeydup/iterator.go index 4369589..6511ee2 100644 --- a/tree/avlkeydup/iterator.go +++ b/tree/avlkeydup/iterator.go @@ -30,6 +30,16 @@ func NewIteratorWithCap(n *Node, cap int) *Iterator { return iter } +func (iter *Iterator) ToHead() { + for iter.Prev() { + } +} + +func (iter *Iterator) ToTail() { + for iter.Next() { + } +} + func (iter *Iterator) GetNode() *Node { return iter.cur } @@ -44,24 +54,6 @@ func (iter *Iterator) Value() interface{} { return iter.cur.value } -func (iter *Iterator) Left() bool { - if iter.cur.children[0] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[0] - return true - } - return false -} - -func (iter *Iterator) Right() bool { - if iter.cur.children[1] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[1] - return true - } - return false -} - func (iter *Iterator) GetNext(cur *Node, idx int) *Node { // iter := NewIterator(cur) diff --git a/tree/vbt/iterator.go b/tree/vbt/iterator.go index 3a09be1..3afdf0a 100644 --- a/tree/vbt/iterator.go +++ b/tree/vbt/iterator.go @@ -30,6 +30,16 @@ func NewIteratorWithCap(n *Node, cap int) *Iterator { return iter } +func (iter *Iterator) ToHead() { + for iter.Prev() { + } +} + +func (iter *Iterator) ToTail() { + for iter.Next() { + } +} + func (iter *Iterator) GetNode() *Node { return iter.cur } @@ -44,24 +54,6 @@ func (iter *Iterator) Value() interface{} { return iter.cur.value } -func (iter *Iterator) Left() bool { - if iter.cur.children[0] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[0] - return true - } - return false -} - -func (iter *Iterator) Right() bool { - if iter.cur.children[1] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[1] - return true - } - return false -} - func (iter *Iterator) GetNext(cur *Node, idx int) *Node { // iter := NewIterator(cur) diff --git a/tree/vbtkey/iterator.go b/tree/vbtkey/iterator.go index 2d7acc4..1cd3d31 100644 --- a/tree/vbtkey/iterator.go +++ b/tree/vbtkey/iterator.go @@ -34,6 +34,16 @@ func (iter *Iterator) GetNode() *Node { return iter.cur } +func (iter *Iterator) ToHead() { + for iter.Prev() { + } +} + +func (iter *Iterator) ToTail() { + for iter.Next() { + } +} + func (iter *Iterator) SetNode(n *Node) { iter.up = n iter.dir = 0 @@ -44,24 +54,6 @@ func (iter *Iterator) Value() interface{} { return iter.cur.value } -func (iter *Iterator) Left() bool { - if iter.cur.children[0] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[0] - return true - } - return false -} - -func (iter *Iterator) Right() bool { - if iter.cur.children[1] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[1] - return true - } - return false -} - func (iter *Iterator) GetNext(cur *Node, idx int) *Node { // iter := NewIterator(cur) From 05bc567249d8a363b9c246d172143e9bf3c281cb Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Mon, 22 Jul 2019 02:34:57 +0800 Subject: [PATCH 3/4] =?UTF-8?q?TODO:=20=E9=AA=8C=E8=AF=81GetNext=20GetPrev?= =?UTF-8?q?=20=E6=98=AF=E5=90=A6=E6=AD=A3=E7=A1=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- priority_queue/vbt.go | 2 +- priority_queuekey/priority_queuekey_test.go | 1 + priority_queuekey/vbt.go | 35 +++++++++++---------- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 4bd7dc9..812a477 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ func main() { values3 := pq.GetAround(5) // values3 = [, 5, 4] log.Println(values3) - iter := pq.Iterator() // Next 小到大 从root节点起始 + iter := pq.Iterator() // Next 大到小 从root节点起始 // log.Println(iter.Value()) 直接使用会报错, iter.ToHead() log.Println(iter.Value()) // 起始最大值. true 5 diff --git a/priority_queue/vbt.go b/priority_queue/vbt.go index 0808565..62d51bd 100644 --- a/priority_queue/vbt.go +++ b/priority_queue/vbt.go @@ -174,7 +174,7 @@ func (tree *vbTree) removeNode(n *Node) { } if tree.top == n { - tree.top = tree.iter.GetPrev(n, 1) + tree.top = tree.iter.GetNext(n, 1) } ls, rs := getChildrenSize(n) diff --git a/priority_queuekey/priority_queuekey_test.go b/priority_queuekey/priority_queuekey_test.go index 1776d7d..faa8d9f 100644 --- a/priority_queuekey/priority_queuekey_test.go +++ b/priority_queuekey/priority_queuekey_test.go @@ -409,4 +409,5 @@ func TestMain(t *testing.T) { // Prev 大到小 log.Println(iter.Next(), iter.Value()) // true 4 + } diff --git a/priority_queuekey/vbt.go b/priority_queuekey/vbt.go index 45b1c6f..b3caedd 100644 --- a/priority_queuekey/vbt.go +++ b/priority_queuekey/vbt.go @@ -24,6 +24,7 @@ func (n *Node) String() string { return spew.Sprint(n.value) + "(" + p + "|" + spew.Sprint(n.size) + ")" } +// 已经修改过GetNext GetPrev逻辑与avl相反 type vbTree struct { root *Node Compare compare.Compare @@ -118,7 +119,7 @@ func (tree *vbTree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { iter := tree.iter result = make([]interface{}, 0, idx1-idx2) for i := idx2; i <= idx1; i++ { - if iter.Next() { + if iter.Prev() { result = append(result, iter.Value()) } else { ok = false @@ -140,7 +141,7 @@ func (tree *vbTree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { iter := tree.iter result = make([]interface{}, 0, idx2-idx1) for i := idx1; i <= idx2; i++ { - if iter.Prev() { + if iter.Next() { result = append(result, iter.Value()) } else { ok = false @@ -174,7 +175,7 @@ func (tree *vbTree) removeNode(n *Node) { } if tree.top == n { - tree.top = tree.iter.GetPrev(n, 1) + tree.top = tree.iter.GetNext(n, 1) } ls, rs := getChildrenSize(n) @@ -278,7 +279,7 @@ func (tree *vbTree) GetRange(k1, k2 interface{}) (result []interface{}) { // iter := NewIterator(min) tree.iter.SetNode(min) iter := tree.iter - for iter.Next() { + for iter.Prev() { result = append(result, iter.Value()) if iter.cur == max { break @@ -305,7 +306,7 @@ func (tree *vbTree) GetRange(k1, k2 interface{}) (result []interface{}) { // iter := NewIterator(max) tree.iter.SetNode(max) iter := tree.iter - for iter.Prev() { + for iter.Next() { result = append(result, iter.Value()) if iter.cur == min { break @@ -357,8 +358,8 @@ func (tree *vbTree) getArounNode(key interface{}) (result [3]*Node) { tree.iter.SetNode(n) iter := tree.iter - iter.Prev() - for iter.Prev() { + iter.Next() + for iter.Next() { if tree.Compare(iter.cur.key, n.key) == 0 { n = iter.cur } else { @@ -377,21 +378,21 @@ func (tree *vbTree) getArounNode(key interface{}) (result [3]*Node) { if result[1] != nil { - result[0] = tree.iter.GetPrev(result[1], 1) - result[2] = tree.iter.GetNext(result[1], 1) + result[0] = tree.iter.GetNext(result[1], 1) + result[2] = tree.iter.GetPrev(result[1], 1) } else { result[0] = last - result[2] = tree.iter.GetNext(last, 1) + result[2] = tree.iter.GetPrev(last, 1) } case -1: if result[1] != nil { - result[0] = tree.iter.GetPrev(result[1], 1) - result[2] = tree.iter.GetNext(result[1], 1) + result[0] = tree.iter.GetNext(result[1], 1) + result[2] = tree.iter.GetPrev(result[1], 1) } else { result[2] = last - result[0] = tree.iter.GetPrev(last, 1) + result[0] = tree.iter.GetNext(last, 1) } case 0: @@ -399,8 +400,8 @@ func (tree *vbTree) getArounNode(key interface{}) (result [3]*Node) { if result[1] == nil { return } - result[0] = tree.iter.GetPrev(result[1], 1) - result[2] = tree.iter.GetNext(result[1], 1) + result[0] = tree.iter.GetNext(result[1], 1) + result[2] = tree.iter.GetPrev(result[1], 1) } return } @@ -417,8 +418,8 @@ func (tree *vbTree) GetNode(key interface{}) (*Node, bool) { tree.iter.SetNode(n) iter := tree.iter - iter.Prev() - for iter.Prev() { + iter.Next() + for iter.Next() { if tree.Compare(iter.cur.key, n.key) == 0 { n = iter.cur } else { From 60c1442dca2d00a5b75e1840fdf4521d2dfcf00c Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Mon, 22 Jul 2019 03:28:27 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E9=80=9A=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- priority_queue/priority_queue_test.go | 57 ++++++++++++++++++++- priority_queue/vbt.go | 32 ++++++------ priority_queuekey/priority_queuekey_test.go | 55 ++++++++++++++++++++ priority_queuekey/vbt.go | 2 +- 4 files changed, 128 insertions(+), 18 deletions(-) diff --git a/priority_queue/priority_queue_test.go b/priority_queue/priority_queue_test.go index b168708..d096bdf 100644 --- a/priority_queue/priority_queue_test.go +++ b/priority_queue/priority_queue_test.go @@ -265,12 +265,67 @@ func TestPriorityQueue_Iterator(t *testing.T) { t.Error(values[i], " != ", iter.Value()) } - if !iter.Next() { + if !iter.Prev() { break } } } +func TestPriorityQueue_Iterator2(t *testing.T) { + pq := New(compare.Int) + for i := 0; i < 5; i++ { + pq.Push(i) + } + + iter := pq.Iterator() + iter.ToHead() + + n, _ := pq.IndexNode(0) + if n.value != 4 { + t.Error(n) + } + + if v, _ := pq.Top(); v != 4 { + t.Error("Top != 4, and is ", v) + } + + if v := iter.GetNext(n, 2).value; v != 2 { + t.Error("iter.GetNext(n, 2) != 2, and is ", v) + } + + pq = New(compare.Int) + for i := 100; i >= 0; i-- { + pq.Push(i) + } + if v, _ := pq.Top(); v != 100 { + t.Error("Top != 100, and is ", v) + } + + for pq.Size() >= 50 { + pq.Pop() + } + + if v, _ := pq.Top(); v != 48 { + t.Error("Top != 48, and is ", v) + } + + pq = New(compare.Int) + for i := 0; i < 100; i++ { + pq.Push(i) + } + if v, _ := pq.Top(); v != 99 { + t.Error("Top != 99, and is ", v) + } + + for pq.Size() >= 50 { + pq.Pop() + } + + if v, _ := pq.Top(); v != 48 { + t.Error("Top != 49, and is ", v) + } +} + // func BenchmarkQueueGet(b *testing.B) { // l := loadTestData() diff --git a/priority_queue/vbt.go b/priority_queue/vbt.go index 62d51bd..c9bcd78 100644 --- a/priority_queue/vbt.go +++ b/priority_queue/vbt.go @@ -118,7 +118,7 @@ func (tree *vbTree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { iter := tree.iter result = make([]interface{}, 0, idx1-idx2) for i := idx2; i <= idx1; i++ { - if iter.Next() { + if iter.Prev() { result = append(result, iter.Value()) } else { ok = false @@ -140,7 +140,7 @@ func (tree *vbTree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { iter := tree.iter result = make([]interface{}, 0, idx2-idx1) for i := idx1; i <= idx2; i++ { - if iter.Prev() { + if iter.Next() { result = append(result, iter.Value()) } else { ok = false @@ -278,7 +278,7 @@ func (tree *vbTree) GetRange(k1, k2 interface{}) (result []interface{}) { // iter := NewIterator(min) tree.iter.SetNode(min) iter := tree.iter - for iter.Next() { + for iter.Prev() { result = append(result, iter.Value()) if iter.cur == max { break @@ -305,7 +305,7 @@ func (tree *vbTree) GetRange(k1, k2 interface{}) (result []interface{}) { // iter := NewIterator(max) tree.iter.SetNode(max) iter := tree.iter - for iter.Prev() { + for iter.Next() { result = append(result, iter.Value()) if iter.cur == min { break @@ -358,8 +358,8 @@ func (tree *vbTree) getArounNode(key interface{}) (result [3]*Node) { tree.iter.SetNode(n) iter := tree.iter - iter.Prev() - for iter.Prev() { + iter.Next() + for iter.Next() { if tree.Compare(iter.cur.value, n.value) == 0 { n = iter.cur } else { @@ -378,21 +378,21 @@ func (tree *vbTree) getArounNode(key interface{}) (result [3]*Node) { if result[1] != nil { - result[0] = tree.iter.GetPrev(result[1], 1) - result[2] = tree.iter.GetNext(result[1], 1) + result[0] = tree.iter.GetNext(result[1], 1) + result[2] = tree.iter.GetPrev(result[1], 1) } else { result[0] = last - result[2] = tree.iter.GetNext(last, 1) + result[2] = tree.iter.GetPrev(last, 1) } case -1: if result[1] != nil { - result[0] = tree.iter.GetPrev(result[1], 1) - result[2] = tree.iter.GetNext(result[1], 1) + result[0] = tree.iter.GetNext(result[1], 1) + result[2] = tree.iter.GetPrev(result[1], 1) } else { result[2] = last - result[0] = tree.iter.GetPrev(last, 1) + result[0] = tree.iter.GetNext(last, 1) } case 0: @@ -400,8 +400,8 @@ func (tree *vbTree) getArounNode(key interface{}) (result [3]*Node) { if result[1] == nil { return } - result[0] = tree.iter.GetPrev(result[1], 1) - result[2] = tree.iter.GetNext(result[1], 1) + result[0] = tree.iter.GetNext(result[1], 1) + result[2] = tree.iter.GetPrev(result[1], 1) } return } @@ -418,8 +418,8 @@ func (tree *vbTree) GetNode(value interface{}) (*Node, bool) { tree.iter.SetNode(n) iter := tree.iter - iter.Prev() - for iter.Prev() { + iter.Next() + for iter.Next() { if tree.Compare(iter.cur.value, n.value) == 0 { n = iter.cur } else { diff --git a/priority_queuekey/priority_queuekey_test.go b/priority_queuekey/priority_queuekey_test.go index faa8d9f..5b9b538 100644 --- a/priority_queuekey/priority_queuekey_test.go +++ b/priority_queuekey/priority_queuekey_test.go @@ -375,6 +375,61 @@ func TestPriorityQueue_Iterator(t *testing.T) { } } +func TestPriorityQueue_Iterator2(t *testing.T) { + pq := New(compare.Int) + for i := 0; i < 5; i++ { + pq.Push(i, i) + } + + iter := pq.Iterator() + iter.ToHead() + + n, _ := pq.IndexNode(0) + if n.value != 4 { + t.Error(n) + } + + if v, _ := pq.Top(); v != 4 { + t.Error("Top != 4, and is ", v) + } + + if v := iter.GetNext(n, 2).value; v != 2 { + t.Error("iter.GetNext(n, 2) != 2, and is ", v) + } + + pq = New(compare.Int) + for i := 100; i >= 0; i-- { + pq.Push(i, i) + } + if v, _ := pq.Top(); v != 100 { + t.Error("Top != 100, and is ", v) + } + + for pq.Size() >= 50 { + pq.Pop() + } + + if v, _ := pq.Top(); v != 48 { + t.Error("Top != 48, and is ", v) + } + + pq = New(compare.Int) + for i := 0; i < 100; i++ { + pq.Push(i, i) + } + if v, _ := pq.Top(); v != 99 { + t.Error("Top != 99, and is ", v) + } + + for pq.Size() >= 50 { + pq.Pop() + } + + if v, _ := pq.Top(); v != 48 { + t.Error("Top != 49, and is ", v) + } +} + func TestMain(t *testing.T) { pq := New(compare.Int) pq.Push(1, 1) diff --git a/priority_queuekey/vbt.go b/priority_queuekey/vbt.go index b3caedd..27fa641 100644 --- a/priority_queuekey/vbt.go +++ b/priority_queuekey/vbt.go @@ -175,7 +175,7 @@ func (tree *vbTree) removeNode(n *Node) { } if tree.top == n { - tree.top = tree.iter.GetNext(n, 1) + tree.top = tree.iter.GetNext(n, 1) // 最大值 Next一位后为下个最大值 } ls, rs := getChildrenSize(n)