Skip to content

Commit

Permalink
take rand.Uint32 for heap priority
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Jan 20, 2023
1 parent dce1bcd commit ccb2de2
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (t Tree[T]) FprintBST(w io.Writer) error {
// preorderStringify, traverse the tree, stringify the nodes in preorder
func (n *node[T]) preorderStringify(w io.Writer, pad string) error {
// stringify this node
if _, err := fmt.Fprintf(w, "%v [prio:%.4g] [%p|l:%p|r:%p]\n", n.item, n.prio, n, n.left, n.right); err != nil {
if _, err := fmt.Fprintf(w, "%v [prio:%.4g] [%p|l:%p|r:%p]\n", n.item, float64(n.prio)/math.MaxUint32, n, n.left, n.right); err != nil {
return err
}

Expand Down
6 changes: 3 additions & 3 deletions treap.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type node[T Interface[T]] struct {
// base treap fields, in memory efficient order
left *node[T]
right *node[T]
prio float64 // random key for binary heap, balances the tree
item T // generic key/value
prio uint32 // random key for binary heap, balances the tree
item T // generic key/value
}

// Tree is the public handle to the hidden implementation.
Expand All @@ -46,7 +46,7 @@ func NewTree[T Interface[T]](items ...T) Tree[T] {
func makeNode[T Interface[T]](item T) *node[T] {
n := new(node[T])
n.item = item
n.prio = rand.Float64()
n.prio = rand.Uint32()
n.recalc() // initial calculation of finger pointers...

return n
Expand Down

0 comments on commit ccb2de2

Please sign in to comment.