Question

Overview The purpose of this assignment is to practice functional programming in the Racket Programming Language and to also

3. bst-contains? - if T is a binary search tree of integers and X is an integer, then the value of the expression (bst- conta

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.checking BST empty or not

bool BST(node *root)

{

if (root==NULL)

return true;

else

return false;

}

2. searching an element in BST

struct node* search(struct node* root, int key)

{

    if (root == NULL || root->key == key)

       return root;

    if (root->key < key)

       return search(root->right, key);

    return search(root->left, key);

}

3.inserting an element in bst

void inorder(struct node *root)

{

    if (root != NULL)

    {

        inorder(root->left);

        printf("%d \n", root->key);

        inorder(root->right);

    }

}

struct node* insert(struct node* node, int key)

{

    if (node == NULL) return newNode(key);

    if (key < node->key)

        node->left = insert(node->left, key);

    else if (key > node->key)

        node->right = insert(node->right, key);   

    return node;

}

4.int bst_size(struct node* root)

{

if(root->left ==NULL && root->right==NULL)

return 1;

else

{

return (bst_size(root->left) +bst_size(root->right) +1);

}

}

5.bst height

int bst_height(struct node* root)

{

if(root->left == NULL && root->right==NULL)

return 0;

else

{

int a=bst_height(root->left) +1;

int b=bst_height(root->right)+1;

return max(a,b);

}

}

6.bst delete a node

struct node * minValueNode(struct node* node)

{

    struct node* current = node;

    while (current && current->left != NULL)

        current = current->left;

    return current;

}

struct node* deleteNode(struct node* root, int key)

{

    if (root == NULL) return root;

    if (key < root->key)

root->left = deleteNode(root->left, key);

    else if (key > root->key)

        root->right = deleteNode(root->right, key);

    else

    {

if (root->left == NULL)

        {

            struct node *temp = root->right;

            free(root);

            return temp;

        }

        else if (root->right == NULL)

        {

            struct node *temp = root->left;

            free(root);

            return temp;

        }

        struct node* temp = minValueNode(root->right);

root->key = temp->key;

        root->right = deleteNode(root->right, temp->key);

    }

    return root;

}

Add a comment
Know the answer?
Add Answer to:
Overview The purpose of this assignment is to practice functional programming in the Racket Programming Language...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Racket Recursion, tail-recursion, high-order functions and functional programming. 1. Modify our filter function so that...

    Using Racket Recursion, tail-recursion, high-order functions and functional programming. 1. Modify our filter function so that it is tail-recursive. You may use the letrec form but do not use any additional forms and functions besides those we've talked about in class. (define filter (lambda (input-list func)     (cond ((null? input-list) '())           ((func (car input-list))            (cons (car input-list) (filter (cdr input-list) func)))           (else            (filter (cdr input-list) func))))) 2. Test your filter function on '(25 -22 44 56...

  • Would appreciate the answer in the Java coding language please and thank you! 10d 10h left...

    Would appreciate the answer in the Java coding language please and thank you! 10d 10h left Java 7 1. Check the Structure Autocomplete Ready 1 > import java.io.*;... 10 ALL A binary tree uses a multi-node data structure where each node may have 0 to 2 child nodes, and has one stored value, its node number in this case. A tree may either be: 11 class Result { * Complete the 'isValid' function below. • An empty tree, the root...

  • Could someone please summarize the following for my programming class? They are study questions for java...

    Could someone please summarize the following for my programming class? They are study questions for java What an association list is. How to test if an association list is empty. How to find the value associated with a key in an association list. How to add a key-value pair to an association list. How to delete a key-value pair from an association list. How efficient an association list is (using O notation). What a circular list is. What a circular...

  • The goal of this assignment is to reinforce the tree data structure in C++. Specifically, the...

    The goal of this assignment is to reinforce the tree data structure in C++. Specifically, the assignment is to do the following: Binary search trees have their best performance when they are balanced, which means that at each node, n, the height of the left subtree of n is within one of the height of the right subtree of n. Write a function (and a test program) which takes a sorted list of entries and produces a balanced binary search...

  • In this assignment, you will add several methods to the Binary Search Tree. You should have compl...

    In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...

  • using java to write,show me the output. please write some common. You CAN NOT use inbuild...

    using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main {    public static void main(String[] args) {        BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...

  • Recall from Assignment 2 the definition of a binary tree data structure: either an empty tree,...

    Recall from Assignment 2 the definition of a binary tree data structure: either an empty tree, or a node with two children that are trees. Let T(n) denote the number of binary trees with n nodes. For example T(3) 5 because there are five binary trees with three nodes: (a) Using the recursive definition of a binary tree structure, or otherwise, derive a recurrence equation for T(n). (8 marks) A full binary tree is a non-empty binary tree where every...

  • Assignment 4 1)For all the questions in Assignment 4, you need to draw a diagram to...

    Assignment 4 1)For all the questions in Assignment 4, you need to draw a diagram to describe the steps and provide short description as needed. Construct a Binary Search Tree by inserting the following node one by one. [4 points] 7,12,6,9,11,23,24,27,15 2)Provide an example for each following cases to search a given key in a Binary Search Tree. [4 points] Time efficiency=Ο(1)3S8CEMtd9AAAAAElFTkSuQmCC Time efficiency=ΟlogncdA3f5imtB2VefcxtquYZA8cQHOJLX+AydvU9UjZ Time efficiency=Ο(n)ZRO+N+OcdxOBP4CbNI+pPm7zwkAAAAASUVORK5CY 3)For the BST you created in question 1, delete the node 9, then 7,...

  • Assignment 4 1)For all the questions in Assignment 4, you need to draw a diagram to describe the ...

    Assignment 4 1)For all the questions in Assignment 4, you need to draw a diagram to describe the steps and provide short description as needed. Construct a Binary Search Tree by inserting the following node one by one. [4 points] 7,12,6,9,11,23,24,27,15 2)Provide an example for each following cases to search a given key in a Binary Search Tree. [4 points] Time efficiency=Ο(1)3S8CEMtd9AAAAAElFTkSuQmCC Time efficiency=ΟlogncdA3f5imtB2VefcxtquYZA8cQHOJLX+AydvU9UjZ Time efficiency=Ο(n)ZRO+N+OcdxOBP4CbNI+pPm7zwkAAAAASUVORK5CY 3)For the BST you created in question 1, delete the node 9, then 7,...

  • in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree...

    in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree You will also need to implement a Node class. This class will not be tested, but is needed to implement the BST. Your BST must implement the following methods. You are free to implement additional helper methods. It is recommended you create your own helper methods Constructor: Creates an Empty Tree String Method: Returns the string "Empty Tree" for an empty tree. Otherwise, returns...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT