首页 > 解决方案 > 在二叉搜索树中插入值字符串(节点)的算法

问题描述

我正在为大学考试开发一个程序,问题的特殊性要求:

1) 用户获取数值 n --> 完成

2) 随机生成 0 到 3n 之间的 n 个整数和 n 个 10 个字符的字符串 --> 完成

3)将所有生成数据对(数字,字符串)加载到二叉搜索树算法中。

4) 在屏幕上打印包含在树中的数据按照用户从预期、对称和延迟中选择的访问顺序。--> done--> 仅适用于 int 值。

5) 丝网打印按数字键排序的树中包含的数据(在我问这个问题之前我会尝试自己做)。

我已经有了允许我在树中插入值的算法,但是如何修改算法以允许我获取值字符串节点?

你能给我一些建议吗?

软件必须用c语言编写。我不确定是放全部还是只放一部分代码。

编辑 1)

不,现在我将尝试列出软件执行的步骤以使一切更清楚:1)软件要求用户键入有效的键盘值(> 0,整数)。2)软件会在屏幕上打印几个随机值,范围从0到用户输入的数字,乘以3,n串10个字符(见考试规范第2点)。例如,如果用户输入的数字是 4,软件将打印 0 到 12 之间的随机值,并且对于每个值,将对齐一个由 10 个随机字符组成的字符串。问题来了 * 第 3 点说:“将生成的所有数据对(数字、字符串)加载到二叉搜索树算法中”。* 因为第 3 点必须在第 4) 点之前完成,才能正确应用第 4) 点。为了让用户选择访问顺序,我创建了一个菜单(开关)。但是,目前我的软件只能部分应用第 4 点),事实上,它只在屏幕上打印 int 值而不是字符串,与 int 值对齐。

我已经知道我必须修改在树中插入值的算法,使程序除了获取值 int 还获取字符串。但我该怎么做呢?

点 2) 和 4) 只有 int 值

/*Programma contente un albero binario di ricerca che permette all'utente di stampare l'albero nel modo da lui richiesto*/
/*Inclusione librerie*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/*tree*/

typedef struct  albero_principale /*tree */
{
  int             valore; /* value */
  char            *CARATTERE; /* string */
  struct          albero_principale *sx_p, *dx_p;
} albero_principale_t;


/*function declarations*/
void visita_albero_ant(albero_principale_t *nodo_p); /* visit algorithm in early order */
void visita_albero_simm(albero_principale_t *nodo_p); /* visit algorithm in symmetrical order */
void visita_albero_post(albero_principale_t *nodo_p); /*  visit algorithm in delayed order /* 
albero_principale_t *cerca_in_albero_bin(albero_principale_t *nodo_p, int valore);  /* look for a value in the tree */
int inserisci_in_albero_bin_ric(albero_principale_t **radice_p, int valore); /*insert a value in the tree*/

