douchuitang0642 2013-04-18 16:50
浏览 17
已采纳

stathat中的treap包重叠?

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
", v)
    }
}

let's say I want to get keys in range [2,5] => [c,a]

  • 写回答

2条回答 默认 最新

  • dongluan1743 2013-04-19 04:27
    关注

    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
    ", i.value)
        }
    }
    

    Still I am wondering if this is also possible using stathat's treap.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用
  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启