コード例 #1
0
ファイル: tst.go プロジェクト: jmptrader/data-structures
func (self *TST) PrefixFind(prefix ByteSlice) KVIterator {
	if len(prefix) == 0 {
		return self.Iterate()
	}
	type entry struct {
		n *TSTNode
		d int
	}
	var root *TSTNode = nil
	next := &entry{self.heads[prefix[0]], 1}
	for {
		if next.n == nil {
			break
		} else if next.n.Internal() {
			if next.d == len(prefix) {
				root = next.n
				break
			}
			ch := prefix[next.d]
			if ch < next.n.ch {
				next = &entry{next.n.l, next.d}
			} else if ch == next.n.ch {
				next = &entry{next.n.m, next.d + 1}
			} else if ch > next.n.ch {
				next = &entry{next.n.r, next.d}
			}
		} else if next.n.accepting && next.n.key[:len(prefix)].Equals(prefix) {
			root = next.n
			break
		} else {
			break
		}
	}
	if root == nil {
		return func() (Hashable, interface{}, KVIterator) {
			return nil, nil, nil
		}
	}
	tni := tree.TraverseTreePreOrder(root)
	var kv_iterator KVIterator
	kv_iterator = func() (key Hashable, value interface{}, next KVIterator) {
		var tn TreeNode
		for {
			tn, tni = tni()
			if tni == nil {
				return nil, nil, nil
			}
			n := tn.(*TSTNode)
			if n.accepting {
				return n.key[:len(n.key)-1], n.value, kv_iterator
			}
		}
	}
	return kv_iterator
}
コード例 #2
0
func TestTraversals(t *testing.T) {
	var data []int = []int{
		1, 5, 7, 9, 12, 13, 17, 18, 19, 20,
	}
	var order []int = []int{
		6, 1, 8, 2, 4, 9, 5, 7, 0, 3,
	}
	var preorder []int = []int{
		17, 7, 5, 1, 12, 9, 13, 19, 18, 20,
	}
	var postorder []int = []int{
		1, 5, 9, 13, 12, 7, 18, 20, 19, 17,
	}

	test := func(T types.TreeMap) {
		t.Logf("%T", T)
		for j := range order {
			if err := T.Put(types.Int(data[order[j]]), order[j]); err != nil {
				t.Error(err)
			}
		}

		j := 0
		for tn, next := tree.TraverseBinaryTreeInOrder(T.Root().(types.BinaryTreeNode))(); next != nil; tn, next = next() {
			if int(tn.Key().(types.Int)) != data[j] {
				t.Error("key in wrong spot in-order")
			}
			j += 1
		}

		j = 0
		for tn, next := tree.TraverseTreePreOrder(T.Root())(); next != nil; tn, next = next() {
			if int(tn.Key().(types.Int)) != preorder[j] {
				t.Error("key in wrong spot pre-order")
			}
			j += 1
		}

		j = 0
		for tn, next := tree.TraverseTreePostOrder(T.Root())(); next != nil; tn, next = next() {
			if int(tn.Key().(types.Int)) != postorder[j] {
				t.Error("key in wrong spot post-order")
			}
			j += 1
		}
	}
	test(NewAvlTree())
	test(NewImmutableAvlTree())
}
コード例 #3
0
ファイル: tst.go プロジェクト: jmptrader/data-structures
func (self *TST) Iterate() KVIterator {
	tnis := make([]TreeNodeIterator, 0, 256)
	for _, n := range self.heads {
		if n != nil {
			tnis = append(tnis, tree.TraverseTreePreOrder(n))
		}
	}
	tni := ChainTreeNodeIterators(tnis...)
	var kv_iterator KVIterator
	kv_iterator = func() (key Hashable, value interface{}, next KVIterator) {
		var tn TreeNode
		for {
			tn, tni = tni()
			if tni == nil {
				return nil, nil, nil
			}
			n := tn.(*TSTNode)
			if n.accepting {
				return n.key[:len(n.key)-1], n.value, kv_iterator
			}
		}
	}
	return kv_iterator
}