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

CS 2123 Data Structures Due Friday March 25 1. (100 pts) Write a program to find

ID: 3680230 • Letter: C

Question

CS 2123 Data Structures Due Friday March 25 1. (100 pts) Write a program to find the kth smallest element in a binary search tree. Since we don't know how many elements are in each subtree, we can add a new field count to each node that stores the total nmber of elements that are in the left subtree and the current node. You need the update count field of the nodes when you insert and delete from the tree To find the kth smallest node, you need to use the count field of the current mode and decide whether kth smallest node is in the left subtree or the right subtree and follow that subtree, Node structure with count ficld is as follow struct node int key: int count struct node left, right typedef struet node node: Consider the binary search tree given below Count field for a node is shown on top left of the node, 10 16 Figure 1: Binary Search Tree Sample execution for above tree is given below foxoaasig4 Enter the set of nusbers for the tree 10 6 14 4 8 12 16 Enter k Result 12 Submit your program electronically using the blackboard system The program you submit should be your own work. Cheating will be reported to office of academic Both the copier a nd will be held responsible. be held

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

#define ARRAY_SIZE(arr) sizeof(arr)/sizeof(arr[0])

typedef struct node_t node_t;

/* Binary search tree node */

struct node_t

{

    int key;

    int Count;

    node_t* left;

    node_t* right;

};

/* Iterative insertion

   Recursion is least preferred unless we gain something

*/

node_t *insert_node(node_t *root, node_t* node)

{

    /* A crawling pointer */

    node_t *pTraverse = root;

    node_t *currentParent = root;

    // Traverse till appropriate node

    while(pTraverse)

    {

        currentParent = pTraverse;

        if( node->key< pTraverse->key)

        {

            /* We are branching to left subtree

               increment node count */

            pTraverse->Count++;

            /* left subtree */

            pTraverse = pTraverse->left;

        }

        else

        {

            /* right subtree */

            pTraverse = pTraverse->right;

        }

    }

    /* If the tree is empty, make it as root node */

    if( !root )

    {

        root = node;

    }

    else if( node->key< currentParent->key)

    {

        /* Insert on left side */

        currentParent->left = node;

    }

    else

    {

        /* Insert on right side */

        currentParent->right = node;

    }

    return root;

}

/* Elements are in an array. The function builds binary tree */

node_t* binary_search_tree(node_t *root, int keys[], int const size)

{

    int iterator;

    node_t *new_node = NULL;

    for(iterator = 0; iterator < size; iterator++)

    {

        new_node = (node_t *)malloc( sizeof(node_t) );

        /* initialize */

        new_node->key= keys[iterator];

        new_node->Count = 0;

        new_node->left   = NULL;

        new_node->right = NULL;

        /* insert into BST */

        root = insert_node(root, new_node);

    }

return root;

}

int k_smallest_element(node_t *root, int k)

{

    int ret = -1;

if( root )

    {

        /* A crawling pointer */

        node_t *pTraverse = root;

        /* Go to k-th smallest */

        while(pTraverse)

        {

            if( (pTraverse->Count + 1) == k )

            {

                ret = pTraverse->key;

                break;

            }

            else if( k > pTraverse->Count )

            {

                /* There are less nodes on left subtree

                    Go to right subtree */

                k = k - (pTraverse->Count + 1);

                pTraverse = pTraverse->right;

            }

            else

            {

                /* The node is on left subtree */

                pTraverse = pTraverse->left;

            }

        }

    }

    return ret;

}

/* main program to call above functions */

int main(void)

{

    /* just add elements to test */

    /* NOTE: A sorted array results in skewed tree */

    int ele[] = { 20, 8, 22, 4, 12, 10, 14 };

    int i;

    node_t* root = NULL;

/* Creating the tree given in the above diagram */

    root = binary_search_tree(root, ele, ARRAY_SIZE(ele));

    /* It should print the sorted array */

    for(i = 1; i <= ARRAY_SIZE(ele); i++)

    {

        printf(" kth smallest elment for k = %d is %d",

                 i, k_smallest_element(root, i));

    }

getchar();

    return 0;

}