The following binary search tree functions for a binary search tree of integers. (This will essentially be a tree set, i.e., a set data structure implemented using a tree.) Use the following type definition for a BST (copy this into your solution): // Tree definition for problem 3 type BST = | Empty | TreeNode of int * BST * BST i. insert 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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

In F# write:

The following binary search tree functions for a binary search tree of integers. (This will essentially be a tree set, i.e., a set data structure implemented using a tree.) Use the following type definition for a BST (copy this into your solution):

// Tree definition for problem 3
type BST =
    | Empty
    | TreeNode of int * BST * BST

i. insert 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.

ii. contains value tree

Returns true if the value is in the tree or false if it is not.

iii. 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.

iv. evenCount tree

Returns the number of nodes that contain even integers.

REQUIREMENT: This function may not call any other function by name, except for the count function. However, you may use lambda functions (which do not have a name).

NOTE: 

  1. Do not use "mutable" variables. Example any use of the mutable keyword.
  2. Functions should have no side effects. This includes:
    • no output (other than the function's return value) - e.g., no use of printfn or file output
    • no input (other than the function's arguments) - e.g., no user input or file input
    • no use of external state - e.g., global or static variables
  3.  In particular, you may not use F# system functions or methods (such as map or max).
  4. You may define and use additional functions (e.g., helper functions) if your feel it is appropriate.
  5. Think about what appropriate test cases for each of the functions you implement should be (including any "helper functions" that you choose to create).  Be sure to think about all the edge cases and exceptions to normal behavior (e.g., what if the list input is an empty list?).  Do not assume any input is invalid unless the assignment description below explicitly says you may, or it would violate F#'s type checking.

Examples in F# Interactive:

> let bt1 = insert 10 Empty;;
val bt1 : BST = TreeNode (10,Empty,Empty)

 

> let bt2 = insert 5 bt1;;
val bt2 : BST = TreeNode (10,TreeNode (5,Empty,Empty),Empty)

 

> let bt3 = insert 3 bt2;;
val bt3 : BST =
  TreeNode (10,TreeNode (5,TreeNode (3,Empty,Empty),Empty),Empty)

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 6 images

Blurred answer
Knowledge Booster
Types of trees
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education