aboutsummaryrefslogtreecommitdiff
path: root/comp2521/tf_idf/invertedIndexTree.c
diff options
context:
space:
mode:
authorNicolas James <Eele1Ephe7uZahRie@tutanota.com>2025-02-13 18:00:17 +1100
committerNicolas James <Eele1Ephe7uZahRie@tutanota.com>2025-02-13 18:00:17 +1100
commit98cef5e9a772602d42acfcf233838c760424db9a (patch)
tree5277fa1d7cc0a69a0f166fcbf10fd320f345f049 /comp2521/tf_idf/invertedIndexTree.c
initial commit
Diffstat (limited to 'comp2521/tf_idf/invertedIndexTree.c')
-rw-r--r--comp2521/tf_idf/invertedIndexTree.c40
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);
+}