• This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #1760
    admin
    Keymaster
      // Saves popular dog names in a trie
      #include <cs50.h>
      #include <ctype.h>
      #include <stdbool.h>
      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      #define SIZE_OF_ALPHABET 26
      #define MAXCHAR 20
      typedef struct node
      {
          bool is_word;
          struct node *children[SIZE_OF_ALPHABET];
      } node;
      // Function prototypes
      bool check(char *word);
      bool unload(void);
      void unloader(node *current);
      // Root of trie
      node *root;
      // Buffer to read dog names into
      char name[MAXCHAR];
      int main(int argc, char *argv[])
      {
          // Check for command line args
          if (argc != 2)
          {
              printf(“Usage: ./trie infile\n”);
              return 1;
          }
          // File with names
          FILE *infile = fopen(argv[1], “r”);
          if (!infile)
          {
              printf(“Error opening file!\n”);
              return 1;
          }
          // Allocate root of trie
          root = malloc(sizeof(node));
          if (root == NULL)
          {
              return 1;
          }
          root->is_word = false;
          for (int i = 0; i < SIZE_OF_ALPHABET; i++)
          {
              root->children = NULL;
          }
          // Add words to the trie
          while (fscanf(infile, “%s”, name) == 1)
          {
              node *cursor = root;
              for (int i = 0, n = strlen(name); i < n; i++)
              {
                  int index = tolower(name) – ‘a’;
                  if (cursor->children[index] == NULL)
                  {
                      // Make node
                      node *new = malloc(sizeof(node));
                      new->is_word = false;
                      for (int j = 0; j < SIZE_OF_ALPHABET; j++)
                      {
                          new->children[j] = NULL;
                      }
                      cursor->children[index] = new;
                  }
                  // Go to node which we may have just been made
                  cursor = cursor->children[index];
              }
              // if we are at the end of the word, mark it as being a word
              cursor->is_word = true;
          }
          if (check(get_string(“Check word: “)))
          {
              printf(“Found!\n”);
          }
          else
          {
              printf(“Not Found.\n”);
          }
          if (!unload())
          {
              printf(“Problem freeing memory!\n”);
              return 1;
          }
          fclose(infile);
      }
      // TODO: Complete the check function, return true if found, false if not found
      bool check(char *word)
      {
           strcmp()
          return true;
              }
          return false;
      }
      // Unload trie from memory
      bool unload(void)
      {
          // The recursive function handles all of the freeing
          unloader(root);
          return true;
      }
      void unloader(node *current)
      {
          // Iterate over all the children to see if they point to anything and go
          // there if they do point
          for (int i = 0; i < SIZE_OF_ALPHABET; i++)
          {
              if (current->children != NULL)
              {
                  unloader(current->children);
              }
          }
          // After we check all the children point to null we can get rid of the node
          // and return to the previous iteration of this function.
          free(current);
      }
      My query is regarding bool unload(void); function. Is the parameter for it set void because it is taking input of root node from global variable root.

      Replied by ChatGPT:

      Yes, the `unload` function is designed to take no parameters because it is intended to operate on the global variable `root`. In this case, `root` is a pointer to the root of the trie data structure. The `unload` function uses a recursive approach to free the memory associated with each node in the trie.

      Since the `root` variable is a global variable, it can be accessed directly within the `unload` function without the need to pass it as a parameter. This is a common practice when working with tree structures or other data structures where you need to traverse and modify nodes recursively.

      In C, when you pass a pointer to a function, you’re passing a copy of the pointer’s value, not the actual pointer itself. Since you want to modify the original pointer (set it to `NULL` after freeing all nodes), it’s more convenient to work with the global variable directly. That’s why the `unload` function takes no parameters.

    Viewing 1 post (of 1 total)
    • You must be logged in to reply to this topic.
    Scroll to Top