Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Priority problem #3

Closed
icexin opened this issue Aug 7, 2015 · 1 comment
Closed

Priority problem #3

icexin opened this issue Aug 7, 2015 · 1 comment

Comments

@icexin
Copy link

icexin commented Aug 7, 2015

type Item struct {
    Key string
    Pri int
}

func itemCompare(a, b interface{}) int {
    return strings.Compare(a.(Item).Key, b.(Item).Key)
}

func main() {
    s := gtreap.NewTreap(itemCompare)
    s = s.Upsert(Item{"m", 20}, 20)
    s = s.Upsert(Item{"l", 18}, 18)
    s = s.Upsert(Item{"n", 19}, 19)
    s = s.Upsert(Item{"m", 4}, 4)
    s.VisitAscend(Item{"", 0}, func(i gtreap.Item) bool {
        item := i.(Item)
        fmt.Printf("%s %d\n", item.Key, item.Pri)
        return true
    })

}

It prints

l 18
m 4
n 19

m as root node with priority 4 is lower than it's sons.
The reason is in https://github.com/steveyen/gtreap/blob/master/treap.go#L86
When middle is not nil, use middle's priority as result node's priority
Always use this.priority should get the correct answer.

@steveyen
Copy link
Owner

steveyen commented Aug 7, 2015

Thanks! Fixed in commit 0abe01e

@steveyen steveyen closed this as completed Aug 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants