C Program to Print Border of given Tree in Anticlockwise Direction

This C Program print border of given tree in anticlockwise direction.

Here is source code of the C Program to print border of given tree in anticlockwise direction. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Print Border of given Tree in Anticlockwise Direction 
  3.  *              50
  4.  *             /  \
  5.  *            20  30
  6.  *           /  \
  7.  *          70  80
  8.  *         / \    \
  9.  *       10  40   60                                
  10.  */            
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. struct btnode {
  15.     int value;
  16.     struct btnode *l;
  17.     struct btnode *r;
  18. };
  19. struct btnode *root;
  20. typedef struct btnode bt;
  21. bt *new, *ptr, *ptr1, *ptr2;
  22.  
  23. void print();
  24. void print_leaf_nodes(bt*);
  25. void print_right_recursive(bt*);
  26. bt* create();
  27. void construct_binary_tree();
  28.  
  29. void main()
  30. {
  31.     construct_binary_tree();
  32.     printf("\nprinting the border elements anticlockwise direction:\n");
  33.     print();
  34.     printf("\n");
  35. }
  36.  
  37. bt* create()
  38. {
  39.     new = (bt *)malloc(sizeof(bt));
  40.     new->l = NULL;
  41.     new->r = NULL;
  42.     return new;
  43. }
  44.  
  45. void construct_binary_tree()
  46. {
  47.     root = create();
  48.     root->value = 50;
  49.  
  50.     ptr = create();
  51.     root->l = ptr;
  52.     ptr->value = 20;
  53.     ptr1 = create();
  54.     ptr->l = ptr1;
  55.     ptr1->value = 70;
  56.     ptr2 = create();
  57.     ptr1->l = ptr2;
  58.     ptr2->value = 10;
  59.     ptr2 = create();
  60.     ptr1->r = ptr2;
  61.     ptr2->value = 40;
  62.     ptr1 = create();
  63.     ptr->r = ptr1;
  64.     ptr1->value = 80;
  65.     ptr2 = create();
  66.     ptr1->r = ptr2;
  67.     ptr2->value = 60;
  68.     ptr = create();
  69.     root->r = ptr;
  70.     ptr->value = 30;
  71. }
  72.  
  73. void print()
  74. {
  75.     ptr = root;
  76.     while (ptr->l != NULL)
  77.     {
  78.         printf("->%d", ptr->value);
  79.         ptr = ptr->l;
  80.     }
  81.     ptr = root;
  82.     print_leaf_nodes(ptr);
  83.     ptr = root;
  84.     print_right_recursive(ptr);
  85. }
  86.  
  87. void print_leaf_nodes(bt* ptr)
  88. {
  89.     if (ptr != NULL)
  90.     {
  91.         if (ptr->l == NULL && ptr->r == NULL)
  92.         {
  93.             printf("->%d", ptr->value);
  94.         }
  95.         else
  96.         {
  97.             print_leaf_nodes(ptr->l);
  98.             print_leaf_nodes(ptr->r);
  99.         }
  100.     }
  101.     else
  102.         return;
  103. }
  104.  
  105. void print_right_recursive(bt* ptr)
  106. {
  107.     int val;
  108.     ptr = ptr->r;
  109.     if (ptr->r != NULL)
  110.     {    
  111.         print_right_recursive(ptr->r);
  112.         printf("->%d", ptr->value);
  113.     }
  114.     else
  115.     {
  116.         return;
  117.     }
  118. }

 
         50
        /  \
       20   30
       / \
      70  80
     /  \   \
    10   40  60        
 
$ cc tree30.c
$ a.out
 
printing the border elements anticlockwise direction:
->50->20->70->10->40->60->30

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 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.