/*main */
int main (int argc, char **argv)
{
  int num_input, num_casuale, esito_lettura, i, y, val, ordinamento;
  albero_principale_t *nodo, *radice;
  int *ARRAY;
  char *STRINGA;

  srand(time(NULL));  
  radice = NULL;
  ARRAY = (int*)malloc(sizeof(int)*num_input);
  STRINGA = (char*)malloc(sizeof(char)*num_input);


  do
    {
      printf("Digitare un input, maggiore di 0, valido: "); /* verify the validation of the input */
      esito_lettura = scanf ("%d",&num_input);
 inserisci_in_albero_bin_ric(&radice, ARRAY[i]);

      if (esito_lettura != 1 || num_input < 0)
           printf("input non valido.\n" );
      while (getchar() != '\n');
    }
    while (esito_lettura != 1 || num_input < 0);


  /*generation of n integers between 0 and 3n and n strings of 10 characters */  
  num_casuale = num_input * 3;
  printf("I valori generati saranno copresi tra 0 e %d. \n", num_casuale);
  for (i = 0 ;(i < num_input); i++)
  {
   ARRAY[i] = rand () % (num_casuale);
   printf("%d ", ARRAY[i]);

   for (y = 0; y < 5; y++)
   {
     STRINGA [y] = printf("%c ",'A'+rand()%('Z'-'A'));
     STRINGA [y+5] = printf("%c ",'a'+rand()%('a'-'z'));
   }
   printf("\n");
  }
   for(i = 0,
      (i < num_input);
      i++);
    {
             inserisci_in_albero_bin_ric(&radice, ARRAY[i]);
             inserisci_in_albero_bin_ric(&radice, STRINGA[i]);
   }



/*Menu that allows the user to choose the visit algorithm*/

  do
   {
    printf("\nI valori doppi saranno sovrapposti.\n");
    printf("Digita 1 per ordinare l'array con il metodo di visita in ordine anticipato\n");
    printf("Digita 2 per ordinare l'array con il metodo di visita in ordine simmetrica\n");
    printf("Digita 3 per ordinare l'array con il metodo di visita in ordine posticipato\n");
    printf("Digita 4 per terminare il programma\n"); /* quit */
    esito_lettura = scanf("%d", &ordinamento);

    switch (ordinamento)
      {

       case 1: printf("Visita anticipata:\n");
       visita_albero_ant(radice);
       break;

       case 2:printf("Visita simmetrica:\n");
       visita_albero_simm(radice);
       break;

       case 3:  printf("Visita posticipata:\n");
       visita_albero_post(radice);
       break;

       default:
       if (esito_lettura != 1 || num_input < 0)
            printf("input non valido.\n" );
       while (getchar() != '\n');
       break;

      }
    }  while(esito_lettura != 1 || ordinamento != 4 );

/*Algoritmo di ricerca
 printf("\nAlgoritmo di ricerca:");
 nodo = cerca_in_albero_bin(radice,val); */  

  return(0);
}

 int inserisci_in_albero_bin_ric(albero_principale_t **radice_p, int valore) /* insert a value in the tree */
{
  int inserito;
  albero_principale_t *nodo_p, *padre_p, *nuovo_p;

  for (nodo_p = padre_p = *radice_p;
    ((nodo_p != NULL) && (nodo_p->valore != valore));
    padre_p = nodo_p, nodo_p = (valore < nodo_p->valore)? nodo_p->sx_p:nodo_p -> dx_p);
  if (nodo_p != NULL)
    inserito = 0;
  else
 {
   inserito = 1;
   nuovo_p = (albero_principale_t *)malloc(sizeof(albero_principale_t));
   nuovo_p->valore = valore;
   nuovo_p->sx_p = nuovo_p->dx_p = NULL;
   if (nodo_p == *radice_p)
    *radice_p = nuovo_p;
   else
    if (valore < padre_p->valore)
      padre_p->sx_p = nuovo_p;
    else
      padre_p->dx_p = nuovo_p;
 }

 return(inserito);
}

/*visit algorithm in early order*/
void visita_albero_ant(albero_principale_t *nodo_p)
{
  if (nodo_p != NULL)
  {
    printf("%d ",nodo_p->valore); /* elabora */
    visita_albero_ant(nodo_p->sx_p);
    visita_albero_ant(nodo_p->dx_p);
  }
}

/*visit algorithm in symmetrical order*/
void visita_albero_simm(albero_principale_t *nodo_p)
  {
    if (nodo_p != NULL)
    {
      visita_albero_simm(nodo_p->sx_p);
      printf("%d ",nodo_p->valore); /* elabora */
      visita_albero_simm(nodo_p->dx_p);
    }
  }

/*visit algorithm in delayed order*/
void visita_albero_post(albero_principale_t *nodo_p)
{
  if (nodo_p != NULL)
  {
    visita_albero_post(nodo_p->sx_p);
    visita_albero_post(nodo_p->dx_p);
    printf("%d ", nodo_p->valore); /* elabora */
  }
}


/*look for a value in the tree*/
albero_principale_t *cerca_in_albero_bin(albero_principale_t *nodo_p, int valore)
{
    albero_principale_t *nodo_ris_p;

    if ((nodo_p == NULL) || (nodo_p->valore == valore))
        nodo_ris_p = nodo_p;
        else
        {
          nodo_ris_p = cerca_in_albero_bin(nodo_p->sx_p,valore);
          if (nodo_ris_p == NULL)
            nodo_ris_p = cerca_in_albero_bin(nodo_p->dx_p, valore);
        }
    return(nodo_ris_p);
}

标签: calgorithmtreeinsertion

解决方案


推荐阅读