Kalp

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.9 kB
3
Indexable
Never
#include <stdio.h>
#include <stdlib.h>

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

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

struct Node* insert(struct Node* root, int key) {
    if (root == NULL) {
        return createNode(key);
    }
    if (key < root->data) {
        root->left = insert(root->left, key);
    }
    else if (key > root->data) {
        root->right = insert(root->right, key);
    }
    return root;
}

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

struct Node* deleteNode(struct Node* root, int key) {
    if (root == NULL) {
        return root;
    }
    if (key < root->data) {
        root->left = deleteNode(root->left, key);
    }
    else if (key > root->data) {
        root->right = deleteNode(root->right, key);
    }
    else {
        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;
        }
        struct Node* temp = minValueNode(root->right);
        root->data = temp->data;
        root->right = deleteNode(root->right, temp->data);
    }
    return root;
}

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

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

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

int findMin(struct Node* root) {
    if (root == NULL) {
        return -1; // Return -1 to indicate the tree is empty
    }
    while (root->left != NULL) {
        root = root->left;
    }
    return root->data;
}

int findMax(struct Node* root) {
    if (root == NULL) {
        return -1; // Return -1 to indicate the tree is empty
    }
    while (root->right != NULL) {
        root = root->right;
    }
    return root->data;
}

int findSecondMin(struct Node* root, int minVal) {
    if (root == NULL) {
        return -1; // Return -1 to indicate second minimum does not exist
    }
    if (root->data == minVal) {
        if (root->left == NULL) {
            return -1; // No left subtree, so no second minimum
        }
        int secondMin = findMin(root->left);
        if (secondMin == minVal) {
            // The left subtree contains only the minimum value, so we need to look in the right subtree.
            secondMin = findMin(root->right);
        }
        return secondMin;
    }
    else if (root->data > minVal) {
        // If the current node's data is greater than minVal, look for second minimum in the left subtree.
        int leftSecondMin = findSecondMin(root->left, minVal);
        if (leftSecondMin == -1) {
            // If left subtree doesn't have a second minimum, check the right subtree.
            int rightSecondMin = findSecondMin(root->right, minVal);
            return (rightSecondMin != -1) ? rightSecondMin : -1;
        }
        return leftSecondMin;
    }
    else {
        // The current node's data is less than minVal, so it's not a candidate for the second minimum.
        // Look for second minimum in the right subtree.
        return findSecondMin(root->right, minVal);
    }
}

int main() {
    struct Node* root = NULL;
    
    // Insert elements
    root = insert(root, 2);
    root = insert(root, 9);
    root = insert(root, 4);
    root = insert(root, 13);
    root = insert(root, 7);
    root = insert(root, 1);

    printf("Inorder Traversal: ");
    inorder(root);
    printf("\n");

    printf("Preorder Traversal: ");
    preorder(root);
    printf("\n");

    printf("Postorder Traversal: ");
    postorder(root);
    printf("\n");

    int minVal = findMin(root);
    int maxVal = findMax(root);
    printf("Minimum Value: %d\n", minVal);
    printf("Maximum Value: %d\n", maxVal);

    int secondMin = findSecondMin(root, minVal);
    if (secondMin != -1) {
        printf("Second Minimum Value: %d\n", secondMin);
    } else {
        printf("Second Minimum Value does not exist.\n");
    }

    // Delete elements
    root = deleteNode(root, 1);
    root = deleteNode(root, 4);

    printf("Inorder Traversal after deleting 1 and 4: ");
    inorder(root);
    printf("\n");

    // Clean up memory by freeing allocated nodes
    // Note: In a production environment, it's important to free the memory properly.
    
    return 0;
}