Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help writing a method that prints out the counts of all the words in the in the trie. I am providing the rest of

Need help writing a method that prints out the counts of all the words in the in the trie. I am providing the rest of my code in the file to you as to help with clarity and any methods that you may need to write the method.

method header----- void printTrieContents(node* trieNode);

IMPORTANT TO READ When printing out the counts of all the words in the trie, print out one line for each word with non-zero count, in alphabetical order. Do not print out words with 0 counts. To traverse the nodes in alphabetical order, you should print out the count for the root node of your trie (if non-zero), then recurse upon each child in alphabetical order. To keep track of the word associated with the current node, you can use a character buffer as a stack: every time you go down a level in the trie (i.e., make a recursive call), you add the corresponding character to the end of the buffer (i.e., push on the stack). When you go back up a level in the trie (i.e., return from a recursive call), you remove a character from the end of the buffer (i.e., pop off the stack). You should keep track of the buffer length and print an error message if the buffer is too small.

-------------------------------------------------------------------------------------------------------------------------------------------- (My code)

#include

#include

#include

/* TODO: structure definitions */

typedef struct trieNode{

int isLeaf;

int count;

struct trieNode* children[26];

}node;

//This method makes a new node with the given input

node* newNode() {

node* newNode = (node*) malloc(sizeof(node));

newNode->isLeaf = 0;

newNode->count = 0;

int i = 0;

for (i; i < 26; i++) {

node->children[i] = null;

}

return node;

}

/* NOTE: int return values can be used to indicate errors (typically non-zero)

or success (typically zero return value) */

/* TODO: change this return type */

char* indexPage(const char* url);

int addWordOccurrence(const char* word, const int wordLength, node* tNode);

void printTrieContents(node* trieNode);

int freeTrieMemory(*node trieNode);

int getText(const char* srcAddr, char* buffer, const int bufSize);

int main(int argc, char** argv){

/* TODO: write the (simple) main function

/* argv[1] will be the URL to index, if argc > 1 */

return 0;

}

/* TODO: define the functions corresponding to the above prototypes */

/* TODO: change this return type */

char* indexPage(const char* url)

{

}

int addWordOccurrence(const char* word, const int wordLength, node* tNode)

{

node* isNode = tNode;

while(*word) {

if (isNode->children[*word-'a'] == null) {

isNode->children[*word-'a'] = newNode();

if (isNode->children[*word-'a'] == null) {

return 1;

}

}

isNode = isNode->children[*word-'a'];

word++;

}

isNode->count++;

isLeaf = 1;

return 0;

}

void printTrieContents(node *trieNode)

{

//method i Need help writing...please read instructions given below question outline.

}

int freeTrieMemory(node* tNode)

{

if (tNode != null) {

int i = 0;

for (i; i < 26; i++) {

if (tNode->children[i] != NULL) {

freeTrieMemory(tNode->children[i]);

}

}

free(tNode);

tNode = null;

return 1;

}

return 0;

}

/* You should not need to modify this function */

int getText(const char* srcAddr, char* buffer, const int bufSize){

FILE *pipe;

int bytesRead;

snprintf(buffer, bufSize, "curl -s \"%s\" | python getText.py", srcAddr);

pipe = popen(buffer, "r");

if(pipe == NULL){

fprintf(stderr, "ERROR: could not open the pipe for command %s ",

buffer);

return 0;

}

bytesRead = fread(buffer, sizeof(char), bufSize-1, pipe);

buffer[bytesRead] = '\0';

pclose(pipe);

return bytesRead;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions