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

Howdy, Stranger!

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

Categories

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.