diff --git a/README.md b/README.md index ee47fe6..a6036b4 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,16 @@ ``` golang pq := pqueuekey.New(compare.Int) pq.Push(1, 1) -pq.Push(2, 2) -v = pq.Pop() // v = 2 +pq.Push(4, 4) +pq.Push(5, 5) +pq.Push(6, 6) +pq.Push(2, 2) // pq.Values() = [6 5 4 2 1] +value, _ := pq.Pop() // value = 6 +t.Error(value) +value, _ = pq.Get(1) // value = 1 pq.Values() = [5 4 2 1] +value, _ = pq.Get(0) // value = nil , Get equal to Seach Key +value, _ = pq.Index(0) // value = 5, compare.Int the order from big to small +values := pq.GetRange(2, 5) // values = [2 4 5] +values = pq.GetRange(5, 2) // values = [5 4 2] +values3 := pq.GetAround(5) // values3 = [, 5, 4] ``` diff --git a/priority_queue/priority_queue_test.go b/priority_queue/priority_queue_test.go index dd8abd3..c3bf3f5 100644 --- a/priority_queue/priority_queue_test.go +++ b/priority_queue/priority_queue_test.go @@ -122,32 +122,32 @@ func TestQueueGetAround(t *testing.T) { var result string result = spew.Sprint(pq.GetAround(53)) - if result != "[32 53 78]" { + if result != "[78 53 32]" { t.Error(result) } result = spew.Sprint(pq.GetAround(52)) - if result != "[32 53]" { + if result != "[53 32]" { t.Error(result) } result = spew.Sprint(pq.GetAround(1)) - if result != "[ 1 4]" { + if result != "[4 1 ]" { t.Error(result) } result = spew.Sprint(pq.GetAround(90)) - if result != "[78 90 ]" { + if result != "[ 90 78]" { t.Error(result) } result = spew.Sprint(pq.GetAround(0)) - if result != "[ 1]" { + if result != "[1 ]" { t.Error(result) } result = spew.Sprint(pq.GetAround(100)) - if result != "[90 ]" { + if result != "[ 90]" { t.Error(result) } } diff --git a/priority_queue/vbt.go b/priority_queue/vbt.go index 0a116df..e0c96eb 100644 --- a/priority_queue/vbt.go +++ b/priority_queue/vbt.go @@ -273,7 +273,7 @@ func (tree *vbTree) GetRange(k1, k2 interface{}) (result []interface{}) { return []interface{}{} } - result = make([]interface{}, 0, 16) + result = make([]interface{}, 0, 8) // iter := NewIterator(min) tree.iter.SeNode(min) @@ -300,7 +300,7 @@ func (tree *vbTree) GetRange(k1, k2 interface{}) (result []interface{}) { return []interface{}{} } - result = make([]interface{}, 0, 16) + result = make([]interface{}, 0, 8) // iter := NewIterator(max) tree.iter.SeNode(max) @@ -329,11 +329,12 @@ func (tree *vbTree) Get(key interface{}) (interface{}, bool) { return n, false } +// GetAround 改成Big To Small func (tree *vbTree) GetAround(key interface{}) (result [3]interface{}) { an := tree.getArounNode(key) for i, n := range an { if n != nil { - result[i] = n.value + result[2-i] = n.value } } return diff --git a/priority_queue/vbt_test.go b/priority_queue/vbt_test.go index af2e593..970f5f2 100644 --- a/priority_queue/vbt_test.go +++ b/priority_queue/vbt_test.go @@ -93,56 +93,56 @@ func TestGetAround(t *testing.T) { var Result string Result = spew.Sprint(tree.GetAround(17)) - if Result != "[16 17 20]" { + if Result != "[20 17 16]" { t.Error(tree.Values()) t.Error("17 is root, tree.GetAround(17)) is error", Result) t.Error(tree.debugString()) } Result = spew.Sprint(tree.GetAround(3)) - if Result != "[ 3 7]" { + if Result != "[7 3 ]" { t.Error(tree.Values()) t.Error("tree.GetAround(3)) is error", Result) t.Error(tree.debugString()) } Result = spew.Sprint(tree.GetAround(40)) - if Result != "[30 40 40]" { + if Result != "[40 40 30]" { t.Error(tree.Values()) t.Error("tree.GetAround(40)) is error", Result) t.Error(tree.debugString()) } Result = spew.Sprint(tree.GetAround(50)) - if Result != "[40 50 ]" { + if Result != "[ 50 40]" { t.Error(tree.Values()) t.Error("tree.GetAround(50)) is error", Result) t.Error(tree.debugString()) } Result = spew.Sprint(tree.GetAround(18)) - if Result != "[17 20]" { + if Result != "[20 17]" { t.Error(tree.Values()) t.Error("18 is not in list, tree.GetAround(18)) is error", Result) t.Error(tree.debugString()) } Result = spew.Sprint(tree.GetAround(5)) - if Result != "[3 7]" { + if Result != "[7 3]" { t.Error(tree.Values()) t.Error("5 is not in list, tree.GetAround(5)) is error", Result) t.Error(tree.debugString()) } Result = spew.Sprint(tree.GetAround(2)) - if Result != "[ 3]" { + if Result != "[3 ]" { t.Error(tree.Values()) t.Error("2 is not in list, tree.GetAround(2)) is error", Result) t.Error(tree.debugString()) } Result = spew.Sprint(tree.GetAround(100)) - if Result != "[50 ]" { + if Result != "[ 50]" { t.Error(tree.Values()) t.Error("50 is not in list, tree.GetAround(50)) is error", Result) t.Error(tree.debugString()) diff --git a/priority_queuekey/priority_queuekey_test.go b/priority_queuekey/priority_queuekey_test.go index 8a242b5..f91d378 100644 --- a/priority_queuekey/priority_queuekey_test.go +++ b/priority_queuekey/priority_queuekey_test.go @@ -122,32 +122,32 @@ func TestQueueGetAround(t *testing.T) { var result string result = spew.Sprint(pq.GetAround(53)) - if result != "[32 53 78]" { + if result != "[78 53 32]" { t.Error(result) } result = spew.Sprint(pq.GetAround(52)) - if result != "[32 53]" { + if result != "[53 32]" { t.Error(result) } result = spew.Sprint(pq.GetAround(1)) - if result != "[ 1 4]" { + if result != "[4 1 ]" { t.Error(result) } result = spew.Sprint(pq.GetAround(90)) - if result != "[78 90 ]" { + if result != "[ 90 78]" { t.Error(result) } result = spew.Sprint(pq.GetAround(0)) - if result != "[ 1]" { + if result != "[1 ]" { t.Error(result) } result = spew.Sprint(pq.GetAround(100)) - if result != "[90 ]" { + if result != "[ 90]" { t.Error(result) } } diff --git a/priority_queuekey/vbt.go b/priority_queuekey/vbt.go index 802be1c..70cc6c7 100644 --- a/priority_queuekey/vbt.go +++ b/priority_queuekey/vbt.go @@ -273,7 +273,7 @@ func (tree *vbTree) GetRange(k1, k2 interface{}) (result []interface{}) { return []interface{}{} } - result = make([]interface{}, 0, 16) + result = make([]interface{}, 0, 8) // iter := NewIterator(min) tree.iter.SeNode(min) @@ -300,7 +300,7 @@ func (tree *vbTree) GetRange(k1, k2 interface{}) (result []interface{}) { return []interface{}{} } - result = make([]interface{}, 0, 16) + result = make([]interface{}, 0, 8) // iter := NewIterator(max) tree.iter.SeNode(max) @@ -333,7 +333,7 @@ func (tree *vbTree) GetAround(key interface{}) (result [3]interface{}) { an := tree.getArounNode(key) for i, n := range an { if n != nil { - result[i] = n.value + result[2-i] = n.value } } return diff --git a/priority_queuekey/vbt_test.go b/priority_queuekey/vbt_test.go index 4c8f671..a1bdcda 100644 --- a/priority_queuekey/vbt_test.go +++ b/priority_queuekey/vbt_test.go @@ -93,56 +93,56 @@ func TestGetAround(t *testing.T) { var Result string Result = spew.Sprint(tree.GetAround(17)) - if Result != "[16 17 20]" { + if Result != "[20 17 16]" { t.Error(tree.Values()) t.Error("17 is root, tree.GetAround(17)) is error", Result) t.Error(tree.debugString()) } Result = spew.Sprint(tree.GetAround(3)) - if Result != "[ 3 7]" { + if Result != "[7 3 ]" { t.Error(tree.Values()) t.Error("tree.GetAround(3)) is error", Result) t.Error(tree.debugString()) } Result = spew.Sprint(tree.GetAround(40)) - if Result != "[30 40 40]" { + if Result != "[40 40 30]" { t.Error(tree.Values()) t.Error("tree.GetAround(40)) is error", Result) t.Error(tree.debugString()) } Result = spew.Sprint(tree.GetAround(50)) - if Result != "[40 50 ]" { + if Result != "[ 50 40]" { t.Error(tree.Values()) t.Error("tree.GetAround(50)) is error", Result) t.Error(tree.debugString()) } Result = spew.Sprint(tree.GetAround(18)) - if Result != "[17 20]" { + if Result != "[20 17]" { t.Error(tree.Values()) t.Error("18 is not in list, tree.GetAround(18)) is error", Result) t.Error(tree.debugString()) } Result = spew.Sprint(tree.GetAround(5)) - if Result != "[3 7]" { + if Result != "[7 3]" { t.Error(tree.Values()) t.Error("5 is not in list, tree.GetAround(5)) is error", Result) t.Error(tree.debugString()) } Result = spew.Sprint(tree.GetAround(2)) - if Result != "[ 3]" { + if Result != "[3 ]" { t.Error(tree.Values()) t.Error("2 is not in list, tree.GetAround(2)) is error", Result) t.Error(tree.debugString()) } Result = spew.Sprint(tree.GetAround(100)) - if Result != "[50 ]" { + if Result != "[ 50]" { t.Error(tree.Values()) t.Error("50 is not in list, tree.GetAround(50)) is error", Result) t.Error(tree.debugString())