Binary Search Tree : Insertion

  • + 0 comments

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

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

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

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

    }

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