Example #1
0
func (list *List) Compare(left int, right int) (int, error) {
	if !list.withinRange(left) {
		return 0, fmt.Errorf("golib/arraylist/Compare: Invalid left index: %d, size: %d", left, list.Size())
	}

	if !list.withinRange(right) {
		return 0, fmt.Errorf("golib/arraylist/Compare: Invalid right index: %d", right)
	}

	return comparable.Compare(list.elements[left], list.elements[right]), nil
}
Example #2
0
//recursive insertion
func (bt *BinaryTree) insert(root *BTNode, pNewNode *BTNode) *BTNode {
	if root == nil {
		root = pNewNode
		return root
	}

	if comparable.Compare(root.element, pNewNode.element) < 0 {
		root.right = bt.insert(root.right, pNewNode)
	} else {
		root.left = bt.insert(root.left, pNewNode)
	}

	return root
}
Example #3
0
func (bt *BinaryTree) lookup(node *BTNode, target interface{}) (*BTNode, bool) {
	if node == nil {
		return nil, false
	}

	if node.element == target {
		return node, true
	}

	if comparable.Compare(node.element, target) < 0 {
		return bt.lookup(node.right, target)
	} else {
		return bt.lookup(node.left, target)
	}
}