Question: Need help implementing the print contents and printTrieContents method in main method. Please comment if more information is needed. The printContents and printTrieContents are in
Need help implementing the print contents and printTrieContents method in main method.
Please comment if more information is needed. The printContents and printTrieContents are in bold.
/* This program indexes a web page, printing out the counts of words on that page */
#include
#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 */
int indexPage(const char* url, node* tNode);
int addWordOccurrence(const char* word, const int wordLength, node* tNode);
void printContents(struct trieNode* node , char arr[], int l);
void printTrieContents(struct trieNode* root);
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 */
node* tNode = newNode();
int result;
char word[200];
int current = 0;
if (argc > 1) {
result = indexPage(argv[1], tNode);
}
if (result) {
freeTrieMemory(tNode);
return 1;
}
//Print contents....
//Free Memory
freeTrieMemory(tNode);
return 0;
}
/* TODO: define the functions corresponding to the above prototypes */
/* TODO: change this return type */
int indexPage(const char* url, node* tNode)
{
const int bufferSize = 10000;
char buffer[10000];
char* temp;
int result = getText(url, buffer, bufferSize);
temp = strtok(buffer, " ");
int i = 0;
for (i; temp[i]; i++) {
temp[i] = toLower(temp[i]);
}
while (temp != NULL && !addWordOccurence(temp, tNode)) {
temp = strtok(NULL, " ");
int j = 0;
for (j; temp[j]; j++) {
temp[j] = toLower(temp[j]);
}
}
return addWordOccurence(temp, tNode) ? 1 : 0;
}
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;
}
//recursive helper method for printTrieContents
void printContents(struct trieNode* node , char arr[], int l){
if( node->isLeaf == 1)
{
arr[l] = '\0';
printf("%s", arr);
}
for (int i = 0; i < 26; i++)
{
if (node->children[i])
{
arr[l] = i + 'a';
printContents(node->children[i], arr, l + 1);
}
}
}
//print trie method,
void printTrieContents(struct trieNode* root)
{
int l = 0;
char arr[100];
printContents(root, arr, l);
}
//This method uses recursion to free the entire trie along with its children
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
Get step-by-step solutions from verified subject matter experts
