Question

C programming Create a BST with following values :{5.5,9.88,3.15,4.7,10.5} find the two maximum values in the...

C programming

Create a BST with following values :{5.5,9.88,3.15,4.7,10.5}

find the two maximum values in the tree

Find the two minimum values in the tree

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

//For any queries, feel free to comment.

**CODE START**

#include <stdio.h>
#include <stdlib.h>

float arr[100];
int cnt;

struct btnode{
    float value;
    struct btnode *l;
    struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;

//Creating a new node
void create(float data)
{
    temp = (struct btnode *)malloc(1*sizeof(struct btnode));
    temp->value = data;
    temp->l = temp->r = NULL;
}

//Function to search the appropriate position to insert the new node
void search(struct btnode *t){
    //value more than root node value insert at right
    if ((temp->value > t->value) && (t->r != NULL))    
        search(t->r);
    else if ((temp->value > t->value) && (t->r == NULL))
        t->r = temp;
      
    //value less than root node value insert at left
    else if ((temp->value < t->value) && (t->l != NULL))  
        search(t->l);
    else if ((temp->value < t->value) && (t->l == NULL))
        t->l = temp;
}

/* To insert a node in the tree */

void insert(float data){
    create(data);
  
    if (root == NULL)
        root = temp;
    else  
        search(root);  
}

//Function to perform inorder traversal of tree
void inorder(struct btnode *t){
    if (t->l != NULL)  
        inorder(t->l);

    //Storing inorder traversal in an array "arr"
    arr[cnt++] = t->value;

    if (t->r != NULL)  
        inorder(t->r);
}

void main()
{
    //Inserting all those 5 values
    insert(5.5);
    insert(9.88);
    insert(3.15);
    insert(4.7);
    insert(10.5);
  
    //Inorder will provide elements in sorted order
    inorder(root);
  
    //Last two values will be Maximum
    printf("Maximum two values are %f and %f\n",arr[cnt-1],arr[cnt-2]);
  
    //First two values will be Minimum
    printf("Minimum two values are %f and %f\n",arr[0],arr[1]);
}

**CODE END**

**OUTPUT

**SNIPPET END**

Add a comment
Know the answer?
Add Answer to:
C programming Create a BST with following values :{5.5,9.88,3.15,4.7,10.5} find the two maximum values in the...
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
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