I need help with the following program:
Define a structure VEHICLE which contains producer, model and chassis number. Store the content of a structure in binary search tree such that the key for storing contains all fields of a structure (producer has the highest priority, and chassis number has the lowest). Print data about all vehicles from the specific producer (which is read after all input content).
Note: The code is not for compilation.

typedef struct
{
  char prod[100];
  char mod[100];
  char chs[100];
}VEHICLE;

typedef struct node
{
  VEHICLE ve;
  struct node *left,*right;
}NODE;

NODE *form_node(VEHICLE *ve)
{
  NODE *node=(NODE *)malloc(sizeof(NODE));
  node->left=node->right=0;
  node->ve=*ve;
  return node;
}

NODE *add_node(NODE *root,VEHICLE *ve)
{
  if(root==0)
  return form_node(ve);
  if(strcmp(ve->prod,root->ve.prod)<0 ||
  ((strcmp(ve->prod,root->ve.prod)==0) &&
  strcmp(ve->mod,root->ve.mod)<0) ||
  (strcmp(ve->prod,root->ve.prod)==0 &&
  strcmp(ve->mod,root->ve.mod)==0 &&
  strcmp(ve->chs,root->ve.chs)<0))
  root->left=add_node(root->left,ve);
  else
  root->right=add_node(root->right,ve);
  return root;
}

NODE *searching(NODE *root,VEHICLE *ve)
{
  if(root == 0)
  return NULL;
  else if(strcmp(ve->prod,root->ve.prod)==0 &&
  strcmp(ve->mod,root->ve.mod)==0 &&
  strcmp(ve->chs,root->ve.chs)==0)
  return root;
  else if(strcmp(root->ve.prod,ve->prod)>0 &&
  strcmp(root->ve.mod,ve->mod)>0 &&
  strcmp(root->ve.chs,ve->chs)>0)
  return searching(root->left,ve);
  else
  return searching(root->left,ve);
}

void read(VEHICLE *ve)
{
  printf("producer:");
  scanf("%s",ve->prod);
  printf("model:");
  scanf("%s",ve->mod);
  printf("chassis number:");
  scanf("%s",ve->chs);
}

How to implement a function that prints data about all vehicles from specified input producer?

Edited 11 Months Ago by negru

Firstly I would suggest a couple of helper functions:

void PrintHeader()
{
    printf("Producer\tModel\tChassis Number\n");
}
void PrintVehicle(VEHICLE* ve)
{
    printf("s%\ts%\ts%\n", ve->prod, ve->mod, ve->chs);
}

Now it's a matter of iterating through the tree and printing any vehicle matching that producer. Something like this should work:

void FindVehicles(NODE* root, int field, char searchTerm[])
{
    PrintHeader();
    if (root == NULL)
    {
        printf("No vehicles found");
        return;
    }
    bool found = false;
    switch (field)
    {
    case 1:
        if (root->ve.prod == searchTerm)
        {
            found = true;
        }
        break;
    case 2:
        if (root->ve.mod == searchTerm)
        {
            found = true;
        }
        break;
    case 3:
        if (root->ve.chs == searchTerm)
        {
            found = true;
        }
        break;
    default:
        printf("Invalid search field");
        break;
    }
    if (found)
    {
        PrintVehicle(&root->ve);
    }
    FindVehicles(root->left, field, searchTerm);
    FindVehicles(root->right, field, searchTerm);
}

On a side note, the read function is very much prone to errors. Since misspelling any part of the vehicle will mean a non-standard entry in the tree, it would make more sense to create menus of choices rather than having the user type in each field. Also concatenating the fields into one string and using that for the comparions when adding and searching for one vehicle, simplifies your code considerably. Something like this:

typedef struct
{
    char prod[100];
    char mod[100];
    char chs[100];
    char cmpString[300];

}VEHICLE;
typedef struct node
{
    VEHICLE ve;
    struct node *left, *right;
}NODE;
NODE *form_node(VEHICLE *ve)
{
    NODE *node = (NODE *)malloc(sizeof(NODE));
    node->left = node->right = NULL;
    node->ve = *ve;
    return node;
}
NODE *add_node(NODE *root, VEHICLE *ve)
{
    if (root == NULL)
        return form_node(ve);
    int veCmp = strcmp(ve->cmpString, root->ve.cmpString);
    if (veCmp == 0)
    {
        return root;
    }
    if (veCmp < 0)
    {
        root->left = add_node(root->left, ve);
    }
    else
    {
        root->right = add_node(root->right, ve);
    }

    return root;
}
NODE *searching(NODE *root, VEHICLE *ve)
{
    if (root == NULL)
    {
        return NULL;
    }
    int veCmp = strcmp(ve->cmpString, root->ve.cmpString);
    if (veCmp == 0)
    {
        return root;
    }
    if (veCmp < 0)
    {
        return searching(root->left, ve);
    }
    else
    {
        return searching(root->right, ve);
    }
}
void ReadVehicle(VEHICLE *ve)
{
    printf("producer:");
    scanf("%s", ve->prod);
    printf("model:");
    scanf("%s", ve->mod);
    printf("chassis number:");
    scanf("%s", ve->chs);
    strcat(ve->cmpString, ve->prod);
    strcat(ve->cmpString, ve->mod);
    strcat(ve->cmpString, ve->chs);
}

Edited 11 Months Ago by tinstaafl

This question has already been answered. Start a new discussion instead.