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

**Complete this problem in the LANGUAGE SCHEME (R5RS)** 4. Deleting a value from

ID: 3603831 • Letter: #

Question

**Complete this problem in the LANGUAGE SCHEME (R5RS)**

4. Deleting a value from a binary search tree can be tricky - when you delete a node containing a value, the result must still be a binary search tree. There is an explanation below of the various cases you will need to consider. You will do this in parts, starting with the easier cases and then solving the general problem. For each of these delete functions, the trees that are returned must maintain the binary search tree property explained below.

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>

  

struct node

{

    int key;

    struct node *left, *right;

};

  

// A utility function to create a new BST node

struct node *newNode(int item)

{

    struct node *temp = (struct node *)malloc(sizeof(struct node));

    temp->key = item;

    temp->left = temp->right = NULL;

    return temp;

}

  

// A utility function to do inorder traversal of BST

void inorder(struct node *root)

{

    if (root != NULL)

    {

        inorder(root->left);

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

        inorder(root->right);

    }

}

  

/* A utility function to insert a new node with given key in BST */

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

{

    /* If the tree is empty, return a new node */

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

    /* Otherwise, recur down the tree */

    if (key < node->key)

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

    else if (key > node->key)

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

    /* return the (unchanged) node pointer */

    return node;

}

  

// Driver Program to test above functions

int main()

{

    /* Let us create following BST

              50

           /    

          30      70

         /     /

       20   40 60   80 */

    struct node *root = NULL;

    root = insert(root, 50);

    insert(root, 30);

    insert(root, 20);

    insert(root, 40);

    insert(root, 70);

    insert(root, 60);

    insert(root, 80);

  

    // print inoder traversal of the BST

    inorder(root);

  

    return 0;

}