Complete the code to declare a recursive enum case.
enum ArithmeticExpression {
case number(Int)
indirect case [1](ArithmeticExpression, ArithmeticExpression)
}The indirect keyword marks the case as recursive. Here, addition is the recursive case combining two expressions.
Complete the code to evaluate the recursive enum expression.
func evaluate(_ expression: ArithmeticExpression) -> Int {
switch expression {
case .number(let value):
return value
case .addition(let left, let right):
return evaluate(left) [1] evaluate(right)
}
}The evaluate function adds the results of the left and right expressions using the + operator.
Fix the error in the recursive enum declaration by completing the code.
enum BinaryTree {
case leaf(Int)
[1] case node(BinaryTree, BinaryTree)
}The indirect keyword is required before the recursive case node to allow the enum to reference itself.
Fill both blanks to create a recursive enum with indirect keyword and a recursive case.
indirect enum [1] { case value(Int) case [2]([1], [1]) }
The enum is named Tree and the recursive case is Node which takes two Tree values.
Fill all three blanks to complete the recursive enum and a function that counts nodes.
indirect enum [1] { case leaf(Int) case branch([1], [1]) } func countNodes(_ tree: [1]) -> Int { switch tree { case .leaf(_): return 1 case .branch(let left, let right): return countNodes(left) [2] countNodes(right) + 1 } }
The enum is named Tree. The function recursively counts nodes by adding counts of left and right branches plus one for the current branch.