Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Language : Racket Objective: Implement a Standard Binary Search Tree

ID: 3816209 • Letter: P

Question

Programming Language: Racket

Objective: Implement a Standard Binary Search Tree which include the following functions:

.

.

.

AT THE MINIMUM, IMPLEMENT ADD AND SEARCH TO THE BINARY SEARCH TREE:

1.) (constructBST)

constructs empty BST

2.) (addToBST BST key value)

Adds a key-value pair to a BST, where key is an integer and value is a string, integer, list, etc...

3.) (searchBST BST key)

Searches for a value based on key. Return key if value is not found for the key. Return key and value if value is found for the key.

.

.

.

IMPLEMENT THESE IF YOU ARE CAPABLE OF DOING SO:

4.) (mapBST BST someFunction)

Maps a BST to a new BST with modified values.The values should be modified by an arbitrary function someFunction

Map means is that you apply the function to all the nodes in the tree

mapBST should leave the input BST as is, and create a new BST with the same "treeshape" and same keys, but with different value in each node (based on   someFunction)

For example: if there's a tree with root node 2, and children 1 and 3 and I call map with the function addOne, it becomes one with root 2, children 3 4

5.) (foldBST BST someFunction)

Folding a BST to a single fold-value

Based on traversal of the tree from leaves to root

The returned fold-value should be produced by user-provided someFunction based on key/value of current node a fold-values return from left and right children

.

.

.

It's okay to submit partially completed BST, although a fully functional BST is much appreciated.

NOTE: BST = Binary Search Tree, Implement add and search functions at the minimum for the binary search tree

Explanation / Answer

import java.util.Scanner;


class BinaryNode
{
BSTNode left, right;
int data;
struct node* lleft;
struct node* rright;


public BinaryNode()
{
left = null;
right = null;
data = 0;
}
  
public BinaryNode(int n)
{
left = null;
right = null;
data = n;
}
public void setLeft(Binary n)
{
left = n;

public void setRight(BinaryNode n)
{
right = n;
}
public BinaryNode getLeft()
{
return left;
}
public BinaryNode getRight()
{
return right;
}
public void setData(int d)
{
data = d;
}
public int getData()
{
return data;
}   
}

class Binary
{
private BinaryNode root;

public Binary()
{
root = null;
}
public boolean isEmpty()
{
return root == null;
}

public void insert(int data)
{
root = insert(root, data);
}

private Binaryode insert(BinaryNode node, int data)
{
if (node == null)
node = new BinaryNode(data);
else
{
if (data <= node.getData())
node.left = insert(node.left, data);
else
node.right = insert(node.right, data);
}
return node;
}
  
public void delete(int k)
{
if (isEmpty())
System.out.println("Tree Empty");
else if (search(k) == false)
System.out.println("Sorry "+ k +" is not present");
else
{
root = delete(root, k);
System.out.println(k+ " deleted from the tree");
}
}

public boolean search(int val)
{
return search(root, val);
}
private boolean search(BinaryNode r, int val)
{
boolean found = false;
while ((r != null) && !found)
{
int rval = r.getData();
if (val < rval)
r = r.getLeft();
else if (val > rval)
r = r.getRight();
else
{
found = true;
break;
}
found = search(r, val);
}
return found;
}
public int height(struct node* node)
{
if (node==NULL)
return 0;
else
{
/* compute the depth of each subtree */
int lDepth = maxDepth(node->lleft);
int rDepth = maxDepth(node->rright);

/* use the larger one */
if (lDepth > rDepth)
return(lDepth+1);
else return(rDepth+1);
}
}

}
public boolean search(int val)
{
return search(root, val);
}
/* Function to search for an element recursively */
private boolean search(BSTNode r, int val)
{
boolean found = false;
while ((r != null) && !found)
{
int rval = r.getData();
if (val < rval)
r = r.getLeft();
else if (val > rval)
r = r.getRight();
else
{
found = true;
break;
}
found = search(r, val);
}
return found;
}   


/* Class BinarySearchTree */
public class BinarySearchTree
{
public static void main(String[] args)
{   
Scanner scan = new Scanner(Syst
Binary binary = new Binary();
System.out.println("Binary Search Tree Test ");
char ch;
  
do
{
System.out.println(" Binary Search Tree Operations ");
System.out.println("1. insert ");
System.out.println("2. delete");
System.out.println("3. search");
   System.out.println("4. height");

int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
binary.insert( scan.nextInt() );   
break;
case 2 :
System.out.println("Enter integer element to delete");
binary.delete( scan.nextInt() );   
break;   
case 3 :
System.out.println("Enter integer element to search");
System.out.println("Search result : "+ bst.search( scan.nextInt() ));
break;
case 4 :
System.out.println("height = "+ binary.height ());
break;   

default :
System.out.println("Wrong Entry ");
break;   
}
  
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');   
}
}