diff options
Diffstat (limited to 'comp2521/tf_idf/invertedIndexTree.c')
| -rw-r--r-- | comp2521/tf_idf/invertedIndexTree.c | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/comp2521/tf_idf/invertedIndexTree.c b/comp2521/tf_idf/invertedIndexTree.c new file mode 100644 index 0000000..b66d6a8 --- /dev/null +++ b/comp2521/tf_idf/invertedIndexTree.c @@ -0,0 +1,40 @@ +#include "invertedIndexTree.h" + +// Returns a newly malloc'ed inverted index node, fields initialised to zero +// except for word. +struct InvertedIndexNode *createInvertedIndexNode(char *const word) { + struct InvertedIndexNode *ret = calloc(1, sizeof(struct InvertedIndexNode)); + ret->word = word; + return ret; +} + +// Inserts to a FileListNode BST. Uses strcmp to determine priority, with +// negative values moving into the left and positive values into the right. +struct InvertedIndexNode * +insertInvertedIndexNode(struct InvertedIndexNode *node, char *const word) { + if (node == NULL) { + return createInvertedIndexNode(word); + } + + const int comparison = strcmp(word, node->word); + if (comparison < 0) { + node->left = insertInvertedIndexNode(node->left, word); + } + if (comparison > 0) { + node->right = insertInvertedIndexNode(node->right, word); + } + return node; +} + +// Searches for an invertedIndexNode via a string. +struct InvertedIndexNode * +searchInvertedIndexNode(struct InvertedIndexNode *head, char *const word) { + int comparison = strcmp(word, head->word); + if (head == NULL || !comparison) { + return head; + } + if (comparison < 0) { + return searchInvertedIndexNode(head->left, word); + } + return searchInvertedIndexNode(head->right, word); +} |
