+1 (315) 557-6473 

Max Cylinder, Duplicate Elimination, Binary Search Tree Assignment Solution.


Instructions

Objective

First Exercise

Write a F# assignment function maxCylinderVolume that takes a list of floating-point tuples that represent dimensions of a cylinder and returns the volume of the cylinder that has the largest volume.
Each tuple has two floating point values that are both greater than zero. The first value is the radius r and the second value is the height h. The volume of the cylinder is computed using tr?h. The value r is represented in F# with System. Math. PI. If the list is empty, return 0.0.

Second Exercise

Write a function elimDuplicates that takes a list of integers and eliminates consecutive duplicates; replacing them with a single instance of the value. Order is preserved and non-consecutive duplicates are unaffected.

Third exercise

Write the following binary search tree functions for a binary search tree of integers. Use the following type definition for a BST (copy this into your solution):
  1. 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.
  2. Sarch value tree: Returns true if the value is in the tree and false otherwise.
  3. 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.
  4. EvenCount tree: Returns the number of nodes that contain even integers. REQUIREMENT:  
  5. This function must be a single call to count (part 3C) using a lambda function.

Screenshots of output
functions for manipulating trees in haskell
Source Code
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 max
let 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 rest
type BST =  
    | Empty
    | TreeNode of int * BST * BST
let 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 tree
let 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 it
let 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)