| | | 1 | | /* This file is a template for bst.c. Content will be filled by yagiz on 2025-12-29. */ |
| | | 2 | | #include <stdio.h> |
| | | 3 | | #include <stdlib.h> |
| | | 4 | | #include <string.h> |
| | | 5 | | #include "bst.h" |
| | | 6 | | |
| | 2 | 7 | | void initBST(BST *tree) { |
| | 2 | 8 | | tree->root = NULL; |
| | 2 | 9 | | tree->count = 0; |
| | 2 | 10 | | } |
| | | 11 | | |
| | | 12 | | // Helper: Create a new node |
| | 3 | 13 | | static BSTNode *createNode(Machine machine) { |
| | 3 | 14 | | BSTNode *newNode = (BSTNode *)malloc(sizeof(BSTNode)); |
| | | 15 | | |
| | 3 | 16 | | if (newNode == NULL) { |
| | 0 | 17 | | printf("CRITICAL ERROR: Memory allocation failed!\n"); |
| | 0 | 18 | | return NULL; |
| | | 19 | | } |
| | | 20 | | |
| | 3 | 21 | | newNode->data = machine; |
| | 3 | 22 | | newNode->left = NULL; |
| | 3 | 23 | | newNode->right = NULL; |
| | 3 | 24 | | return newNode; |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | // Helper: Recursive Insert |
| | 6 | 28 | | static bool insertRecursive(BSTNode **node, Machine machine) { |
| | | 29 | | // 1. Base Case: Found empty spot |
| | 6 | 30 | | if (*node == NULL) { |
| | 3 | 31 | | *node = createNode(machine); |
| | 3 | 32 | | return true; |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | // 2. Recursive Step |
| | 3 | 36 | | if (machine.machine_id < (*node)->data.machine_id) { |
| | 1 | 37 | | return insertRecursive(&(*node)->left, machine); |
| | 2 | 38 | | } else if (machine.machine_id > (*node)->data.machine_id) { |
| | 1 | 39 | | return insertRecursive(&(*node)->right, machine); |
| | | 40 | | } else { |
| | | 41 | | // 3. Duplicate Case |
| | 1 | 42 | | printf("ERROR: Machine ID %d already exists! duplicate rejected.\n", machine.machine_id); |
| | 1 | 43 | | return false; |
| | | 44 | | } |
| | | 45 | | } |
| | | 46 | | |
| | 4 | 47 | | bool insertMachine(BST *tree, Machine machine) { |
| | 4 | 48 | | bool success = insertRecursive(&tree->root, machine); |
| | | 49 | | |
| | 4 | 50 | | if (success) { |
| | 3 | 51 | | tree->count++; |
| | 3 | 52 | | printf("Inventory Added: %s (ID: %d)\n", machine.name, machine.machine_id); |
| | | 53 | | } |
| | | 54 | | |
| | 4 | 55 | | return success; |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | // Helper: Recursive Search |
| | 5 | 59 | | static Machine *searchRecursive(BSTNode *node, int id) { |
| | | 60 | | // Base Case: Empty or Found |
| | 5 | 61 | | if (node == NULL || node->data.machine_id == id) { |
| | 2 | 62 | | return (node == NULL) ? NULL : &node->data; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | // Navigation |
| | 3 | 66 | | if (id < node->data.machine_id) { |
| | 0 | 67 | | return searchRecursive(node->left, id); |
| | | 68 | | } else { |
| | 3 | 69 | | return searchRecursive(node->right, id); |
| | | 70 | | } |
| | | 71 | | } |
| | | 72 | | |
| | 2 | 73 | | Machine *searchMachine(BST *tree, int machine_id) { |
| | 2 | 74 | | Machine *result = searchRecursive(tree->root, machine_id); |
| | | 75 | | |
| | 2 | 76 | | if (result) { |
| | 1 | 77 | | printf("FOUND: %s in %s\n", result->name, result->location); |
| | | 78 | | } else { |
| | 1 | 79 | | printf("NOT FOUND: Machine ID %d\n", machine_id); |
| | | 80 | | } |
| | | 81 | | |
| | 2 | 82 | | return result; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | // Helper: In-Order Traversal (L-N-R) |
| | 0 | 86 | | static void printRecursive(BSTNode *node) { |
| | 0 | 87 | | if (node != NULL) { |
| | 0 | 88 | | printRecursive(node->left); |
| | 0 | 89 | | printf("[%d] %-20s | Location: %s\n", |
| | 0 | 90 | | node->data.machine_id, node->data.name, node->data.location); |
| | 0 | 91 | | printRecursive(node->right); |
| | | 92 | | } |
| | 0 | 93 | | } |
| | | 94 | | |
| | 0 | 95 | | void printInventory(BST *tree) { |
| | 0 | 96 | | printf("\n--- FACTORY INVENTORY (Sorted by ID) ---\n"); |
| | 0 | 97 | | printRecursive(tree->root); |
| | 0 | 98 | | printf("----------------------------------------\n"); |
| | 0 | 99 | | } |
| | | 100 | | |
| | 7 | 101 | | static void destroyRecursive(BSTNode *node) { |
| | 7 | 102 | | if (node != NULL) { |
| | 3 | 103 | | destroyRecursive(node->left); |
| | 3 | 104 | | destroyRecursive(node->right); |
| | 3 | 105 | | free(node); |
| | | 106 | | } |
| | 7 | 107 | | } |
| | | 108 | | |
| | 1 | 109 | | void destroyBST(BST *tree) { |
| | 1 | 110 | | destroyRecursive(tree->root); |
| | 1 | 111 | | tree->root = NULL; |
| | 1 | 112 | | tree->count = 0; |
| | 1 | 113 | | } |