binary search tree

operation insertion ,deletion, inorder, postorder, traversal
 avatar
user_9350232
plain_text
9 months ago
3.4 kB
8
Indexable
#include <iostream>
using namespace std;

// Structure for a BST node
struct Node {
    int data;
    Node* left;
    Node* right;
};

// Function to create a new node
Node* createNode(int value) {
    Node* newNode = new Node();
    newNode->data = value;
    newNode->left = newNode->right = NULL;
    return newNode;
}

// Function to insert a node into BST
Node* insert(Node* root, int value) {
    if (root == NULL)
        return createNode(value);

    if (value < root->data)
        root->left = insert(root->left, value);
    else if (value > root->data)
        root->right = insert(root->right, value);

    return root;
}

// Function to search for a node
bool search(Node* root, int key) {
    if (root == NULL)
        return false;
    if (root->data == key)
        return true;
    else if (key < root->data)
        return search(root->left, key);
    else
        return search(root->right, key);
}

// Function to find the minimum value node (for deletion)
Node* findMin(Node* root) {
    while (root && root->left != NULL)
        root = root->left;
    return root;
}

// Function to delete a node
Node* deleteNode(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 {
        // Node found
        if (root->left == NULL) {
            Node* temp = root->right;
            delete root;
            return temp;
        } else if (root->right == NULL) {
            Node* temp = root->left;
            delete root;
            return temp;
        }

        // Node with two children
        Node* temp = findMin(root->right);
        root->data = temp->data;
        root->right = deleteNode(root->right, temp->data);
    }
    return root;
}

// Traversal functions
void inorder(Node* root) {
    if (root != NULL) {
        inorder(root->left);
        cout << root->data << " ";
        inorder(root->right);
    }
}

void preorder(Node* root) {
    if (root != NULL) {
        cout << root->data << " ";
        preorder(root->left);
        preorder(root->right);
    }
}

void postorder(Node* root) {
    if (root != NULL) {
        postorder(root->left);
        postorder(root->right);
        cout << root->data << " ";
    }
}

// ---------------- MAIN ----------------
int main() {
    Node* root = NULL;

    // Fixed / Predefined values
    int values[] = {50, 30, 70, 20, 40, 60, 80};
    int n = 7;

    // Insert values into BST
    for (int i = 0; i < n; i++)
        root = insert(root, values[i]);

    cout << "Inorder Traversal: ";
    inorder(root);
    cout << "\nPreorder Traversal: ";
    preorder(root);
    cout << "\nPostorder Traversal: ";
    postorder(root);
    cout << endl;

    // Searching a fixed value
    int key = 40;
    if (search(root, key))
        cout << "\n" << key << " found in BST.";
    else
        cout << "\n" << key << " not found in BST.";

    // Deleting a fixed value
    cout << "\nDeleting 30 from BST...\n";
    root = deleteNode(root, 30);

    cout << "Inorder Traversal after deletion: ";
    inorder(root);
    cout << endl;

    cout << "\nTime Complexity: O(h)\nSpace Complexity: O(h)\n";
    return 0;
}
Editor is loading...
Leave a Comment