Пример #1
0
/** adds a tnode to a tree
 * @param current_tnode pointer to the Tnode which we are adding below (initially root node)
 */
Tnode* add_tnode(Tnode* current_tnode, char* value){
  if(current_tnode == NULL){ //if the pointer is null, allocate a new node
    create_tnode(current_tnode, value); 
  }
  else if(strcmp(current_tnode->data, value)<0){ //the string at current node comes before value
    current_tnode->right = add_tnode(current_tnode->right, value); //add node to the right subtree
  }
  else { //string at current node either matches or comes after value
    current_tnode->left = add_tnode(current_tnode->left, value); //add node to the left subtree
  }
}
Пример #2
0
/*prints all the files associated with ALL the words*/
int sa(char *str, tnode root) {
  char *word;
  int count = 0;
  lnode ptr; /* Iterates through filenames  */
  tnode t = create_tnode(); /*lets us keep a list*/

  if (strcmp(strtok(str, " "), "sa") != 0) {
    return 1;
  }


  /* goes through each word in input */
  while ((word = strtok(NULL, " "))) {
    /* goes through each filename for associated word */
    for (ptr = getFiles(word, root); ptr != NULL;
        ptr = ptr->next) {
      insert_to_list(t, ptr->filename);
    }
    count++;
  }



  /* prints all the filenames */
  for (ptr = t->files; ptr != NULL && ptr->count != count; ptr = ptr->next);
  if (ptr != NULL) {
    printf("%s", ptr->filename);
    for (ptr = ptr->next; ptr != NULL; ptr = ptr->next) {
      if(ptr->count == count)
        printf(", %s", ptr->filename);
    }
    printf("\n");
  }
  else {
    printf("No matches found\n");
  }

  destroyTree(t);
  return 0;
}