Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How do I store words in the left, root and right binary nodes?

sandratzsandratz Posts: 18Member
How do I store words in the left, root and right binary nodes using the interface below?

void IndexInit(Index *);
void IndexAppend(Index*, char*,int);
void IndexPrint(Index *);
Can someone tell me how to insert words and page numbers into the left root and right nodes of a binary tree? I know that the page numbers corresponding to the words will have to be stored in a linked list, but how? The words are read from a file, tokenised and stored in the tree and their corresponding page nos along with them.
I tried writing the code but it has the following errors:
It does not append words in the left and right nodes, it doesn't store anything in the linked list.
[code]
header file: asgn1.h
struct pagelist {
struct pagelist *next;
int page;
};

struct node {
char *word;
struct pagelist *head;
struct node *left;
struct node *right;
int size;
};

typedef struct node NODE;
typedef struct node *Index;

void IndexInit(Index *);
void IndexAppend(Index*, char*,int);
void IndexPrint(Index *);
int IndexQuery(Index*,char*,int**);
void IndexDestroy(Index *);

implementation: asgn1.c

#include
#include
#include
#include
#include "index1.h"

#define MAX 10000

static char buff[MAX] = {0};

void IndexInit(Index *);
void IndexAppend(Index *, char *,int);
void IndexPrint(Index *);
int IndexQuery(Index*,char*,int**);
int noise(char *s);
char *tokeniser(char **, char *);
char *char_in_string(char *, int );
int compareString(const char *, const char *);
int PageNumbers(struct pagelist *);
struct pagelist *addPageNo(int);
int findWord (Index *, char);
void printPageNumbers(struct pagelist *);
char *GetFile1(char *,int, FILE *);
char *GetFile2(char *,int, FILE *);

char *word;
char *s=NULL;
char *SEPARATORS = " ";

void IndexPrint(Index *);


void IndexInit(Index *ind) {


(*ind) -> word = NULL;
(*ind) -> size = 0;
printf("Init. success.");
}

void IndexAppend(Index *ind, char *word, int page) {
int diff;
struct pagelist *pl = malloc(sizeof(*pl));
*ind = (Index)malloc(sizeof(NODE));


diff = compareString((*ind)->word, word);
if ((*ind) !=NULL) {

(*ind)->word = word;
printf("%s ",(*ind)->word);
printf("%d
",page);
(*ind)->head = addPageNo(page);
IndexPrint(&ind);

}
else
if (diff > 0) {
IndexAppend(&((*ind)->left), word, page);
}
else
IndexAppend(&((*ind)->right), word, page);


}

void IndexPrint(Index *ind) {

if ((*ind) != NULL) {
printf("%s ", (ind)->word);
printPageNumbers((*ind)->head);
}
}


int IndexQuery(Index *ind,char *word,int **results) {

if (!findWord(&ind,*word)) {

printf("Word not found.");

}
else *results = findWord(&ind,*word);

return **results;
}


void IndexDestroy(Index *ind) {
if((*ind) != NULL) {
free(*ind);
*ind=NULL;
}
}




int noise(char *c) {
int found = 0;

while (GetFile2(buff, sizeof(buff),stdin) != NULL) {
s=buff;
while ((word=tokeniser(&s,SEPARATORS)) != NULL) {
if (c==s)
found=1;
}
}
return found;

}

char *tokeniser(char **s, char *delims) {
char *p = NULL;
char *q;
[code]




Sign In or Register to comment.