C Program to Implement Double Order Traversal of a Binary Tree

This is a C Program to print the double order traversal of the given tree.
Recurse through:
1. Visit root of (sub)tree.
2. Visit left sub-tree.
3. Revisit root of (sub)tree.
4. Visit right sub-tree.

Here is source code of the C Program to Implement Double Order Traversal of a Binary Tree. 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. /* A binary tree node has data, pointer to left child
  5.  and a pointer to right child */
  6. struct node {
  7.     int data;
  8.     struct node* left;
  9.     struct node* right;
  10. };
  11.  
  12. /* Helper function that allocates a new node with the
  13.  given data and NULL left and right pointers. */
  14. struct node* newNode(int data) {
  15.     struct node* node = (struct node*) malloc(sizeof(struct node));
  16.     node->data = data;
  17.     node->left = NULL;
  18.     node->right = NULL;
  19.  
  20.     return (node);
  21. }
  22.  
  23. /* Given a binary tree, print its nodes in doubleorder*/
  24. void printDoubleorder(struct node* node) {
  25.     if (node == NULL)
  26.         return;
  27.  
  28.     /* first print data of node */
  29.     printf("%d ", node->data);
  30.  
  31.     /* then recur on left sutree */
  32.     printDoubleorder(node->left);
  33.  
  34.     /* first print data of node */
  35.     printf("%d ", node->data);
  36.  
  37.     /* now recur on right subtree */
  38.     printDoubleorder(node->right);
  39. }
  40.  
  41. /* Driver program to test above functions*/
  42. int main() {
  43.     struct node *root = newNode(1);
  44.     root->left = newNode(2);
  45.     root->right = newNode(3);
  46.     root->left->left = newNode(4);
  47.     root->left->right = newNode(5);
  48.  
  49.     printf("\n Doubleorder traversal of binary tree is \n");
  50.     printDoubleorder(root);
  51.  
  52.     getchar();
  53.     return 0;
  54. }

Output:

$ gcc Doubleorder.c
$ ./a.out
 
 Preorder traversal of binary tree is 
1 2 4 4 2 5 5 1 3 3

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.