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

3. In lab this week, you wrote some functions to work with binary trees. In this

ID: 3605138 • Letter: 3

Question

3. In lab this week, you wrote some functions to work with binary trees. In this question, you will develop some code for binary search trees, as we have been doing in lecture. The representation of a binary search tree will be the same as the binary trees we used in lab, that is, a tree is a three-element list: the value of the node, the left subtree, and the right subtree, with empty trees represented as the empty list. Unlike the binary search trees we have been discussing in lecture, for the questions here we will assume all of the values are strings, so you will need to use the comparison functions string

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

#include "bst.h"

#define SIZE 9

int main()

{

    node* root = NULL;

    comparer int_comp = compare;

    callback f = display;

    /* insert data into the tree */

    int a[SIZE] = {8,3,10,1,6,14,4,7,13};

    int i;

    printf("--- C Binary Search Tree ---- ");

    printf("Insert: ");

    for(i = 0; i < SIZE; i++)

    {

        printf("%d ",a[i]);

        root = insert_node(root,int_comp,a[i]);

    }

    printf(" into the tree. ");

    /* display the tree */

    display_tree(root);

    /* remove element */

    int r;

    do

    {

        printf("Enter data to remove, (-1 to exit):");

        scanf("%d",&r);

        if(r == -1)

            break;

        root = delete_node(root,r,int_comp);

        /* display the tree */

        if(root != NULL)

            display_tree(root);

        else

            break;

    }

    while(root != NULL);

    /* search for a node */

    int key = 0;

    node *s;

    while(key != -1)

    {

        printf("Enter data to search (-1 to exit):");

        scanf("%d",&key);

        s = search(root,key,int_comp);

        if(s != NULL)

        {

            printf("Found it %d",s->data);

            if(s->left != NULL)

                printf("(L: %d)",s->left->data);

            if(s->right != NULL)

                printf("(R: %d)",s->right->data);

            printf(" ");

        }

        else

        {

            printf("node %d not found ",key);

        }

    }

    /* remove the whole tree */

    dispose(root);

    return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote