/* AICE Tree * * Build a sorted binary tree for competency information * * Template for Fundamental Data Structures Lab 3 * Created by Nunthatinn Veerapaiboon, 2025-02-15 */ #include #include #include #define MAX_COMPETENCY_CODE 8 #define MAX_COMPETENCY_TITLE 64 // Define the structs here // Node Binary tree structure typedef struct NODE_BT { char competency_code[8]; char competency_title[64]; int credit; int year; int semester; struct NODE_BT *leftChildren; struct NODE_BT *rightChildren; } NODE_BT; // Declare a global variable for the root of the binary tree NODE_BT* root = NULL; /* Creates the node containing compentency's information **** You may change the type of the function to make it matches the function output **** */ NODE_BT * create_node(char competency_code[MAX_COMPETENCY_CODE], char competency_title[MAX_COMPETENCY_TITLE], int credit, int year, int semester) { NODE_BT* newNode = (NODE_BT*)malloc(sizeof(NODE_BT)); if (newNode == NULL) { printf("Memory allocation failed.\n"); exit(1); } strncpy(newNode->competency_code, competency_code, MAX_COMPETENCY_CODE - 1); newNode->competency_code[MAX_COMPETENCY_CODE - 1] = '\0'; strncpy(newNode->competency_title, competency_title, MAX_COMPETENCY_TITLE - 1); newNode->competency_title[MAX_COMPETENCY_TITLE - 1] = '\0'; newNode->credit = credit; newNode->year = year; newNode->semester = semester; newNode->leftChildren = NULL; newNode->rightChildren = NULL; return newNode; } /* Inserts the node into the sorted binary tree **** You may add the function's parameter **** */ void insert(NODE_BT* newNode, NODE_BT* currentNode) { if (root == NULL) { root = newNode; return; } // compare year if (currentNode->year < newNode->year) { if (currentNode->rightChildren == NULL) currentNode->rightChildren = newNode; else insert(newNode, currentNode->rightChildren); return; } if (currentNode->year > newNode->year) { if (currentNode->leftChildren == NULL) currentNode->leftChildren = newNode; else insert(newNode, currentNode->leftChildren); return; } // If years are equal, compare semester if (currentNode->semester < newNode->semester) { if (currentNode->rightChildren == NULL) currentNode->rightChildren = newNode; else insert(newNode, currentNode->rightChildren); return; } if (currentNode->semester > newNode->semester) { if (currentNode->leftChildren == NULL) currentNode->leftChildren = newNode; else insert(newNode, currentNode->leftChildren); return; } // If both year and semester are equal, compare competency codes for (int i = 0; i < MAX_COMPETENCY_CODE; i++) { if (currentNode->competency_code[i] < newNode->competency_code[i]) { if (currentNode->rightChildren == NULL) currentNode->rightChildren = newNode; else insert(newNode, currentNode->rightChildren); return; } if (currentNode->competency_code[i] > newNode->competency_code[i]) { if (currentNode->leftChildren == NULL) currentNode->leftChildren = newNode; else insert(newNode, currentNode->leftChildren); return; } } } /* Prints competencies in-order */ void print_subtree(NODE_BT* currentNode) { if(currentNode->leftChildren != NULL) { print_subtree(currentNode->leftChildren); } printf("%d %d %s %s\n", currentNode->year, currentNode->semester, currentNode->competency_code, currentNode->competency_title); if(currentNode->rightChildren != NULL) { print_subtree(currentNode->rightChildren); } } void print_competencies() { print_subtree(root); } /* Free the contents */ void freeBT(NODE_BT* node) { if (node == NULL) return; // Base case: empty node freeBT(node->leftChildren); // Free left subtree freeBT(node->rightChildren); // Free right subtree free(node); // Free the current node } // Wrapper function to free the entire tree void free_all() { freeBT(root); // Free all nodes starting from the root root = NULL; // Set root to NULL to prevent dangling reference } int main() { char competency_code[8]; char competency_title[64]; int credit; int year; int semester; // Spring = 0, Fall = 1 int num_operations; // The number of operations. scanf("%d", &num_operations); for (int i = 0; i < num_operations; i++) { scanf("%7s %63s %d %d %d", competency_code, competency_title, &credit, &year, &semester); NODE_BT * new_node = create_node(competency_code, competency_title, credit, year, semester); // You may change the datatype insert(new_node, root); // You may add the function's argument } print_competencies(); free_all(); }