The C language implements tree dynamic lookup of instance code

  • 2020-05-26 09:43:23
  • OfStack

The C language implements tree dynamic lookup of instance code

This example demonstrates a dynamic lookup method when a tree data structure stores a collection of records. First, the program USES the existing structure array data to build a two-fork tree through the construct() function. In the process of tree building, the value of each node should be greater than the value of its left subtree node and less than the value of all nodes in its right subtree. This function returns the root pointer of tree building. It is then looked up by the function Search(root,name), and if it is found, it is printed out. If it is not, the user can choose whether or not to insert the data into the tree.

The specific code is as follows:


#include <stdio.h>
#include <stdlib.h> 
#include <string.h>
#define NUM 4
 
struct tree
{
  char name[20];
  char city[20];
  char sex[10];
  char age[10];
  char job[10];
  struct tree *left;
  struct tree *right;
};
 
struct tree Datas[NUM]=
{
  "Willing","Tianjing","Female","21","worker",NULL,NULL,
  "Tom","Beijing","Male","31","doctor",NULL,NULL,
  "Sun","Weifang","Male","24","student",NULL,NULL,
  "Marry","Shanghai","Female","19","techer",NULL,NULL
};
 
struct tree *construct(
  struct tree *root, 
  struct tree *r, 
  struct tree *Data)
{
  if(!r)
  {
    r = (struct tree *)malloc(sizeof(struct tree));
    if(!r)
    {
      printf(" Memory allocation failed! ");
      exit(0);
    }
    r->left = NULL;
    r->right = NULL;
    strcpy(r->name,Data->name);
    strcpy(r->city,Data->city);
    strcpy(r->sex,Data->sex);
    strcpy(r->age,Data->age);
    strcpy(r->job,Data->job);
    if(!root)
      return r;
    if(strcmp(Data->name,root->name)<0)
      root->left = r;
    else
      root->right = r;
    return r;
  }
  if(strcmp(Data->name,r->name)<0)
    construct(r,r->left,Data);
  else
    construct(r,r->right,Data);
 
  return root;  
}
 
struct tree *Search(root,name)
struct tree *root;
char name[];
{
  struct tree *p;
  if(root == NULL)
    printf(" The tree is empty \n");
  p = root;
  while(strcmp(p->name,name)!=0)
  {
    if(strcmp(p->name,name)>0)
      p = p->left;
    else
      p = p->right;
    if(p == NULL)
      break;
  }
  return(p);
}
 
void print(struct tree *r)
{
  if(!r)
    return;
  print(r->left);
  printf("%s\n",r->name);
  print(r->right);
}
 
void print_currentData(struct tree *point)
{
  if(point == NULL)
    return;
  printf("   Name: %s\n",point->name);
  printf("   City: %s\n",point->city);
  printf("   Gender: %s\n",point->sex);
  printf("   Age: %s\n",point->age);
  printf("   Work: %s\n",point->job);
}
 
int main(void)
{
  int i;
  char c[10];
  char swap[20];
  char name[20];
  struct tree *root,*p;
  struct tree *temp;
  p = NULL;
  temp = NULL;
  root = NULL;
  for(i = 0;i<NUM;i++)
    root =construct(root,root,&Datas[i]);
  printf(" Available personnel information: \n");
  print(root);
  printf(" Please enter the name of the person you are looking for \n");
  scanf("%s",name);
  p = Search(root,name);
  if(p == NULL)
  {
    printf(" No such person is available \n");
    printf(" Whether to insert the person's data [y/n]\n");
    scanf("%s",c);
    if(strcmp(c,"y")==0)
    {
      temp = (struct tree *)malloc(sizeof(struct tree));
      if(!temp)
      {
        printf(" Memory allocation failed! ");
        exit(0);
      }
      printf(" Please enter the person's name: \n");
      scanf("%s",swap);
      strcpy(temp->name,swap);
      printf(" Please enter the city where the person lives: \n");
      scanf("%s",swap);
      strcpy(temp->city,swap);
      printf(" Please enter the gender of the person [Male/Female] : \n");
      scanf("%s",swap);
      strcpy(temp->sex,swap);
      printf(" Please enter the person's age: \n");
      scanf("%s",swap);
      strcpy(temp->age,swap);
      printf(" Please enter the person's job: \n");
      scanf("%s",swap);
      strcpy(temp->job,swap);
      temp->left = NULL;
      temp->right = NULL;
      root =construct(root,root,temp);
      print_currentData(temp);
      printf(" Available personnel information: \n");
      root = root;
      print(root);
    }
    else
      return 0;
  }
  print_currentData(p);
  return 1;
}

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: