Untitled

Untitled

Untitled

Untitled

Untitled

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Structure for a BST node
struct Node {
    int year;
    int score;
    char name[50];
    struct Node *left;
    struct Node *right;
};

// Function to create a new node
struct Node *createNode(char name[], int year, int score) {
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));

    for (int i = 0; i < 50; i++) {
        if (int(name[i]) == 0|| int(name[i])==63||int(name[i]==32)) {
            break;
        }
        newNode->name[i] = name[i];
    }
    newNode->name[49] = '\\0';

    newNode->score = score;
    newNode->year = year;
    newNode->left = NULL;
    newNode->right = NULL;
    return newNode;
}

// Function to insert a node into the BST
struct Node *insert(struct Node *root, char name[], int year, int score) {
    if (root == NULL) {
        return createNode(name, year, score);
    }

    if (strcmp(name, root->name) < 0) {
        root->left = insert(root->left, name, year, score);
    } else if (strcmp(name, root->name) > 0) {
        root->right = insert(root->right, name, year, score);
    }

    return root;
}

// Function to check if a name exists in the BST
int isNameExists(struct Node *root, char name[]) {
    if (root == NULL) {
        return 0;
    }

    if (strcmp(name, root->name) == 0) {
        return 1; // Name exists
    }

    if (strcmp(name, root->name) < 0) {
        return isNameExists(root->left, name);
    } else {
        return isNameExists(root->right, name);
    }
}

void inOrder(struct Node *root) {
    // Base case
    if (root == NULL) {
        return;
    }
    // Travel the left sub-tree first.
    inOrder(root->left);
    printf("Name: ");
    // Print the current node value
    for (int i = 0; i < 50; i++) {
        if (int(root->name[i]) == 0|| int(root->name[i])==63||int(root->name[i])==32){
            break;
        }
        printf("%c", root->name[i]);
    }
    printf(", Data: %d \\n", root->score);
    // Travel the right sub-tree next.
    inOrder(root->right);
}

struct Node *search(struct Node *root, char name[]) {
    if (root == NULL) {
        printf("No Data Found\\n");
        return NULL;
    }

    if (strcmp(name, root->name) == 0) {
        // Print the current node value
        for (int i = 0; i < 50; i++) {
            if (int(root->name[i]) == 0) {
                break;
            }
            printf("%c", root->name[i]);
        }
        printf(", Data: %d \\n", root->score);
        return root;
    }

    if (strcmp(name, root->name) < 0) {
        return search(root->left, name);
    } else {
        return search(root->right, name);
    }
}

struct Node* findMin(struct Node* node) {
    struct Node* current = node;

    // Find the leftmost leaf node
    while (current && current->left != NULL) {
        current = current->left;
    }

    return current;
}

// Function to delete a node with a given key from the BST
struct Node* deleteNode(struct Node* root, char name[]) {
    if (root == NULL) {
        return root;
    }

    // If the key to be deleted is smaller than the root's key, then it lies in the left subtree
    if (strcmp(name, root->name) < 0) {
        root->left = deleteNode(root->left, name);
    }
    // If the key to be deleted is larger than the root's key, then it lies in the right subtree
    else if (strcmp(name, root->name) > 0) {
        root->right = deleteNode(root->right, name);
    }
    // If the key is the same as the root's key, then this is the node to be deleted
    else {
        // Node with only one child or no child
        if (root->left == NULL) {
            struct Node* temp = root->right;
            free(root);
            return temp;
        } else if (root->right == NULL) {
            struct Node* temp = root->left;
            free(root);
            return temp;
        }

        // Node with two children: Get the inorder successor (smallest in the right subtree)
        struct Node* temp = findMin(root->right);

        // Copy the inorder successor's content to this node
        strcpy(root->name, temp->name);
        root->year = temp->year;
        root->score = temp->score;

        // Delete the inorder successor
        root->right = deleteNode(root->right, temp->name);
    }

    return root;
}

// Function to delete a node by name
void deleteNodeByName(struct Node** root, char name[]) {
    if (*root == NULL) {
        printf("No Data Found\\n");
        return;
    }

    if (!isNameExists(*root, name)) {
        printf("Name not found\\n");
        return;
    }

    *root = deleteNode(*root, name);
    printf("Node with name %s deleted successfully\\n", name);
}

int main() {
    int ordercall = 0;
    char name[50];
    int year;
    int score;
    
    struct Node *root = NULL;
    root = insert(root, "Lin", 2, 81);
    root = insert(root, "Lee", 3, 70);
    root = insert(root, "Wang", 1, 58);
    root = insert(root, "Chen", 4, 77);
    root = insert(root, "Fan", 2, 64);
    root = insert(root, "Li", 3, 90);
    root = insert(root, "Pan", 1, 95);

    while (1) {
        printf("Main Menu\\n1. Show Data\\n2. Search\\n3. ADD\\n4. Delete\\n5. EXIT\\nEnter your choice: ");
        scanf("%d", &ordercall);

        switch (ordercall) {
            case 1:
                printf("Data in the Binary Search Tree\\n");
                inOrder(root);
                //            show data
                break;
            case 2:
                printf("Enter the Name to search: ");
                char rename[50];
                scanf("%s", &rename);
                search(root, rename);
                //        search
                break;
			case 3:
			    // Add
			    printf("Enter the NAME to add: ");
			    getchar(); // Consume the newline character left in the buffer
			    fgets(name, sizeof(name), stdin);
			    name[strcspn(name, "\\n")] = '\\0'; // Remove the newline character

                // Check if the name already exists
                if (isNameExists(root, name)) {
                    printf("Same name exists\\n");
                } else {
                    printf("Enter the YEAR: ");
                    scanf("%d", &year);
                    printf("Enter the SCORE to add: ");
                    scanf("%d", &score);
                    root = insert(root, name, year, score);
                }

                break;
            case 4:
                //        delete
                    // Delete
    	printf("Enter the NAME to delete: ");
    	scanf("%s", name);
    	deleteNodeByName(&root, name);
    	break;
                break;
            case 5:
                //        exit
                exit(0);
                break;
            //	        default:
            //	            break;
        }
    }
    return 0;
}

please note, li? has an error