Instructions
First Exercise
Second Exercise
Third exercise
- Isert value tree: Inserts the value into the tree and returns the resulting tree. The resulting tree does NOT need to be balanced. If the value already exists in the tree, return the tree without inserting the value.
- Sarch value tree: Returns true if the value is in the tree and false otherwise.
- Count func tree: The parameter func is a Boolean function that takes a single parameter and returns true or false. The function tests the value of each node with func and returns the number of nodes that evaluate to true.
- EvenCount tree: Returns the number of nodes that contain even integers. REQUIREMENT:
This function must be a single call to count (part 3C) using a lambda function.
let rec maxCylinderVolume list = match list with | [] -> 0.0 // if empty, return 0.0 | (r,h)::xs -> // if not empty, it has (radius, height) let volume = System.Math.PI*r*r*h // calculate volume let maxVol = maxCylinderVolume xs // calculate max of tail if volume > maxVol then volume // if our volume is max, return it else maxVol // otherwise return tail maxlet rec elimDuplicates list = match list with | [] -> [] // if empty, return empty | [a] -> [a] // if one element, return same list | a::b::xs -> // if two or more elements if a = b then elimDuplicates (b::xs) // if elements are equal, remove 1, search rest else a::(elimDuplicates (b::xs)) // if not repeated, add first, search in resttype BST = | Empty | TreeNode of int * BST * BSTlet rec insert value tree = match tree with | Empty -> TreeNode (value, Empty, Empty) // if empty tree, add treenode with value | TreeNode (n, lt, rt) -> // if not empty tree if value < n then TreeNode (n, (insert value lt), rt) // if value is smaller than val in this node, go left branch else if value > n then TreeNode (n, lt, (insert value rt)) // if value is greater than val in this node, go right branch else TreeNode (n, lt, rt) // if value is equal to this node's value, don't change treelet rec search value tree = match tree with | Empty -> false // empty node means not found | TreeNode (n, lt, rt) -> // if not empty if value < n then search value lt // if value is smaller than val in this node, search left branch else if value > n then search value rt // if value is greater than val in this node, search right branch else true // else, value is equal to this node's value, we found itlet rec count func tree = match tree with | Empty -> 0 // empty node doesn't have a value, no count | TreeNode (n, lt, rt) -> // if not empty, add matches in left plus matches in right, plus our match if func of our value is true (count func lt) + (count func rt) + (if func n then 1 else 0)let evenCount tree = count (fun x -> x % 2 = 0) tree // count using lambda that returns true if remainder by 2 is zero (number is even)
Related Samples
Discover our Computer Science Assignments sample section, where we offer expertly solved problems covering diverse topics. From algorithms to software engineering principles, each sample provides annotated solutions for learning and reference. Perfect for students aiming to excel in their computer science studies. Enhance your skills with our curated samples today!
Computer Science
Computer Science
Computer Science
Computer Science
Computer Science
Computer Science
Computer Science
Computer Science
Computer Science
Computer Science
Computer Science
Computer Science
Computer Science
Computer Science
Computer Science
Computer Science
Computer Science
Computer Science
Computer Science
Computer Science