/**
 * This function takes a dictionary tree and minimizes it using
 * Dominique Revuz's algorithm. 'used_inf_values' is used to mark
 * INF codes that are actually used in the .bin.
 */
void minimize_tree(struct dictionary_node* root,struct bit_array* used_inf_values,Abstract_allocator prv_alloc) {
u_printf("Minimizing...                      \n");
struct transition_list** transitions_by_height;
struct dictionary_node_transition** transitions;
//init_minimize_arrays(&transitions_by_height,&transitions);

init_minimize_arrays_transition_list(&transitions_by_height);


unsigned int H=sort_by_height(root,transitions_by_height,used_inf_values,prv_alloc);

unsigned int nb=0;
for (unsigned int k1=0;k1<=H;k1++) {
	unsigned int nbcur = convert_list_to_array_size(k1,transitions_by_height);
	if (nbcur>nb) {
		nb = nbcur;
	}
}
init_minimize_arrays_dictionary_node_transition(&transitions,nb);
float z;
for (unsigned int k=0;k<=H;k++) {
   int size=convert_list_to_array(k,transitions_by_height,transitions,nb,prv_alloc);
   for (int l=0;l<size;l++) {
	   check_nodes(transitions[l]);
   }
   quicksort(0,size-1,transitions);
   merge(size,transitions,prv_alloc);
   z=(float)(100.0*(float)(k)/(float)H);
   if (z>100.0) z=(float)100.0;
   u_printf("%2.0f%% completed...    \r",z);
}
u_printf("Minimization done.                     \n");
free_minimize_arrays(transitions_by_height,transitions);
}
/**
 * This function explores the dictionary and puts its transitions into the
 * 'transitions' array. For a given height, the transitions are not sorted,
 * they will be later in the 'minimize_tree' function.
 * The function returns the height of the given node.
 */
static int sort_by_height(struct dictionary_node* n,struct transition_list** transitions_by_height,
					struct bit_array* used_inf_values,Abstract_allocator prv_alloc) {
if (n==NULL) {
   fatal_error("NULL error in sort_by_height\n");
}
/* We mark used INF codes */
if (n->single_INF_code_list!=NULL) {
	set_value(used_inf_values,n->INF_code,1);
}
if (n->trans==NULL) {
   /* If the node is a leaf, we have nothing to do */
   return 0;
}
struct dictionary_node_transition* trans=n->trans;
int height=-1;
int k;
while (trans!=NULL) {
   /* We process recursively the node pointed out by the current transition */
   k=sort_by_height(trans->node,transitions_by_height,used_inf_values,prv_alloc);
   if (k==MAXIMUM_HEIGHT) {
      fatal_error("Maximum height reached in sort_by_height\n");
   }
   /* We insert the transition in the appropriate list */
   transitions_by_height[k]=new_transition_list(trans,transitions_by_height[k],prv_alloc);
   /* The height of the current node is the maximum of his sons' heights... */
   if (height<k) height=k;
   trans=trans->next;
}
/* ... +1 because of the current node itself */
return 1+height;
}
Example #3
0
File: kadai5.c Project: HanaHosoe/c
int main(void) {
  int i;
  int cont = 1;
  student std[5];      // 5人のデータを入力する
  
  for(i=0;i<5;i++){
    scanf("%s",std[i].name);
    scanf("%d",&std[i].height);
    scanf("%f",&std[i].weight);
    scanf("%ld",&std[i].schols);
  }
  
  while (cont) {
    printf("please select: sort by <1: height, 2:weight, 3:name, 0:quit>\n ");
    scanf("%d", &cont);
    switch (cont) {
    case 1:
      printf("height\n");
      sort_by_height(std, NUMBER);
      break;
    case 2:
      printf("weight\n");
      sort_by_weight(std, NUMBER);
      break;
    case 3:
      printf("name\n");
      sort_by_name(std, NUMBER);
      break;
    }

    if (cont) {
      puts("-----------------------------\n");
      for (i = 0; i < NUMBER; i++)
	printf("%-8s %6d%6.1f%7ld\n",
	       std[i].name,  std[i].height, std[i].weight, std[i].schols);
      puts("-----------------------------\n");
    }
  }

  
  return (0);
  
}