Problems in data structure - c

I'm making a data structure that stores strings from a file in memory. The struture is:
typedef struct node
{
bool end;
struct node *letters[27];
} node;
Each char from the string goes through a hash function:
int
hash(int letter)
{
int n;
// converts upper case to a number from 1 to 26
if (letter >= 65 && letter <= 90)
n = letter - 64;
// converts letter case to a number from 1 to 26
else if (letter >= 97 && letter <= 122)
n = letter - 96;
// converts apostrophe to 27
else if (letter == '\'')
n = 0;
return n;
}
Thus, the structure is similar to a tree where the position of each pointer in the array corresponds to a letter, as follows:
tree
The function that loads the words into memory is as follows:
bool
load(const char *dict)
{
// open the dictionary file
FILE *infile = fopen(dict, "r");
if (infile == NULL)
{
return false;
}
// pointer for the first tree
node *first = calloc(28, sizeof(node));
if (first == NULL)
return false;
//pointer to the nodes
node *nextptr = NULL;
// word storage variables
int index = 0;
char word[LENGTH+1];
// stores the words of the file in the struct
for (int c = fgetc(infile); c != EOF; c = fgetc(infile))
{
// reads only letters and apostrophes
if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122) || (c == '\''))
{
word[index] = c;
// creates a new tree from the first tree
if (index == 0)
{
// checks if there is a struct for the char
if (first->letters[hash(word[0])] != NULL)
{
nextptr = first->letters[hash(word[0])];
index++;
}
// creates a new struct for the char
else
{
first->letters[hash(word[0])] = calloc(28, sizeof(node));
if (first->letters[hash(word[0])] == NULL)
return false;
nextptr = first->letters[hash(word[0])];
index++;
}
}
// create the following structures
else
{
// checks if there is a struct for the char
if (nextptr->letters[hash(word[index])] != NULL)
{
nextptr = nextptr->letters[hash(word[index])];
index++;
}
// creates a new struct for the char
else
{
nextptr->letters[hash(word[index])] = calloc(28, sizeof(node));
if (nextptr->letters[hash(word[index])] == NULL)
return false;
nextptr = nextptr->letters[hash(word[index])];
index++;
}
}
}
// creates the last struct for a word
else if (c == '\n')
{
// ends the word
word[index] = '\0';
// the boolean end is set to true, defining the end of the word
nextptr->end = true;
nextptr = NULL;
// prepares for a new word
index = 0;
}
}
return true;
}
The function that has an error is an function that check strings and verify if is in the structure:
bool
check(const char *word)
{
node *checkptr = first;
for (int i = 0; i < sizeof(word); i++)
{
if (word[i] == '\0' && checkptr->end == true)
return true;
else if (checkptr->letters[hash(word[i])] != NULL)
checkptr = checkptr->letters[hash(word[i])];
else
return false;
}
return false;
}
When the program is started, segmentation fault occurs on the line else if (checkptr->letters[hash(word[i])] != NULL) and valgrind shows Invalid read of size 8.
I will still create a function to give the necessary free but I guess that the problem is there, mainly because I tried to check if the checkptr pointer was really set to the same structure as the first, but I discover that first is set to NULL, why?
Sorry for my bad english, i'm a beginner programmer and it's my first time on Stack Overflow, but I really don't know how to solve this. If someone can help me in any way, i thank you in advance.

The problem was solved. The error really was in the pointer first. I had accidentally used calloc for a new pointer, instead of the original first, so check was accessing the original first pointer (which had remained NULL).

Related

malloc() in C returns populated memory

