func hash(key int) int { u64 := uint64(key) u64 ^= 0x_43ec_4853_c7e7_fa78 u64 *= 0x_c4ce_b9fe_1a85_ec53 return int(u64) } type node struct { lt *node gt *node key int } type Treap struct { root *node } func (t *Treap) Lookup(key int) bool { for curr := t.root; curr != nil; { switch { case key < curr.key: curr = curr.lt case key > curr.key: curr = curr.gt default: return true } } return false } func (t *Treap) Insert(key int) bool { hash1 := hash(key) for at := &t.root; ; { curr := *at if curr == nil { *at = &node{key: key} return true } hash2 := hash(curr.key) switch { case hash1 < hash2: case hash1 == hash2: return false case key < curr.key: at = &curr.lt continue default: at = &curr.gt continue } var ( put = &node{key: key} lt = &put.lt gt = &put.gt ) for curr != nil { if curr.key < key { *lt = curr lt = &curr.gt curr = curr.gt continue } *gt = curr gt = &curr.lt curr = curr.lt } *lt = nil *gt = nil *at = put return true } } func (t *Treap) Remove(key int) bool { for at := &t.root; ; { curr := *at switch { case curr == nil: return false case key < curr.key: at = &curr.lt case key > curr.key: at = &curr.gt continue } var ( lt = curr.lt gt = curr.gt ) for { switch { case lt == nil: *at = gt return true case gt == nil: *at = lt return true case hash(lt.key) < hash(gt.key): *at = lt at = <.gt lt = lt.gt default: *at = gt at = >.lt gt = gt.lt } } } }
To receive a hint, submit unfixed code.