C Program to Perform Insertion in Binary Search Tree

This is a C Program to perform insertion opertion in binary search tree. A new key is always inserted at leaf. We start searching a key from root till we hit a leaf node. Once a leaf node is found, the new node is added as a child of the leaf node.

Here is source code of the C Program to Perform Insertion in a BST. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct node {
  5.     int key;
  6.     struct node *left, *right;
  7. };
  8.  
  9. // A utility function to create a new BST node
  10. struct node *newNode(int item) {
  11.     struct node *temp = (struct node *) malloc(sizeof(struct node));
  12.     temp->key = item;
  13.     temp->left = temp->right = NULL;
  14.     return temp;
  15. }
  16.  
  17. // A utility function to do inorder traversal of BST
  18. void inorder(struct node *root) {
  19.     if (root != NULL) {
  20.         inorder(root->left);
  21.         printf("%d ", root->key);
  22.         inorder(root->right);
  23.     }
  24. }
  25.  
  26. /* A utility function to insert a new node with given key in BST */
  27. struct node* insert(struct node* node, int key) {
  28.     /* If the tree is empty, return a new node */
  29.     if (node == NULL)
  30.         return newNode(key);
  31.  
  32.     /* Otherwise, recur down the tree */
  33.     if (key < node->key)
  34.         node->left = insert(node->left, key);
  35.     else if (key > node->key)
  36.         node->right = insert(node->right, key);
  37.  
  38.     /* return the (unchanged) node pointer */
  39.     return node;
  40. }
  41.  
  42. // Driver Program to test above functions
  43. int main() {
  44.     /* Let us create following BST
  45.         50
  46.      /     \
  47.    30      70
  48.   /  \    /  \
  49. 20   40  60   80 */
  50.     struct node *root = NULL;
  51.     root = insert(root, 50);
  52.     insert(root, 30);
  53.     insert(root, 20);
  54.     insert(root, 40);
  55.     insert(root, 70);
  56.     insert(root, 60);
  57.     insert(root, 80);
  58.  
  59.     // print inoder traversal of the BST
  60.     inorder(root);
  61.  
  62.     return 0;
  63. }

Output:

$ gcc InsertionBST.c
$ ./a.out
 
20 30 40 50 60 70 80

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.