Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8536435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:39:48+00:00 2026-06-11T10:39:48+00:00

the following code reads an input array, and constructs a BST from it. if

  • 0

the following code reads an input array, and constructs a BST from it. if the current arr[i] is a duplicate, of a node in the tree, then arr[i] is discarded. count in the struct node refers to the number of times a number appears in the array. fi refers to the first index of the element found in the array. after the insertion, i am doing a post-order traversal of the tree and printing the data, count and index (in this order). the output i am getting when i run this code is:

0 0 7
0 0 6

thank you for your help.

Jeev

 struct node{

    int data;
    struct node *left;
    struct node *right;
    int fi;
    int count;

};

struct node* binSearchTree(int arr[], int size);
int setdata(struct node**node, int data, int index);
void insert(int data, struct node **root, int index);
void sortOnCount(struct node* root);

void main(){

    int arr[] = {2,5,2,8,5,6,8,8};
    int size = sizeof(arr)/sizeof(arr[0]);
struct node* temp = binSearchTree(arr, size);
sortOnCount(temp);

}   

struct node* binSearchTree(int arr[], int size){

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

    if(!setdata(&root, arr[0], 0))
        fprintf(stderr, "root couldn't be initialized");

    int i = 1;
    for(;i<size;i++){
        insert(arr[i], &root, i);
    }

    return root;
}    

int  setdata(struct node** nod, int data, int index){

    if(*nod!=NULL){

    (*nod)->fi = index;
    (*nod)->left = NULL;
    (*nod)->right = NULL;
    return 1;
}
return 0;
}

void insert(int data, struct node **root, int index){

struct node* new = (struct node*)malloc(sizeof(struct node));
setdata(&new, data, index);
struct node** temp = root;

while(1){

    if(data<=(*temp)->data){
        if((*temp)->left!=NULL)
            *temp=(*temp)->left;
        else{
            (*temp)->left = new;
            break;
        }
    }
    else if(data>(*temp)->data){
        if((*temp)->right!=NULL)
            *temp=(*temp)->right;
        else{
            (*temp)->right = new;
            break;
        }
    }
    else{
        (*temp)->count++;
        free(new);
        break;
    }
}



}

void sortOnCount(struct node* root){

if(root!=NULL){

    sortOnCount(root->left);
    sortOnCount(root->right);
    printf("%d %d %d\n", (root)->data, (root)->count, (root)->fi);
}   
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:39:49+00:00Added an answer on June 11, 2026 at 10:39 am

    Well, for one, you’re passing a pointer to a pointer in your insert function:

    void insert(int data, struct node **root, int index);
    

    And then you have a temporary variable:

    struct node** temp = root;
    

    that you use to traverse your tree. When you do that, you’re changing your root. Just pass a pointer, i.e.,

    void insert(int data, node *root , int index){
    

    Second, if(data<=(*temp)->data){ should JUST be <, not <=.

    Hint: in general, it’s fairly rare when you actually need to pass a pointer to a pointer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following piece of Java code that reads strings from CSV file.
I have the following code that reads response from a POP server through Sockets
The following code reads a text file one character at the time and print
I have the following code which reads in the follow file, append a \r\n
I have the following code that reads the content of a url public static
I have the following code in a few activities and it reads and parses
I'm following a book on c, and I come to some code that reads
I wrote the following code to read a character array and print it. #include<stdio.h>
I have the following code: <?php $this->beginWidget('zii.widgets.jui.CJuiDialog', array( 'id'=>'mydialog', // additional javascript options for
I have written the following line of code that reads the data in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.