Question: Assume the following abstract data structure (ADT) implemented in Scala Assignment: 1.Create a program that instances nodes and branches of a tree of strings as
Assume the following abstract data structure (ADT) implemented in Scala
Assignment:
1.Create a program that instances nodes and branches of a tree of strings as indicated in the figure above. The root node of your tree must be called root, e.g., val root =
2.Write a function size that counts the number of nodes (leaves and branches) in a tree
sizeT(tree: Tree[_]): Int = tree match {}
3.Write a function maximum that returns the maximum element in a Tree[Int]. (Note: In Scala, you can use x.max(y) or x max y to compute the maximum of two integers x and y.)
maximumT(tree: Tree[Int]): Int = tree match {}
4.Write a function depth that returns the maximum path length from the root of a tree to any leaf
depthT(tree: Tree[_]): Int = tree match {}
The instructor will:
Open a Window command line or a Linux terminal
Call scala REPL
Copy and paste your program
Execute:
println(sizeT(root))
println(maximumT(root))
println(depthT(root))
sealed trait Tree[+A] case class Leaf [A] (value: A) extends Tree[A] case class Branch [A] (left: Tree [A], right: Tree [A]) extends Tree [A] A tree Branch Left Right Branch Left Right Left Right Leaf Leaf Leaf Leaf b' I1 Value ValueValue Value
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
