Complementary task for topic: 12

M Nemeth · 2023-08-29 15:21:04.635218'

Binary trees: basics

Binary trees: basics

Implement a simple binary search tree to store and manage integers. Create functions to insert nodes, search for values, print the tree in various traversal orders, and find the minimum and maximum values in the tree.

Hint: That is from lecture can be solved

Solution
#include 
#include 

// Define the structure for a node in the binary search tree
struct Node {
    int data;
    struct Node* left;
    struct Node* right;
};

// Function to create a new node with the given data
struct Node* createNode(int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->left = NULL;
    newNode->right = NULL;
    return newNode;
}

// Function to insert a new node with the given data into the binary search tree
struct Node* insertNode(struct Node* root, int data) {
    if (root == NULL) {
        return createNode(data);
    }

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

    return root;
}

// Function to search for a value in the binary search tree
struct Node* search(struct Node* root, int value) {
    if (root == NULL || root->data == value) {
        return root;
    }

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

// Function to print the binary search tree in inorder traversal (sorted order)
void inorderTraversal(struct Node* root) {
    if (root != NULL) {
        inorderTraversal(root->left);
        printf("%d ", root->data);
        inorderTraversal(root->right);
    }
}

// Function to print the binary search tree in preorder traversal
void preorderTraversal(struct Node* root) {
    if (root != NULL) {
        printf("%d ", root->data);
        preorderTraversal(root->left);
        preorderTraversal(root->right);
    }
}

// Function to print the binary search tree in postorder traversal
void postorderTraversal(struct Node* root) {
    if (root != NULL) {
        postorderTraversal(root->left);
        postorderTraversal(root->right);
        printf("%d ", root->data);
    }
}

// Function to find the minimum value in the binary search tree
int findMin(struct Node* root) {
    if (root == NULL) {
        printf("Error: Tree is empty.\n");
        return -1; // Return a negative value to indicate an error
    }

    while (root->left != NULL) {
        root = root->left;
    }

    return root->data;
}

// Function to find the maximum value in the binary search tree
int findMax(struct Node* root) {
    if (root == NULL) {
        printf("Error: Tree is empty.\n");
        return -1; // Return a negative value to indicate an error
    }

    while (root->right != NULL) {
        root = root->right;
    }

    return root->data;
}

// Function to destroy the entire binary search tree and free memory
void destroyTree(struct Node* root) {
    if (root != NULL) {
        destroyTree(root->left);
        destroyTree(root->right);
        free(root);
    }
}

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

    // Insert nodes into the binary search tree
    root = insertNode(root, 50);
    root = insertNode(root, 30);
    root = insertNode(root, 70);
    root = insertNode(root, 20);
    root = insertNode(root, 40);
    root = insertNode(root, 60);
    root = insertNode(root, 80);

    // Print the binary search tree in various traversal orders
    printf("Inorder traversal (sorted order): ");
    inorderTraversal(root);
    printf("\n");

    printf("Preorder traversal: ");
    preorderTraversal(root);
    printf("\n");

    printf("Postorder traversal: ");
    postorderTraversal(root);
    printf("\n");

    // Search for a value in the binary search tree
    int valueToSearch = 40;
    struct Node* foundNode = search(root, valueToSearch);
    if (foundNode != NULL) {
        printf("Value %d found in the tree.\n", valueToSearch);
    } else {
        printf("Value %d not found in the tree.\n", valueToSearch);
    }

    // Find the minimum and maximum values in the binary search tree
    int minValue = findMin(root);
    printf("Minimum value in the tree: %d\n", minValue);

    int maxValue = findMax(root);
    printf("Maximum value in the tree: %d\n", maxValue);

    // Destroy the binary search tree and free memory
    destroyTree(root);

    return 0;
}



Explanation

< < previous    next > >