예제 #1
0
// Contains returns true iff the tree contains the specified value, in O(log(N)) time.
//
func (a *T) Contains(value interface{}) bool {
	if a == nil {
		return false
	}
	lessFn, s := ordinal.FnScore(value)
	for {
		switch {
		case a == nil:
			return false
		case s < a.score:
			a = a.left
			continue
		case a.score < s:
			a = a.right
			continue
		case lessFn(value, a.value):
			a = a.left
			continue
		case lessFn(a.value, value):
			a = a.right
			continue
		default:
			return true
		}
	}
	panic("never")
}
예제 #2
0
// Remove returns a new treap like the original, but with the value removed, in O(log(N)) time.
// If there is no matching value to remove, the original tree is returned.
// If there are multiple matching values, only one is removed.
//
func (t *T) Remove(value interface{}) *T {
	less, score := ordinal.FnScore(value)
	rv, ok := t.remove(value, score, less)
	if !ok {
		return t
	}
	return rv
}
예제 #3
0
// Insert returns a new tree like the original, but with the value inserted, in O(log(N)) time.
//
func (t *T) Insert(value interface{}) *T {
	less, score := ordinal.FnScore(value)
	nu := &T{1, rand.Int31(), value, score, nil, nil}
	return t.insert(nu, less)
}