C Program to Implement Binary Tree using Linked List

This C Program implements binary tree using linked list. Binary Search tree is a binary tree in which each internal node x stores an element such that the element stored in the left subtree of x are less than or equal to x and elements stored in the right subtree of x are greater than or equal to x. This is called binary-search-tree property

Here is source code of the C Program to implement binary tree using linked list. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Implement Binary Tree using Linked List
  3.  */
  4. #include <stdio.h>
  5. #include <malloc.h>
  6.  
  7. struct node {
  8.     struct node * left;
  9.     char data;
  10.     struct node * right;
  11. };
  12.  
  13. struct node *constructTree( int );
  14. void inorder(struct node *);
  15.  
  16. char array[ ] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', '\0', '\0', 'H' };
  17. int leftcount[ ] = {  1,   3,   5,   -1,   9,  -1,  -1,   -1,   -1,  -1 };
  18. int rightcount[ ] = {  2,   4,   6,   -1,  -1,  -1,  -1,   -1,   -1,  -1 };
  19.  
  20. void main() {
  21.     struct node *root;
  22.     root = constructTree( 0 );
  23.     printf("In-order Traversal: \n");
  24.     inorder(root);
  25. }
  26.  
  27. struct node * constructTree( int index ) {
  28.     struct node *temp = NULL;
  29.     if (index != -1) {
  30.         temp = (struct node *)malloc( sizeof ( struct node ) );
  31.         temp->left = constructTree( leftcount[index] );
  32.         temp->data = array[index];
  33.         temp->right = constructTree( rightcount[index] );
  34.     }
  35.     return temp;
  36. }
  37.  
  38. void inorder( struct node *root ) {
  39.     if (root != NULL) {
  40.         inorder(root->left);
  41.         printf("%c\t", root->data);
  42.         inorder(root->right);
  43.     }
  44. }

$ cc pgm64.c
$ a.out
In-order Traversal:
D    B    H    E    A    F    C    G

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 wish to look at other example programs on Trees, go to Trees. If you wish to look at programming examples on all topics, go to C Programming Examples.

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.