英文:
Overlapping in treap package from stathat?
问题
根据这个链接,stathat在他们的treap中使用了重叠:
> GoLLRB很棒,没有理由你要换。我们认为treap背后的思想是我们问题的一个优雅解决方案,所以我们实现了它。我们喜欢GoLLRB提供的接口,所以我们在我们的实现中模仿了它。
>
> 我们在treap包中添加了一个允许你使用重叠函数进行迭代的功能,这样你就可以获取[3,9)范围内的所有键,例如。我们经常使用这个功能,通常使用结构体作为键。
>
> Patrick
我正在尝试以下代码,但不知道如何继续:
package main
import(
"reflect"
"fmt"
"github.com/stathat/treap"
)
func IntLess(p, q interface{}) bool {
return p.(int) < q.(int)
}
func BucketOverlap(a, b interface{}) bool {
return false
}
func main() {
tree := treap.NewOverlapTree(IntLess, BucketOverlap)
tree.Insert(5, "a")
tree.Insert(7, "b")
tree.Insert(2, "c")
tree.Insert(1, "d")
for v := range tree.IterateOverlap([]int{2,5}) {
fmt.Printf("val: %v\n", v)
}
}
假设我想获取范围为[2,5]
的键 => [c,a]
英文:
According to this link stathat uses overlapping with their treap:
> GoLLRB is great and there's no reason you should switch. We thought
> the idea behind treaps was an elegant solution to our problem, so we
> implemented it. We liked the interface that GoLLRB provided, so we
> mimicked it in our implementation.
>
> One thing we added to the treap package is to allow you to iterate
> using an overlap function, so you can get all the keys in [3,9), for
> example. We use this a lot, often with a struct as the key.
>
> Patrick
I am playing with the following code and have no idea how to continue:
package main
import(
"reflect"
"fmt"
"github.com/stathat/treap"
)
func IntLess(p, q interface{}) bool {
return p.(int) < q.(int)
}
func BucketOverlap(a, b interface{}) bool {
return false
}
func main() {
tree := treap.NewOverlapTree(IntLess, BucketOverlap)
tree.Insert(5, "a")
tree.Insert(7, "b")
tree.Insert(2, "c")
tree.Insert(1, "d")
for v := range tree.IterateOverlap([]int{2,5}) {
fmt.Printf("val: %v\n", v)
}
}
let's say I want to get keys in range [2,5]
=> [c,a]
答案1
得分: 1
我会从stathat treap代码的测试开始:
https://github.com/stathat/treap/blob/master/treap_test.go#L164
看起来你正在尝试传递一个键的切片,但它期望的是一个单独的键。你还试图对一个标量值(即int)进行向量操作(即范围重叠)。
也许我对重叠的目的有误解,但我理解它的用途是作为一个区间树:
key1 := []int{1, 3}
key2 := []int{2, 4}
key3 := []int{5, 6}
这些是区间(低和高)。key1与key2重叠,反之亦然。key3与两者都不重叠。在这种情况下,重叠将是有用的(即IterateOverlap([]int{2,3})
将给我返回key1和key2,而IterateOverlap([]int{3,5})
将返回所有)。
我不确定如何迭代这些条目。也许可以这样做:
for i := 2; i <= 5; i++ {
fmt.Printf("val: %v\n", tree.Get(i))
}
再次说明,我没有使用过这个实现,所以如果我弄错了,请原谅我。
英文:
The first place I'd start would be the tests for the stathat treap code:
https://github.com/stathat/treap/blob/master/treap_test.go#L164
It seems that what you're doing is trying to pass a slice of keys when it is expecting a single one. You are also trying to do vector operations (i.e. range overlap) on a value that is scalar (i.e. int).
Maybe I am misunderstanding the point of the overlap, but my understanding is that the use for it is as an interval tree:
key1 := []int{1, 3}
key2 := []int{2, 4}
key3 := []int{5, 6}
These are intervals (low and high). key1 overlaps key2, and vice-versa. Neither overlap key3. In this case, the overlap would be useful (i.e. IterateOverlap([]int{2,3})
would give me key1 and key2, whereas IterateOverlap([]int{3,5})
would return all).
I'm not sure how you'd iterate over these entries. Maybe this:
for i := 2; i <= 5; i++ {
fmt.Printf("val: %v\n", tree.Get(i))
}
Again, I've not used this implementation, so forgive me if I'm barking up the wrong tree.
答案2
得分: 0
我已经找到了使用GoLLRB的解决方案:
package main
import (
"fmt"
"github.com/petar/GoLLRB/llrb"
)
type Item struct {
key int
value string
}
func lessInt(a, b interface{}) bool {
aa := a.(*Item)
bb := b.(*Item)
return aa.key < bb.key
}
func main() {
tree := llrb.New(lessInt)
tree.ReplaceOrInsert(&Item{5, "a"})
tree.ReplaceOrInsert(&Item{7, "b"})
tree.ReplaceOrInsert(&Item{2, "c"})
tree.ReplaceOrInsert(&Item{1, "d"})
//tree.DeleteMin()
c := tree.IterRangeInclusive(&Item{key: 2}, &Item{key: 5})
for item := <-c; item != nil; item = <-c {
i := item.(*Item)
fmt.Printf("%s\n", i.value)
}
}
我还在想是否也可以使用stathat的treap来实现这个。
英文:
I have found a solution using GoLLRB:
package main
import (
"fmt"
"github.com/petar/GoLLRB/llrb"
)
type Item struct {
key int
value string
}
func lessInt(a, b interface{}) bool {
aa := a.(*Item)
bb := b.(*Item)
return aa.key < bb.key
}
func main() {
tree := llrb.New(lessInt)
tree.ReplaceOrInsert(&Item{5, "a"})
tree.ReplaceOrInsert(&Item{7, "b"})
tree.ReplaceOrInsert(&Item{2, "c"})
tree.ReplaceOrInsert(&Item{1, "d"})
//tree.DeleteMin()
c := tree.IterRangeInclusive(&Item{key: 2}, &Item{key: 5})
for item := <-c; item != nil; item = <-c {
i := item.(*Item)
fmt.Printf("%s\n", i.value)
}
}
Still I am wondering if this is also possible using stathat's treap.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论