Tree traversal in C

#include <stdio.h>
#include <stdlib.h>

struct node {
  int item;
  struct node* left;
  struct node* right;
};

// Inorder traversal
void inorderTraversal(struct node* root) {
  if (root == NULL) return;
  inorderTraversal(root->left);
  printf("%d ->", root->item);
  inorderTraversal(root->right);
}

// preorderTraversal traversal
void preorderTraversal(struct node* root) {
  if (root == NULL) return;
  printf("%d ->", root->item);
  preorderTraversal(root->left);
  preorderTraversal(root->right);
}

// postorderTraversal traversal
void postorderTraversal(struct node* root) {
  if (root == NULL) return;
  postorderTraversal(root->left);
  postorderTraversal(root->right);
  printf("%d ->", root->item);
}

// Create a new Node
struct node* createNode(value) {
  struct node* newNode = malloc(sizeof(struct node));
  newNode->item = value;
  newNode->left = NULL;
  newNode->right = NULL;

  return newNode;
}

// Insert on the left of the node
struct node* insertLeft(struct node* root, int value) {
  root->left = createNode(value);
  return root->left;
}

// Insert on the right of the node
struct node* insertRight(struct node* root, int value) {
  root->right = createNode(value);
  return root->right;
}

int main() {
  struct node* root = createNode(1);
  insertLeft(root, 12);
  insertRight(root, 9);

  insertLeft(root->left, 5);
  insertRight(root->left, 6);

  printf("Inorder traversal \n");
  inorderTraversal(root);

  printf("\nPreorder traversal \n");
  preorderTraversal(root);

  printf("\nPostorder traversal \n");
  postorderTraversal(root);
}

4
4
Krish 100200 points

                                    # include &lt;stdio.h&gt;
# include &lt;conio.h&gt;
# include &lt;stdlib.h&gt;
&nbsp;
typedef struct BST {
&nbsp;&nbsp; int data;
&nbsp;&nbsp; struct BST *lchild, *rchild;
} node;
&nbsp;
void insert(node *, node *);
void inorder(node *);
void preorder(node *);
void postorder(node *);
node *search(node *, int, node **);
&nbsp;
void main() {
&nbsp;&nbsp; int choice;
&nbsp;&nbsp; char ans = 'N';
&nbsp;&nbsp; int key;
&nbsp;&nbsp; node *new_node, *root, *tmp, *parent;
&nbsp;&nbsp; node *get_node();
&nbsp;&nbsp; root = NULL;
&nbsp;&nbsp; clrscr();
&nbsp;
&nbsp;&nbsp; printf(&quot;\nProgram For Binary Search Tree &quot;);
&nbsp;&nbsp; do {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n1.Create&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n2.Search&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n3.Recursive Traversals&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n4.Exit&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\nEnter your choice :&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(&quot;%d&quot;, &amp;choice);
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch (choice) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 1:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; do {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new_node = get_node();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\nEnter The Element &quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(&quot;%d&quot;, &amp;new_node-&gt;data);
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (root == NULL) /* Tree is not Created */
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; root = new_node;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; insert(root, new_node);
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\nWant To enter More Elements?(y/n)&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans = getch();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } while (ans == 'y');
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 2:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;\nEnter Element to be searched :&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scanf(&quot;%d&quot;, &amp;key);
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tmp = search(root, key, &amp;parent);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;\nParent of node %d is %d&quot;, tmp-&gt;data, parent-&gt;data);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 3:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (root == NULL)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Tree Is Not Created&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\nThe Inorder display : &quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inorder(root);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\nThe Preorder display : &quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;preorder(root);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\nThe Postorder display : &quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;postorder(root);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp; } while (choice != 4);
}
/*
 Get new Node
 */
node *get_node() {
&nbsp;&nbsp; node *temp;
&nbsp;&nbsp; temp = (node *) malloc(sizeof(node));
&nbsp;&nbsp; temp-&gt;lchild = NULL;
&nbsp;&nbsp; temp-&gt;rchild = NULL;
&nbsp;&nbsp; return temp;
}
/*
 This function is for creating a binary search tree
 */
void insert(node *root, node *new_node) {
&nbsp;&nbsp; if (new_node-&gt;data &lt; root-&gt;data) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (root-&gt;lchild == NULL)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; root-&gt;lchild = new_node;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; insert(root-&gt;lchild, new_node);
&nbsp;&nbsp; }
&nbsp;
&nbsp;&nbsp; if (new_node-&gt;data &gt; root-&gt;data) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (root-&gt;rchild == NULL)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; root-&gt;rchild = new_node;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; insert(root-&gt;rchild, new_node);
&nbsp;&nbsp; }
}
/*
 This function is for searching the node from
 binary Search Tree
 */
node *search(node *root, int key, node **parent) {
&nbsp;&nbsp; node *temp;
&nbsp;&nbsp; temp = root;
&nbsp;&nbsp; while (temp != NULL) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (temp-&gt;data == key) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;\nThe %d Element is Present&quot;, temp-&gt;data);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return temp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*parent = temp;
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (temp-&gt;data &gt; key)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; temp = temp-&gt;lchild;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; temp = temp-&gt;rchild;
&nbsp;&nbsp; }
&nbsp;&nbsp; return NULL;
}
/*
 This function displays the tree in inorder fashion
 */
void inorder(node *temp) {
&nbsp;&nbsp; if (temp != NULL) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inorder(temp-&gt;lchild);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;%d&quot;, temp-&gt;data);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inorder(temp-&gt;rchild);
&nbsp;&nbsp; }
}
/*
 This function displays the tree in preorder fashion
 */
void preorder(node *temp) {
&nbsp;&nbsp; if (temp != NULL) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;%d&quot;, temp-&gt;data);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;preorder(temp-&gt;lchild);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;preorder(temp-&gt;rchild);
&nbsp;&nbsp; }
}
&nbsp;
/*
 This function displays the tree in postorder fashion
 */
void postorder(node *temp) {
&nbsp;&nbsp; if (temp != NULL) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;postorder(temp-&gt;lchild);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;postorder(temp-&gt;rchild);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;%d&quot;, temp-&gt;data);
&nbsp;&nbsp; }
}

4 (4 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
inorder traversal c program recursive tree traversal program function in c for tree traversal C program to visit the binary tree using various tree traversals write a c program for binary tree traversal what is traversing in c preorder traversal tree in c tree traversal recursive in c binary tree creation and traversal in c traversing a binary tree with recursion tree traversal c code recursive traversal of binary tree BST traversal Without using recursion. post order traversal c binary tree traversal program in c preorder traversal c bst traversal without recursion binary search tree inorder traversal with recursion in c 1. Explain BST and in order traversal using recursion Binary Tree traversals using recursion Creation of binary tree and traversal recursive program in C++ how to use recursion in bst recursive functions to travers a binary tree create binary tree recursively function inorder cpp inorder traversal without recursion Preorder Traversal program in java tree traversal without recursion in c inorder, preorder postorder traversal without recursion in c preorder traversal without recursion in c construct the binary tree without recursion in C run through tree with recursion c algorith of preorder traverasal in binary searc treee recursive tree creation c preorder syntax tree recursive preorder traversal java printing the Inorder traversal of the Tree using the saved array create binary tree by taking inout in c inorder traversal without recursion in c in order tree traversal c traverse binary tree in c BST using Inorder traversal in c generate brinary tree using recurison insertion in binary search tree using recursion in C tree traversal in c bst traversal with recursion c
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source