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 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法