Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
4.1 kB
1
Indexable
Never
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* left;
    struct Node* right;
};

struct Node* simpulawal(int data) {
    struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}

struct Node* insert(struct Node* root, int data) {
    if (root == NULL) {
        return simpulawal(data);
    }

    if (data < root->data) {
        root->left = insert(root->left, data);
    } else if (data > root->data) {
        root->right = insert(root->right, data);
    }
    return root;
}

struct Node* searchdata(struct Node* root, int data) {
    if (root == NULL || root->data == data) {
        return root;
    }
    printf("Data yang dilalui: %d\n", root->data);
    if (data < root->data) {
        return searchdata(root->left, data);
    } else {
        return searchdata(root->right, data);
    }
}

struct Node* findMin(struct Node* node) {
    struct Node* current = node;
    while (current && current->left != NULL) {
        current = current->left;
    }
    return current;
}

struct Node* deletion(struct Node* root, int data) {
    if (root == NULL) {
        return root;
    }
    if (data < root->data) {
        root->left = deletion(root->left, data);
    } else if (data > root->data) {
        root->right = deletion(root->right, data);
    } else {
        if (root->left == NULL) {
            struct Node* temp = root->right;
            printf("Data terhapus: %d\n", root->data);
            free(root);
            return temp;
        } else if (root->right == NULL) {
            struct Node* temp = root->left;
            printf("Data terhapus: %d\n", root->data);
            free(root);
            return temp;
        }
        struct Node* temp = findMin(root->right);
        root->data = temp->data;
        root->right = deletion(root->right, temp->data);
    }
    return root;
}

void printPreorder(struct Node* root) {
    if (root != NULL) {
        printf("%d ", root->data);
        printPreorder(root->left);
        printPreorder(root->right);
    }
}

void printTree(struct Node* root, int level) {
    if (root == NULL) {
        return;
    }
    printTree(root->right, level + 1);
    for (int i = 0; i < level; i++) {
        printf("    ");
    }
    printf("%d\n", root->data);
    printTree(root->left, level + 1);
}

int main() {
    struct Node* root = NULL;
    int choice, data;

    while (1) {
        printf("1. Insert\n");
        printf("2. Search\n");
        printf("3. Delete\n");
        printf("4. Cetak Pohon\n");
        printf("5. Cetak Secara Preorder\n");
        printf("6. Keluar\n");
        printf("Masukkan Menu: ");
        scanf("%d", &menu);

        switch (menu) {
            case 1:
                printf("Masukkan data yang diinginkan: ");
                scanf("%d", &data);
                root = insert(root, data);
                break;
            case 2:
                printf("Masukkan data yang ingin dicari: ");
                scanf("%d", &data);
                    struct Node* result = searchdata(root, data);
                if (result == NULL) {
                    printf("Data tidak ditemukan.\n");
                } else {
                    printf("Data ditemukkan.\n");
                }
                break;
            case 3:
                printf("Masukkan data yang ingin dihapus: ");
                scanf("%d", &data);
                root = deletion(root, data);
                printf("Data terhapus.\n");
                break;
            case 4:
                printf("Struktur Pohon BST:\n");
                printTree(root, 0);
                printf("\n");
                break;
            case 5:
                printf("Data BST Secara Preorder: ");
                printPreorder(root);
                printf("\n");
                break;
            case 6:
                printf("Program Selesai\n");
                exit(0);
            default:
                printf("Masukkan nomor yang valid.\n");
        }
    }
    return 0;
}