{"id":6999,"date":"2025-06-07T00:01:11","date_gmt":"2025-06-07T07:01:11","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6999"},"modified":"2025-06-14T09:21:16","modified_gmt":"2025-06-14T16:21:16","slug":"climbing-the-binary-tree","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6999","title":{"rendered":"Climbing the Binary Tree"},"content":{"rendered":"<p>A binary search tree works best when code is available to search the tree. Before doing so, I&#8217;d like to demonstrate how a tree is processed, one node at a time.<br \/>\n<!--more--><br \/>\nContinuing from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6989\">last week&#8217;s Lesson<\/a>, to visit each node in the tree you must traverse the tree, plumbing its depths. I call this function <em>traverse()<\/em>:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* traverse the tree *\/<\/span>\r\nvoid traverse(struct node *r)\r\n{\r\n    <span class=\"comments\">\/* stop on NULL, empty node *\/<\/span>\r\n    if( r==NULL )\r\n        return;\r\n\r\n    <span class=\"comments\">\/* fetch data *\/<\/span>\r\n    printf(\"%d\\n\",r-&gt;key);\r\n    <span class=\"comments\">\/* go deeper! *\/<\/span>\r\n    traverse(r-&gt;right);\r\n    traverse(r-&gt;left);\r\n}<\/pre>\n<p>As with the <em>add_node()<\/em> function, <em>traverse()<\/em> is recursive. When a NULL node is encountered, indicating an empty item, the function returns. Otherwise, it fetches a key value from the node and then traverses first the right branches and then the left branches in the tree. Eventually, all values are output, though in a manner that dominates the right branches (because the right node is inspected first).<\/p>\n<p>Here&#8217;s sample output from the <a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_06_07-Lesson-a.c\" target=\"_new\" >updated code<\/a>, available on GitHub:<\/p>\n<p><code>50<br \/>\n85<br \/>\n96<br \/>\n59<br \/>\n68<br \/>\n20<br \/>\n41<br \/>\n16<br \/>\n3<\/code><\/p>\n<p>Compare this output with the way the data is stored internally, which is illustrated in Figure 1. You can see how the output favors climbing down the right branches first.<\/p>\n<div id=\"attachment_6991\" style=\"width: 592px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6991\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2025\/05\/0531-Figure1.png\" alt=\"\" width=\"582\" height=\"335\" class=\"size-full wp-image-6991\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2025\/05\/0531-Figure1.png 582w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2025\/05\/0531-Figure1-300x173.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2025\/05\/0531-Figure1-500x288.png 500w\" sizes=\"auto, (max-width: 582px) 100vw, 582px\" \/><p id=\"caption-attachment-6991\" class=\"wp-caption-text\">Figure 1. A Binary Search Tree.<\/p><\/div>\n<p>If you change the order of the <em>traverse()<\/em> recursive call, putting the <code>traverse(r-&gt;left)<\/code> statement first, the output appears in a different order. Regardless, these functions are used more properly to search for data than to output an accurate representation of the tree.<\/p>\n<p>The second improvement to the original code I promised is to free the allocated nodes before the program quits. As with all a program&#8217;s allocated storage, nodes are freed automatically when the program ends. Still, C coders are persnickety (as they should be) with regards to freeing memory.<\/p>\n<p>To de-allocate the binary search tree&#8217;s storage, I added the <em>free_tree()<\/em> function to the sample code:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* free the tree's node storage *\/<\/span>\r\nvoid free_tree(struct node *r)\r\n{\r\n    <span class=\"comments\">\/* stop on NULL *\/<\/span>\r\n    if( r==NULL )\r\n        return;\r\n\r\n    <span class=\"comments\">\/* plumb the depths *\/<\/span>\r\n    free_tree(r-&gt;left);\r\n    free_tree(r-&gt;right);\r\n    free(r);        <span class=\"comments\">\/* the (current) bottom node *\/<\/span>\r\n}<\/pre>\n<p>As you may suspect, this function is also recursive. It seeks the bottom-most (top-most?) node in the tree and frees it, <code>free(r)<\/code>. This function is added to the end of the <em>main()<\/em> function and is available on <a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_06_07-Lesson-b.c\" target=\"_new\" >GitHub here<\/a>.<\/p>\n<p>The next task is to search the tree for a specific tidbit of data. I present this addition to the code in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7015\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The beauty of the binary search tree is how quickly you can fetch its data. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6999\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-6999","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6999","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6999"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6999\/revisions"}],"predecessor-version":[{"id":7035,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6999\/revisions\/7035"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}