char *string = (char *) malloc(sizeof(char) * sz);
code right before this->void insert_word(word *root, char string1[], int linenumber) { int sz=strlen(string1)<=MAX_WORD_LENGTH?strlen(string1):MAX_WORD_LENGTH; Code block 3 has the entire context
Sometimes malloc() returns a populated memory location while using it.
What bothers me is that this is not random.
(This program consists of taking words from a file and passing them to this function. For THE SAME WORD, the function behaviour(in particular that of malloc()) is different.
For the inputs
string1=0x7fffffffdf10 "lol" root=BST, sz gets a value of 3
The value allocated to string by malloc() is 0x55555555c510 "\340\305UUUU" Why is malloc not pointing to an empty memory location? (This is not random behaviour, it is predictable and repeatable)
Furthermore,this loop runs an infinite amount of time for some reason
while(strcmp(string1,string)!=0)
{
free(string);
string=NULL;
string = (char *) malloc(sizeof(char) * sz);
strncpy(string,string1,sz);
}
MORE RELAVANT CODE
#define MAX_WORD_LENGTH 20
Definition of the structures
typedef struct linkedList
{
int number;
struct linkedList *next;
}list;
typedef struct word_with_count
{
char* string;
list *linenumbers;
struct word_with_count *left;
struct word_with_count *right;
}word;```
[3] ) The function
void insert_word(word *root, char string1[], int linenumber) {
int sz=strlen(string1)<=MAX_WORD_LENGTH?strlen(string1):MAX_WORD_LENGTH;
char *string = (char *) malloc(sizeof(char) * sz);
strncpy(string,string1,sz);
if (root==NULL) {
return;
} else if (strcmp(string, root->string) < 0) {
if (root->left == NULL) {
root->left = createword(string, linenumber);
} else {
insert_word(root->left, string, linenumber);
}
} else if (strcmp(string, root->string) > 0) {
if (root->right == NULL) {
root->right = createword(string, linenumber);
} else {
insert_word(root->right, string, linenumber);
}
} else {
append_list(linenumber, root->linenumbers);
}
free(string);
}
main() which calls this function
int main() {
char path[MAX_PATH_LENGTH];
FILE *fp;
fgets(path, MAX_PATH_LENGTH, stdin);
if (strlen(path) > 0 && path[strlen(path) - 1] == '\n')
path[strlen(path) - 1] = '\0';
fp = fopen(path, "r");
if (fp == NULL) {
printf("File not found\n");
return 0;
}
char ch;
int line_count = 1;
char current_word[MAX_WORD_LENGTH] = "";
word *root = NULL;
while (!feof(fp)) {
ch = fgetc(fp);
//printf("%c", ch);
if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') {
if (ch >= 'A' && ch <= 'Z')
ch = ch - 'A' + 'a';
strncat(current_word, &ch, 1);
} else if (ch == '-') {
continue;
} else {
if (strlen(current_word) > 2) {
if (root == NULL) {
root = createword(current_word, line_count);
} else {
insert_word(root, current_word, line_count);
}
}
memset(current_word, 0, sizeof(current_word));
if (ch == '\n') {
line_count++;
}
}
}
if (strlen(current_word) > 2) {
if (root == NULL) {
root = createword(current_word, line_count);
} else {
insert_word(root, current_word, line_count);
}
}
fclose(fp);
// print_tree(root);
//printf("\n");
//print_tree(root);
int status=delete_low_ocurrence(root, NULL, 3);
if (status == -1)root = NULL;
print_tree(root);
freetree(root);
return 0;
}
5)Auxilary function used by this function
word* createword(char string[], int linenumber)
{
word *newword = (word*)malloc(sizeof(word));
int sz=strlen(string)<=MAX_WORD_LENGTH?strlen(string):MAX_WORD_LENGTH;
newword->string = (char*)malloc(sizeof(char)*sz);
strncpy(newword->string, string,sz);
newword->linenumbers = (list*)malloc(sizeof(list));
newword->linenumbers->number = linenumber;
newword->linenumbers->next = NULL;
newword->left = NULL;
newword->right = NULL;
return newword;
}
Textfile given as input
much2f
much3f
lol
lol
lol
qwertyuiopasdfghjklzxcvbnmqwertyuiop
qwertyuiopasdfghjklzxcvbnmqwertyuiop
qwertyuiopasdfghjklzxcvbnmqwertyuiop
qwertyuiopasdfghjklzxcvbnmqwertyuiop
Why is malloc not pointing to an empty memory location?
Because it can. The content of the allocated memory via malloc() is not specified.
If code needs zeroed out memory, see calloc().
Bad code
strncpy(string,string1,sz) does not result in string being a string as it may lack null character termination. The following (strcmp(string... is then undefined behavior. Instead, do not use strncpy(), use strcpy() and make certain the prior allocation has enough room for the terminating null character.
strncpy(string,string1,sz);
...
} else if (strcmp(string, root->string) < 0) { // bad
Repaired code
word* createword(const char string[], int linenumber) {
word *newword = calloc(1, sizeof *newword);
size_t length = strlen(string);
if (length > MAX_WORD_LENGTH) {
length = MAX_WORD_LENGTH;
}
char *s = malloc(length + 1); // Include room for the \0
list *linenumbers = calloc(1, sizeof *linenumbers);
// Test allocation success
if (newword == NULL || s == NULL || linenumbers == NULL) {
free(newword);
free(s);
free(linenumbers);
return NULL;
}
memcpy(s, string, length); // Only copy the first 'length' characters.
s[length] = 0;
newword->string = s;
newword->linenumbers = linenumbers;
newword->linenumbers->number = linenumber;
newword->linenumbers->next = NULL;
newword->left = NULL;
newword->right = NULL;
return newword;
}
Why is “while ( !feof (file) )” always wrong?
feof(fp) improperly used here. fgetc() returns 257 different values. Do not use char ch.
//char ch;
//...
//while (!feof(fp)) {
// ch = fgetc(fp);
int ch;
...
while ((ch = fgetc(fp)) != EOF) {;
This is quite normal behaviour. 'malloc' just does the memory allocation, it makes no commitment on what's already in that memory location. What you probably need is 'calloc', which clears the memory and then allocates it to your program.

char* function returning Null

I am currently working on a spam filter program that takes in and reads text files. In the initializeTraining function, I call the preprocess function which reads in each string from each line in a given text-file.
Once the newDict function is executed from the line first=newDict(string, NULL);, the program, however, returns an error stating that there is a load of null pointer of type 'char' at line while(string[i] !='\0' && i<WORDLENGTH) { in the newDict function.
It seems that the preprocess function is returning null pointers despite the fact that it still takes in the passed-in strings from the text file. Is there something that I'm doing wrong in the preprocess function?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
WORDLENGTH is the length of the word in the linked list named as dictionary
MAILSEPARATOR is the totken to differentiate the mails that are included in one file.
it is also the token to update updated in linkedlist
*/
#define WORDLENGTH 20
#define MAILSEPARATOR "#####"
/*
define DEBUG as 0 to disable debug mode and to 1 to enable the mode.
*/
#define DEBUG 0
typedef struct dictionary dict;
typedef dict* word_dict;
typedef enum {false, true} bool;
/*
linked list, count is for the total word count and
occur is the numbers of the mails that had the word
*/
struct dictionary{
char word[WORDLENGTH];
int occur;
int count;
word_dict next;
bool updated;
};
// if there is no matching words after searching, create a new node
word_dict newDict(char *string, word_dict next){
word_dict target = (word_dict)malloc(sizeof(dict));
int i = 0;
while(string[i] !='\0' && i<WORDLENGTH) {
target->word[i] = string[i];
i++;
}
target->count = 1;
target->next = next;
target->occur = 1;
target->updated = true;
return target;
}
/*
preprocessor, convert string to lowercase
and trim the puctuations at the back
*/
char* preprocess(char* string){
#if DEBUG
printf("\nbefore preprocess, string: %s \n", string);
#endif
int i=0;
while(string[i] != '\0') { // convert to lower case
if (string[i] >= 65 && string[i] < 90) {
string[i] += 32;
i++;
}
while(true) {
i--;
if(i < 0) {
#if DEBUG
printf("word of only punctuations \n");
#endif
return NULL;
} else if((string[i] >= 97 && string[i] <= 122) || (string[i] >= 48 && string[i] <= 57)){
string[i+1]='\0';
break;
}
}
i=0;
while(true) {
if ((string[i] >= 97 && string[i] <= 122) || (string[i] >= 48 && string[i] <= 57)){
break;
} else {
string = &string[i+1];
}
i++;
}
}
#if DEBUG
printf("_after preprocess, string: %s\n", string);
#endif
return string;
}
/*
initialize training
reads the sample mails and creates a linked list of
the percentages of the words occuring in the sample mails
*/
word_dict initializeTraining(char* filename){
FILE *fp = NULL;
fp = fopen(filename, "r");
if(fp == NULL) {
printf("no file found\n");
return NULL;
}
char* string;
string = (char*)malloc(sizeof(char)*50);
word_dict first = NULL;
fscanf(fp, "%s\n", string);
string = preprocess(string);
first = newDict(string, NULL);
while(fscanf(fp,"%s", string) == 1) {
first = searchDict(string, first);
}
fclose(fp);
free(string);
return first;
}
/*
tests whether the mail is pam or not
takes the filename of the test mail,
returns true or false depending on the email's content
*/
bool bayesian_spam_filter(char * filename_for_test_email) {
word_dict spamDict=initializeTraining("spam.txt");
word_dict nonspamDict=initializeTraining("not_spam.txt");
#if DEBUG
printDict(spamDict);
printDict(nonspamDict);
#endif
FILE *stream=NULL;
stream = fopen(filename_for_test_email, "r");
if(stream == NULL){
printf("no file found\n");
return false;
}
char* string;
string = (char*)malloc(sizeof(char)*50);
int ps, pn; // probability of spam mail and non-spam mail
double prob = 0.5;
while(fscanf(stream,"%s", string) == 1){
char* tempString; // for handling the errors happening from string being null during preprocessing
tempString = preprocess(string);
if(tempString == NULL){
continue;
}
if((ps = searchTest(tempString, spamDict)) != 0) {
if((pn = searchTest(tempString, nonspamDict)) != 0) {
printf("ps:%3d, pn:%3d, %s\n", ps, pn, tempString);
prob = prob * (double) ps / ((prob* (double)ps + (1 - prob) * (double) pn));
printf("this prob: %.10f\n", prob);
}
}
}
//printf("%d, %d \n", pSProduct, pNProduct);
//proba=(float)(pSProduct/(pSProduct+pNProduct));
printf("Probability of mail being spam: %.10f\n", prob);
fclose(stream);
free(string);
if (prob > 0.9) {
return true;
}
return false;
}
It seems that the preprocess function is returning null pointers
Hardly surprising when it contains a line return NULL;. At that point, you should instead set the first character of the string to '\0' and return it since the surrounding code expects to get the string returned in all cases.
Another problem can be found in this section:
i=0;
while(true) {
if ((string[i] >= 97 && string[i] <= 122) || (string[i] >= 48 && string[i] <= 57)){
break;
} else {
string = &string[i+1];
}
i++;
}
Let's say the string is ".a" and go through the loop. Since the first character is not a letter, we do not break, instead we update the string pointer so that it points to "a" now. Then, we increment i. In the next iteration, string[i] is the null terminator, which is not a letter, so we continue. Since we are past the string's data, what follows is undefined behavior.
The simple fix for this is to not increment i and rather stick with [0] since you always want to remove from the beginning. The proper fix is to use i but without incrementing the string pointer, since you want to free it later – you must call free on the pointer returned by malloc, therefore modifying the pointer leads to undefined behavior! Instead of returning the string from preprocess, return the offset from the start (as counted by i), this way freeing the string later will work properly. The calling code would then look like this:
int offset = preprocess(string);
first = newDict(string + offset, NULL);

Memory Leak using Tries - pset5 cs50 - dictionary.c

I wrote a code for pset5 in CS50 course and was wondering if anyone could help me solve the problem of memory leak.
There is a program called speller.c that gets words from a text and use functions in dictionary.c (below) to look at another file (dictionary) and if the word is mispelled or not in the dictionary, it will printf all those words and calculate the running time of the whole program.
My code is actually working fine and I do get the correct output, but when I use valgrind, it says that memory is leaking. Why is this hapenning?
/**
* CS50 - PSET5 - user: linhobru
* Implements a dictionary's functionality.
*/
#include <stdbool.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <sys/time.h>
#include "dictionary.h"
#define ALPHABETSIZE 27
typedef struct node
{
bool is_word;
struct node *children[ALPHABETSIZE];
}
node;
node *root = NULL;
int num_of_words = 0;
/**
* Returns true if word is in dictionary else false.
*/
bool check(const char *word)
{
node* next = root;
int k = ALPHABETSIZE;
for (int i = 0, len = strlen(word); i < len ; i++) // until (char) word[i] == '\0'
{
// verify letter and transform in index from 0 to 26
if (word[i] == '\'')
{
k = ALPHABETSIZE - 1;
}
else if (isalpha(word[i]))
{
k = tolower(word[i]) - 97;
}
if (next->children[k] == NULL)
{
return false;
}
else
{
next = next->children[k];
}
}
return next->is_word;
}
/**
* Loads dictionary into memory. Returns true if successful else false.
*/
bool load(const char *dictionary)
{
FILE *dict = fopen(dictionary, "r");
if (dict == NULL)
{
return false;
}
char word[LENGTH];
root = malloc(sizeof(node));
if (root == NULL)
{
unload();
fclose(dict);
return false;
}
while (fscanf(dict,"%s", word) != EOF)
{
node* now = root;
int k = ALPHABETSIZE;
for (int i = 0; word[i] != '\0'; i++)
{
// verify letter and transform in index from 0 to 26
if (word[i] == 39) // '\''
{
k = ALPHABETSIZE - 1;
}
else if (word[i] >= 97 && word[i] <= 122)
{
k = word[i] - 97;
}
/* else if (word[i] >= 65 && word[i] <= 90)
{
k = word[i] % 65;
}*/
else
{
fclose(dict);
return false;
}
if (now->children[k] == NULL)
{
node* new_node = malloc(sizeof(node));
now->children[k] = new_node;
}
now = now->children[k];
if (word[i + 1] == '\0')
{
now->is_word = true;
}
}
num_of_words++;
}
fclose(dict);
return true;
}
/**
* Returns number of words in dictionary if loaded else 0 if not yet loaded.
*/
unsigned int size(void)
{
return num_of_words;
}
/**
* Receives root at first and then each children to free.
*/
void unloadNode(node* next)
{
// call unloadNode on every node in this node's children
for(int i = 0; i < ALPHABETSIZE; i++)
{
if(next->children[i] != NULL)
{
unloadNode(next->children[i]);
}
}
// once the children nodes are freed, free this node
free(next);
}
/**
* Unloads dictionary from memory. Returns true if successful else false.
*/
bool unload(void)
{
unloadNode(root);
return true;
}
access to files in: https://github.com/linhobru/cs50psets/tree/master/workspace/pset5/speller/

While Loop stops before condition

I have a problem with my code. I want to load a dictionary which works fine with a small one. But when i try to load the larger version, my while loop stops at the 701th word which is " acclimatization" and then the programs continues. I searched a lot on forums and tried a lot of things, but i just can't find the reason this is caused. Does anyone have an idea of how this occurs?
Dictionary.c
bool load(const char* dictionary)
{
// reserve space for word
char* word = malloc(sizeof(char*));
// open file
FILE* dict = fopen(dictionary, "r");
if (dict == NULL)
{
fclose(dict);
fprintf(dict, "Could not load %s.\n", dictionary);
return 1;
}
root = (struct node *) malloc(sizeof(struct node));
root->is_word = false;
//Loops over word aslong the EOF is not reached
while (fgets(word,LENGTH,dict) != NULL)
{
printf("word = %s\n", word);
int word_length = strlen(word) -1;
node* current = root;
word_count++;
//Loops over letters
for (int i = 0; i < word_length; i++)
{
int index;
node *next_node;
// checks if letter isnt a apostrophe
if(word[i] == 39)
{
index = MAX_CHARS - 1;
}
// gets nummeric value of letter
else
{
index = tolower(word[i]) - 'a';
}
next_node = current->children[index];
// creates new node if letter didnt exists before
if(next_node == NULL)
{
next_node = malloc(sizeof(node));
current->children[index] = next_node;
current->is_word = false;
printf("new letter: %c\n", word[i]);
}
else
{
printf("letter: %c\n", word[i]);
}
// checks for end of the word
if(i == word_length - 1)
{
next_node->is_word = true;
}
current = next_node;
}
}
return true;
}
The node is defined by:
// node
typedef struct node
{
bool is_word;
struct node* children[27];
}
node;
char* word = malloc(sizeof(char*));
Depending on platform it can be 4 or 8 . You need to allocate more memory.
char* word;
word = malloc(LENGTH); // LENGTH as you use it here while (fgets(word,LENGTH,dict) != NULL)
if(word!=NULL){ // and checking if malloc is successful
// your code
free(word); // freeing allocated memory
return true;
}
else { // executed only if malloc fails
//handle error
}
You can give any desired size.
Note - Using function free() , you need to free every time you allocate memory.
You allocate very little space for word, it's probably 8 or 4 bytes depending on your platform.
You are allocating space for 1 char pointer, so when you read from the file LENGTH characters you can be storing bytes beyond the limits of the allocated buffer. The problem is, that the behavior is undefined thus the program might work or it might stop or anything can happen.
You don't need to allocate it dynamically, just like this it's ok
char word[100];
while (fgets(word, sizeof(word), file) != NULL) ...
/* ^ this only works with arrays, */
/* the benefit is that you can */
/* change the definition of word */
/* and resize it without changing */
/* this part. */
/* */
/* It will NOT work if you use `malloc()' */
Also, you would have a memory leak if fopen() failes, every malloc() requires a corresponding free().
Suggestion:
for (int i = 0; i < word_length; i++)
can be written like this too
for (int i = 0; ((word[i] != '\n') && (word[i] != '\0')); i++)
and you avoid calling strlen() which will also iterate through the characters.

algorithm to print trie alphabetically

I've been busy trying to code a trie ordered tree data structure in C. My program reads in words in a sentence one at a time from a .txt, then it stores each word in a trie without duplicates. It then grabs all other words in that sentence and stores them in a subtrie of the word that was stored. For example if we had the following sentence: "contribute to open source. " My code does the following...
root
ab'c'defghijklmn'o'pqr's''t'uvwxyz
'o' 'p' 'o''o'-subtrie-> "contribute", "open", "source"
'n' 'e' 'u'
't' 'n' 'r'
'r' 'c'-subtrie->"contribute", "to", "open",
'i'
'b'
'u'
't'
'e'-subtrie-> "to", "open", "source"
I have successfully been able to insert words into the trie and also the subtries. I have thoroughly tested this so I am pretty confident everything works the way it was intended to. However, i cant seem to figure out the algorithem to print the trie and subtrie alphabetically.
Here's the struct i am using
typedef struct TrieNode
{
// number of times this string occurs in the corpus
int count;
// 26 TrieNode pointers, one for each letter of the alphabet
struct TrieNode *children[26];
// the co-occurrence subtrie for this string
struct TrieNode *subtrie;
} TrieNode;
here is the function i wrote to insert tries. The parameters are the root of the trie, a char array of the word i want to insert, size of the word i am inserting, z = -1 initially.
TrieNode *trieInsert(TrieNode *root, char *wordArray, int sizeOfWord, int z){
z++;
int x1, j, index;
char c1 = wordArray[z];
//INSERT char second level
// do alphaNum conversions and check uper or lower case for lev1Char
x1 = char2Num(c1);
if(x1 >26 ){
printf("ERRRRRRRRRRRRRRRRRRrr:line475");
return root;
}
//base case
if( sizeOfWord == z )
return root;
//check to see if we already inserted this
if( root->children[x1] == NULL ){
//we insert second letter
root->children[x1] = malloc(sizeof(struct TrieNode) );
root->children[x1]->subtrie = NULL;
root->children[x1]->count = 0;
//set children of newly malloced to null
for(j = 0; j < 27; j++)
root->children[x1]->children[j] = NULL;
}
//increment word count on last char of word
if((sizeOfWord - 1) == z)
root->children[x1]->count++;
return trieInsert(root->children[x1], wordArray, sizeOfWord, z);
}
Here is the code i cant figure out. It was to print the trie alphabetically, however, it's output is incorrect.
void printTrieAlefBet( TrieNode *root ){
int i;
if( root->subtrie != NULL){
printf(" (%d)", root->count);
return;
}
for( i = 0; i < 27; i++)
if( root->children[i] != NULL){
printTrieAlefBet(root->children[i]);
printf("%c", num2Char(i, 0) );
}
}
Any thoughts would be greatly appreciated!
Got it to work. Here's the code.
Special thanks to prof Sean Szumlanski!
// Helper function called by `printTrie()`.
void printTrieHelper(TrieNode *root, char *buffer, int k)
{
int i;
if (root == NULL)
return;
if (root->count > 0)
printf("%s (%d)\n", buffer, root->count);
buffer[k + 1] = '\0';
for (i = 0; i < 26; i++)
{
buffer[k] = 'a' + (i - 1);
printTrieHelper(root->children[i], buffer, k + 1);
}
buffer[k] = '\0';
}
// If printing a subtrie, the second parameter should be 1; otherwise, 0.
void printTrie(TrieNode *root, int useSubtrieFormatting)
{
char buffer[1026];
if (useSubtrieFormatting)
{
strcpy(buffer, "- ");
printTrieHelper(root, buffer, 2);
}
else
{
strcpy(buffer, 'and');
printTrieHelper(root, buffer, 0);
}
}

Resources