Constraints
Type Constraints
enum BinaryTree<T> {
case leaf
indirect case node(left: BinaryTree, value: T, right: BinaryTree)
}extension BinaryTree where T: Equatable {
func contains(_ element: T) -> Bool {
switch self {
case .leaf: return false
case let .node(left, value, right):
if value == element {
return true
}
return left.contains(element) || right.contains(element)
}
}
}Switch Constraints
Last updated
Was this helpful?