struct treenode *addtree2(struct treenode *node, char *word, int linenumber){

  int condition;

  if(node == NULL){
    //new word arrived so setup new struct and allocate memory
    node = talloc();
    node->word = stringduplicate(word);  //so we use strdup to get memory for string
    //had we not bothered and pointed to the string parameter (word), the memory
    //will be cleaned up after the function call allow the memory to be re-used
    //as it is from the stack
    //alloc gets heap which is there till we remove it
    node->count = 1;
    node->left = node->right = NULL;  //set pointers left & right to NULL
    node->startinglinelist = lalloc();
    node->startinglinelist->linenumbervalue = linenumber;
    node->startinglinelist->next = NULL;  
  } else if((condition = strcmp(word, node->word)) == 0){
    node->count++;
    linenode(node, linenumber);
  }else if(condition < 0)
    node->left = addtree2(node->left, word, linenumber);
  else
    node->right = addtree2(node->right, word, linenumber);

  return node;
}
Exemplo n.º 2
0
struct tnode2* addtree2(struct tnode2* p, int n, char* w){

  if ( p == NULL){
    p = (struct tnode2*) malloc(sizeof(struct tnode2));

    p->words = (char**) malloc(sizeof(char*));
    p->words[0] = (char*) malloc(strlen(w)+1);
    strcpy(p->words[0], w);

    p->count = n;
    p->size = 1;
    p->left = p->right = NULL;
  }
  else if ( p->count == n ){
    p->words = realloc(p->words, (p->size + 1) * sizeof(char*));
    p->words[p->size] = (char*) malloc(strlen(w)+1);  
    strcpy(p->words[p->size], w);
    (p->size)++;
  }
  else if ( p->count < n )
    p->left = addtree2(p->left, n, w);
  else
    p->right = addtree2(p->right, n, w);

  return p;
}
int ex6_3(int argc, char *argv[]){

  struct treenode *root;
  char word[100];
  int linenumber = 1;

  root = NULL;

  while(getwordch6(word, 100) != EOF){
    //in here we now need to deal with words returned with a \n in them, basically setting the \n to \0
    int len = strlen(word);
    int foundnewline = 0;

    if(word[len-1] == '\n'){
      foundnewline = 1;
      word[len-1] = '\0';
    }

    if(isalpha(word[0]) && !checkfornoisewords(word)){
      root = addtree2(root, word, linenumber);
    }

    if(foundnewline == 1){
      linenumber++;
      foundnewline = 0;
    }
  }

  treeprint2(root);

  return 0;
}
Exemplo n.º 4
0
struct tnode2* tnodetotnode2(struct tnode* ps){

  if (ps != NULL){
    tnodetotnode2(ps->left);
    root2 = addtree2(root2, ps->count, ps->word);
    tnodetotnode2(ps->right);
  }

  return root2;
}
int ex6_4(int argc, char *argv[]){

  struct treenode *root;
  char word[100];
  int linenumber = 1;

  root = NULL;

  while(getwordch6(word, 100) != EOF){
    int len = strlen(word);
    int foundnewline = 0;

    if(word[len-1] == '\n'){
      foundnewline = 1;
      word[len-1] = '\0';
    }

    if(isalpha(word[0]) && !checkfornoisewords(word)){
      root = addtree2(root, word, linenumber);
    }

    if(foundnewline == 1){
      linenumber++;
      foundnewline = 0;
    }
  }

  int i;
  //store nodes into the list above
  storenodes(root);
  printf("\n::Before sort::");
  for (i = 0; i < nodecount; i++){
    printf("\nCount: %d\tWord: %s\n", nodelist[i].count, nodelist[i].word);
  }
  sortnodelist();
  printf("\n::After sort::");
  for (i = 0; i < nodecount; i++){
    printf("\nCount: %d\tWord: %s\n", nodelist[i].count, nodelist[i].word);
  }

  return 0;
}