TheAlgorithms-C/da/d02/binary__search__tree_8c.html
2023-09-08 21:40:54 +00:00

736 lines
60 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: data_structures/binary_trees/binary_search_tree.c File Reference</title>
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../resize.js"></script>
<script type="text/javascript" src="../../navtreedata.js"></script>
<script type="text/javascript" src="../../navtree.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectalign">
<div id="projectname">Algorithms_in_C<span id="projectnumber">&#160;1.0.0</span>
</div>
<div id="projectbrief">Set of algorithms implemented in C.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.8 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
initMenu('../../',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('da/d02/binary__search__tree_8c.html','../../'); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div class="header">
<div class="summary">
<a href="#nested-classes">Data Structures</a> &#124;
<a href="#typedef-members">Typedefs</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle"><div class="title">binary_search_tree.c File Reference</div></div>
</div><!--header-->
<div class="contents">
<p>A basic unbalanced binary search tree implementation in C.
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;stdio.h&gt;</code><br />
<code>#include &lt;stdlib.h&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for binary_search_tree.c:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d8/d1c/binary__search__tree_8c__incl.svg" width="194" height="126"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="nested-classes" name="nested-classes"></a>
Data Structures</h2></td></tr>
<tr class="memitem:"><td class="memItemLeft" align="right" valign="top">struct &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/da1/structnode.html">node</a></td></tr>
<tr class="memdesc:"><td class="mdescLeft">&#160;</td><td class="mdescRight"><a class="el" href="../../db/d8b/struct_node.html" title="Node, the basic data structure of the tree.">Node</a>, the basic data structure in the tree. <a href="../../d5/da1/structnode.html#details">More...</a><br /></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="typedef-members" name="typedef-members"></a>
Typedefs</h2></td></tr>
<tr class="memitem:af4aeda155dbe167f1c1cf38cb65bf324" id="r_af4aeda155dbe167f1c1cf38cb65bf324"><td class="memItemLeft" align="right" valign="top"><a id="af4aeda155dbe167f1c1cf38cb65bf324" name="af4aeda155dbe167f1c1cf38cb65bf324"></a>
typedef struct <a class="el" href="../../d5/da1/structnode.html">node</a>&#160;</td><td class="memItemRight" valign="bottom"><b>node</b></td></tr>
<tr class="memdesc:af4aeda155dbe167f1c1cf38cb65bf324"><td class="mdescLeft">&#160;</td><td class="mdescRight"><a class="el" href="../../db/d8b/struct_node.html" title="Node, the basic data structure of the tree.">Node</a>, the basic data structure in the tree. <br /></td></tr>
<tr class="separator:af4aeda155dbe167f1c1cf38cb65bf324"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:adf2169c1008551b20c4a59fcf0c9ab3b" id="r_adf2169c1008551b20c4a59fcf0c9ab3b"><td class="memItemLeft" align="right" valign="top"><a class="el" href="../../d5/da1/structnode.html">node</a> *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d02/binary__search__tree_8c.html#adf2169c1008551b20c4a59fcf0c9ab3b">newNode</a> (int <a class="el" href="../../df/dea/structdata.html">data</a>)</td></tr>
<tr class="memdesc:adf2169c1008551b20c4a59fcf0c9ab3b"><td class="mdescLeft">&#160;</td><td class="mdescRight">The node constructor, which receives the key value input and returns a node pointer. <br /></td></tr>
<tr class="separator:adf2169c1008551b20c4a59fcf0c9ab3b"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a031cc98830742bca57c0da81a0fb779f" id="r_a031cc98830742bca57c0da81a0fb779f"><td class="memItemLeft" align="right" valign="top"><a class="el" href="../../d5/da1/structnode.html">node</a> *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d02/binary__search__tree_8c.html#a031cc98830742bca57c0da81a0fb779f">insert</a> (<a class="el" href="../../d5/da1/structnode.html">node</a> *root, int <a class="el" href="../../df/dea/structdata.html">data</a>)</td></tr>
<tr class="memdesc:a031cc98830742bca57c0da81a0fb779f"><td class="mdescLeft">&#160;</td><td class="mdescRight">Insertion procedure, which inserts the input key in a new node in the tree. <br /></td></tr>
<tr class="separator:a031cc98830742bca57c0da81a0fb779f"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ad50f3debc5323bb7b71933effb64f30f" id="r_ad50f3debc5323bb7b71933effb64f30f"><td class="memItemLeft" align="right" valign="top"><a class="el" href="../../d5/da1/structnode.html">node</a> *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d02/binary__search__tree_8c.html#ad50f3debc5323bb7b71933effb64f30f">getMax</a> (<a class="el" href="../../d5/da1/structnode.html">node</a> *root)</td></tr>
<tr class="memdesc:ad50f3debc5323bb7b71933effb64f30f"><td class="mdescLeft">&#160;</td><td class="mdescRight">Utilitary procedure to find the greatest key in the left subtree. <br /></td></tr>
<tr class="separator:ad50f3debc5323bb7b71933effb64f30f"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a6983e8a91a3d25d7d000807f18ba2748" id="r_a6983e8a91a3d25d7d000807f18ba2748"><td class="memItemLeft" align="right" valign="top"><a class="el" href="../../d5/da1/structnode.html">node</a> *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d02/binary__search__tree_8c.html#a6983e8a91a3d25d7d000807f18ba2748">delete</a> (<a class="el" href="../../d5/da1/structnode.html">node</a> *root, int <a class="el" href="../../df/dea/structdata.html">data</a>)</td></tr>
<tr class="memdesc:a6983e8a91a3d25d7d000807f18ba2748"><td class="mdescLeft">&#160;</td><td class="mdescRight">Deletion procedure, which searches for the input key in the tree and removes it if present. <br /></td></tr>
<tr class="separator:a6983e8a91a3d25d7d000807f18ba2748"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:adff4c6248834a9944a1fb03a20230c9c" id="r_adff4c6248834a9944a1fb03a20230c9c"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d02/binary__search__tree_8c.html#adff4c6248834a9944a1fb03a20230c9c">find</a> (<a class="el" href="../../d5/da1/structnode.html">node</a> *root, int <a class="el" href="../../df/dea/structdata.html">data</a>)</td></tr>
<tr class="memdesc:adff4c6248834a9944a1fb03a20230c9c"><td class="mdescLeft">&#160;</td><td class="mdescRight">Search procedure, which looks for the input key in the tree and returns 1 if it's present or 0 if it's not in the tree. <br /></td></tr>
<tr class="separator:adff4c6248834a9944a1fb03a20230c9c"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ae4a66d8b0c2b0d626aea45977e358c83" id="r_ae4a66d8b0c2b0d626aea45977e358c83"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d02/binary__search__tree_8c.html#ae4a66d8b0c2b0d626aea45977e358c83">height</a> (<a class="el" href="../../d5/da1/structnode.html">node</a> *root)</td></tr>
<tr class="memdesc:ae4a66d8b0c2b0d626aea45977e358c83"><td class="mdescLeft">&#160;</td><td class="mdescRight">Utilitary procedure to measure the height of the binary tree. <br /></td></tr>
<tr class="separator:ae4a66d8b0c2b0d626aea45977e358c83"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a01151353aa2d9688934ed39208133241" id="r_a01151353aa2d9688934ed39208133241"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d02/binary__search__tree_8c.html#a01151353aa2d9688934ed39208133241">purge</a> (<a class="el" href="../../d5/da1/structnode.html">node</a> *root)</td></tr>
<tr class="memdesc:a01151353aa2d9688934ed39208133241"><td class="mdescLeft">&#160;</td><td class="mdescRight">Utilitary procedure to free all nodes in a tree. <br /></td></tr>
<tr class="separator:a01151353aa2d9688934ed39208133241"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a0f18adaaca5ecc410cfa16dd2a3684dc" id="r_a0f18adaaca5ecc410cfa16dd2a3684dc"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d02/binary__search__tree_8c.html#a0f18adaaca5ecc410cfa16dd2a3684dc">inOrder</a> (<a class="el" href="../../d5/da1/structnode.html">node</a> *root)</td></tr>
<tr class="memdesc:a0f18adaaca5ecc410cfa16dd2a3684dc"><td class="mdescLeft">&#160;</td><td class="mdescRight">Traversal procedure to list the current keys in the tree in order of value (from the left to the right) <br /></td></tr>
<tr class="separator:a0f18adaaca5ecc410cfa16dd2a3684dc"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4" id="r_ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d02/binary__search__tree_8c.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
<tr class="memdesc:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="mdescLeft">&#160;</td><td class="mdescRight">Main funcion. <br /></td></tr>
<tr class="separator:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p>A basic unbalanced binary search tree implementation in C. </p>
<p>The implementation has the following functionalities implemented:</p><ul>
<li>Insertion</li>
<li>Deletion</li>
<li>Search by key value</li>
<li>Listing of node keys in order of value (from left to right) </li>
</ul>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="a6983e8a91a3d25d7d000807f18ba2748" name="a6983e8a91a3d25d7d000807f18ba2748"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a6983e8a91a3d25d7d000807f18ba2748">&#9670;&#160;</a></span>delete()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname"><a class="el" href="../../d5/da1/structnode.html">node</a> * delete </td>
<td>(</td>
<td class="paramtype"><a class="el" href="../../d5/da1/structnode.html">node</a> *&#160;</td>
<td class="paramname"><em>root</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>data</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Deletion procedure, which searches for the input key in the tree and removes it if present. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">root</td><td>pointer to parent node </td></tr>
<tr><td class="paramname">data</td><td>value to search for int the node </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>pointer to parent node </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 89</span>{</div>
<div class="line"><span class="lineno"> 90</span> <span class="comment">// If the root is null, nothing to be done</span></div>
<div class="line"><span class="lineno"> 91</span> <span class="keywordflow">if</span> (root == NULL)</div>
<div class="line"><span class="lineno"> 92</span> {</div>
<div class="line"><span class="lineno"> 93</span> <span class="keywordflow">return</span> root;</div>
<div class="line"><span class="lineno"> 94</span> }</div>
<div class="line"><span class="lineno"> 95</span> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (<a class="code hl_struct" href="../../df/dea/structdata.html">data</a> &gt; root-&gt;data)</div>
<div class="line"><span class="lineno"> 96</span> { <span class="comment">// If the input key is greater than the root&#39;s, search in the right</span></div>
<div class="line"><span class="lineno"> 97</span> <span class="comment">// subtree</span></div>
<div class="line"><span class="lineno"> 98</span> root-&gt;right = <span class="keyword">delete</span> (root-&gt;right, <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>);</div>
<div class="line"><span class="lineno"> 99</span> }</div>
<div class="line"><span class="lineno"> 100</span> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (data &lt; root-&gt;<a class="code hl_struct" href="../../df/dea/structdata.html">data</a>)</div>
<div class="line"><span class="lineno"> 101</span> { <span class="comment">// If the input key is lower than the root&#39;s, search in the left subtree</span></div>
<div class="line"><span class="lineno"> 102</span> root-&gt;left = <span class="keyword">delete</span> (root-&gt;left, <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>);</div>
<div class="line"><span class="lineno"> 103</span> }</div>
<div class="line"><span class="lineno"> 104</span> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (<a class="code hl_struct" href="../../df/dea/structdata.html">data</a> == root-&gt;data)</div>
<div class="line"><span class="lineno"> 105</span> {</div>
<div class="line"><span class="lineno"> 106</span> <span class="comment">// If the input key matches the root&#39;s, check the following cases</span></div>
<div class="line"><span class="lineno"> 107</span> <span class="comment">// termination condition</span></div>
<div class="line"><span class="lineno"> 108</span> <span class="keywordflow">if</span> ((root-&gt;left == NULL) &amp;&amp; (root-&gt;right == NULL))</div>
<div class="line"><span class="lineno"> 109</span> { <span class="comment">// Case 1: the root has no leaves, remove the node</span></div>
<div class="line"><span class="lineno"> 110</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(root);</div>
<div class="line"><span class="lineno"> 111</span> <span class="keywordflow">return</span> NULL;</div>
<div class="line"><span class="lineno"> 112</span> }</div>
<div class="line"><span class="lineno"> 113</span> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (root-&gt;left == NULL)</div>
<div class="line"><span class="lineno"> 114</span> { <span class="comment">// Case 2: the root has one leaf, make the leaf the new root and</span></div>
<div class="line"><span class="lineno"> 115</span> <span class="comment">// remove</span></div>
<div class="line"><span class="lineno"> 116</span> <span class="comment">// the old root</span></div>
<div class="line"><span class="lineno"> 117</span> <a class="code hl_struct" href="../../d5/da1/structnode.html">node</a> *tmp = root;</div>
<div class="line"><span class="lineno"> 118</span> root = root-&gt;right;</div>
<div class="line"><span class="lineno"> 119</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(tmp);</div>
<div class="line"><span class="lineno"> 120</span> <span class="keywordflow">return</span> root;</div>
<div class="line"><span class="lineno"> 121</span> }</div>
<div class="line"><span class="lineno"> 122</span> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (root-&gt;right == NULL)</div>
<div class="line"><span class="lineno"> 123</span> {</div>
<div class="line"><span class="lineno"> 124</span> <a class="code hl_struct" href="../../d5/da1/structnode.html">node</a> *tmp = root;</div>
<div class="line"><span class="lineno"> 125</span> root = root-&gt;left;</div>
<div class="line"><span class="lineno"> 126</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(tmp);</div>
<div class="line"><span class="lineno"> 127</span> <span class="keywordflow">return</span> root;</div>
<div class="line"><span class="lineno"> 128</span> }</div>
<div class="line"><span class="lineno"> 129</span> <span class="keywordflow">else</span></div>
<div class="line"><span class="lineno"> 130</span> { <span class="comment">// Case 3: the root has 2 leaves, find the greatest key in the left</span></div>
<div class="line"><span class="lineno"> 131</span> <span class="comment">// subtree and switch with the root&#39;s</span></div>
<div class="line"><span class="lineno"> 132</span> </div>
<div class="line"><span class="lineno"> 133</span> <span class="comment">// finds the biggest node in the left branch.</span></div>
<div class="line"><span class="lineno"> 134</span> <a class="code hl_struct" href="../../d5/da1/structnode.html">node</a> *tmp = <a class="code hl_function" href="../../da/d02/binary__search__tree_8c.html#ad50f3debc5323bb7b71933effb64f30f">getMax</a>(root-&gt;left);</div>
<div class="line"><span class="lineno"> 135</span> </div>
<div class="line"><span class="lineno"> 136</span> <span class="comment">// sets the data of this node equal to the data of the biggest node</span></div>
<div class="line"><span class="lineno"> 137</span> <span class="comment">// (lefts)</span></div>
<div class="line"><span class="lineno"> 138</span> root-&gt;data = tmp-&gt;<a class="code hl_variable" href="../../d5/da1/structnode.html#a2d890bb9f6af0ffd73fe79b21124c2a2">data</a>;</div>
<div class="line"><span class="lineno"> 139</span> root-&gt;left = <span class="keyword">delete</span> (root-&gt;left, tmp-&gt;<a class="code hl_variable" href="../../d5/da1/structnode.html#a2d890bb9f6af0ffd73fe79b21124c2a2">data</a>);</div>
<div class="line"><span class="lineno"> 140</span> }</div>
<div class="line"><span class="lineno"> 141</span> }</div>
<div class="line"><span class="lineno"> 142</span> <span class="keywordflow">return</span> root;</div>
<div class="line"><span class="lineno"> 143</span>}</div>
<div class="ttc" id="abinary__search__tree_8c_html_ad50f3debc5323bb7b71933effb64f30f"><div class="ttname"><a href="../../da/d02/binary__search__tree_8c.html#ad50f3debc5323bb7b71933effb64f30f">getMax</a></div><div class="ttdeci">node * getMax(node *root)</div><div class="ttdoc">Utilitary procedure to find the greatest key in the left subtree.</div><div class="ttdef"><b>Definition</b> binary_search_tree.c:72</div></div>
<div class="ttc" id="amalloc__dbg_8h_html_a9cc854374299a1dd933bf62029761768"><div class="ttname"><a href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a></div><div class="ttdeci">#define free(ptr)</div><div class="ttdoc">This macro replace the standard free function with free_dbg.</div><div class="ttdef"><b>Definition</b> malloc_dbg.h:26</div></div>
<div class="ttc" id="astructdata_html"><div class="ttname"><a href="../../df/dea/structdata.html">data</a></div><div class="ttdef"><b>Definition</b> prime_factoriziation.c:25</div></div>
<div class="ttc" id="astructnode_html"><div class="ttname"><a href="../../d5/da1/structnode.html">node</a></div><div class="ttdoc">Node, the basic data structure in the tree.</div><div class="ttdef"><b>Definition</b> binary_search_tree.c:15</div></div>
<div class="ttc" id="astructnode_html_a2d890bb9f6af0ffd73fe79b21124c2a2"><div class="ttname"><a href="../../d5/da1/structnode.html#a2d890bb9f6af0ffd73fe79b21124c2a2">node::data</a></div><div class="ttdeci">int data</div><div class="ttdoc">data of the node</div><div class="ttdef"><b>Definition</b> binary_search_tree.c:18</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../da/d02/binary__search__tree_8c_a6983e8a91a3d25d7d000807f18ba2748_cgraph.svg" width="180" height="60"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div>
</div>
<a id="adff4c6248834a9944a1fb03a20230c9c" name="adff4c6248834a9944a1fb03a20230c9c"></a>
<h2 class="memtitle"><span class="permalink"><a href="#adff4c6248834a9944a1fb03a20230c9c">&#9670;&#160;</a></span>find()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int find </td>
<td>(</td>
<td class="paramtype"><a class="el" href="../../d5/da1/structnode.html">node</a> *&#160;</td>
<td class="paramname"><em>root</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>data</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Search procedure, which looks for the input key in the tree and returns 1 if it's present or 0 if it's not in the tree. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">root</td><td>pointer to parent node </td></tr>
<tr><td class="paramname">data</td><td>value to store int he new node </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>0 if value not found in the nodes </dd>
<dd>
1 if value was found </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 153</span>{</div>
<div class="line"><span class="lineno"> 154</span> <span class="comment">// If the root is null, the key&#39;s not present</span></div>
<div class="line"><span class="lineno"> 155</span> <span class="keywordflow">if</span> (root == NULL)</div>
<div class="line"><span class="lineno"> 156</span> {</div>
<div class="line"><span class="lineno"> 157</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 158</span> }</div>
<div class="line"><span class="lineno"> 159</span> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (<a class="code hl_struct" href="../../df/dea/structdata.html">data</a> &gt; root-&gt;data)</div>
<div class="line"><span class="lineno"> 160</span> {</div>
<div class="line"><span class="lineno"> 161</span> <span class="comment">// If the input key is greater than the root&#39;s, search in the right</span></div>
<div class="line"><span class="lineno"> 162</span> <span class="comment">// subtree</span></div>
<div class="line"><span class="lineno"> 163</span> <span class="keywordflow">return</span> <a class="code hl_function" href="../../da/d02/binary__search__tree_8c.html#adff4c6248834a9944a1fb03a20230c9c">find</a>(root-&gt;right, <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>);</div>
<div class="line"><span class="lineno"> 164</span> }</div>
<div class="line"><span class="lineno"> 165</span> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (data &lt; root-&gt;<a class="code hl_struct" href="../../df/dea/structdata.html">data</a>)</div>
<div class="line"><span class="lineno"> 166</span> {</div>
<div class="line"><span class="lineno"> 167</span> <span class="comment">// If the input key is lower than the root&#39;s, search in the left subtree</span></div>
<div class="line"><span class="lineno"> 168</span> <span class="keywordflow">return</span> <a class="code hl_function" href="../../da/d02/binary__search__tree_8c.html#adff4c6248834a9944a1fb03a20230c9c">find</a>(root-&gt;left, <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>);</div>
<div class="line"><span class="lineno"> 169</span> }</div>
<div class="line"><span class="lineno"> 170</span> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (<a class="code hl_struct" href="../../df/dea/structdata.html">data</a> == root-&gt;data)</div>
<div class="line"><span class="lineno"> 171</span> {</div>
<div class="line"><span class="lineno"> 172</span> <span class="comment">// If the input and the root key match, return 1</span></div>
<div class="line"><span class="lineno"> 173</span> <span class="keywordflow">return</span> 1;</div>
<div class="line"><span class="lineno"> 174</span> }</div>
<div class="line"><span class="lineno"> 175</span> <span class="keywordflow">else</span></div>
<div class="line"><span class="lineno"> 176</span> { <span class="comment">// unknown result!!</span></div>
<div class="line"><span class="lineno"> 177</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 178</span> }</div>
<div class="line"><span class="lineno"> 179</span>}</div>
<div class="ttc" id="abinary__search__tree_8c_html_adff4c6248834a9944a1fb03a20230c9c"><div class="ttname"><a href="../../da/d02/binary__search__tree_8c.html#adff4c6248834a9944a1fb03a20230c9c">find</a></div><div class="ttdeci">int find(node *root, int data)</div><div class="ttdoc">Search procedure, which looks for the input key in the tree and returns 1 if it's present or 0 if it'...</div><div class="ttdef"><b>Definition</b> binary_search_tree.c:152</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../da/d02/binary__search__tree_8c_adff4c6248834a9944a1fb03a20230c9c_cgraph.svg" width="54" height="60"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div>
</div>
<a id="ad50f3debc5323bb7b71933effb64f30f" name="ad50f3debc5323bb7b71933effb64f30f"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ad50f3debc5323bb7b71933effb64f30f">&#9670;&#160;</a></span>getMax()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname"><a class="el" href="../../d5/da1/structnode.html">node</a> * getMax </td>
<td>(</td>
<td class="paramtype"><a class="el" href="../../d5/da1/structnode.html">node</a> *&#160;</td>
<td class="paramname"><em>root</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Utilitary procedure to find the greatest key in the left subtree. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">root</td><td>pointer to parent node </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>pointer to parent node </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 73</span>{</div>
<div class="line"><span class="lineno"> 74</span> <span class="comment">// If there&#39;s no leaf to the right, then this is the maximum key value</span></div>
<div class="line"><span class="lineno"> 75</span> <span class="keywordflow">if</span> (root-&gt;right != NULL)</div>
<div class="line"><span class="lineno"> 76</span> {</div>
<div class="line"><span class="lineno"> 77</span> <span class="keywordflow">return</span> <a class="code hl_function" href="../../da/d02/binary__search__tree_8c.html#ad50f3debc5323bb7b71933effb64f30f">getMax</a>(root-&gt;right);</div>
<div class="line"><span class="lineno"> 78</span> }</div>
<div class="line"><span class="lineno"> 79</span> <span class="keywordflow">return</span> root;</div>
<div class="line"><span class="lineno"> 80</span>}</div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../da/d02/binary__search__tree_8c_ad50f3debc5323bb7b71933effb64f30f_cgraph.svg" width="75" height="60"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div>
</div>
<a id="ae4a66d8b0c2b0d626aea45977e358c83" name="ae4a66d8b0c2b0d626aea45977e358c83"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ae4a66d8b0c2b0d626aea45977e358c83">&#9670;&#160;</a></span>height()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int height </td>
<td>(</td>
<td class="paramtype"><a class="el" href="../../d5/da1/structnode.html">node</a> *&#160;</td>
<td class="paramname"><em>root</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Utilitary procedure to measure the height of the binary tree. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">root</td><td>pointer to parent node </td></tr>
<tr><td class="paramname">data</td><td>value to store int he new node </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>0 if value not found in the nodes </dd>
<dd>
height of nodes to get to data from parent node </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 188</span>{</div>
<div class="line"><span class="lineno"> 189</span> <span class="comment">// If the root is null, this is the bottom of the tree (height 0)</span></div>
<div class="line"><span class="lineno"> 190</span> <span class="keywordflow">if</span> (root == NULL)</div>
<div class="line"><span class="lineno"> 191</span> {</div>
<div class="line"><span class="lineno"> 192</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 193</span> }</div>
<div class="line"><span class="lineno"> 194</span> <span class="keywordflow">else</span></div>
<div class="line"><span class="lineno"> 195</span> {</div>
<div class="line"><span class="lineno"> 196</span> <span class="comment">// Get the height from both left and right subtrees to check which is</span></div>
<div class="line"><span class="lineno"> 197</span> <span class="comment">// the greatest</span></div>
<div class="line"><span class="lineno"> 198</span> <span class="keywordtype">int</span> right_h = <a class="code hl_function" href="../../da/d02/binary__search__tree_8c.html#ae4a66d8b0c2b0d626aea45977e358c83">height</a>(root-&gt;right);</div>
<div class="line"><span class="lineno"> 199</span> <span class="keywordtype">int</span> left_h = <a class="code hl_function" href="../../da/d02/binary__search__tree_8c.html#ae4a66d8b0c2b0d626aea45977e358c83">height</a>(root-&gt;left);</div>
<div class="line"><span class="lineno"> 200</span> </div>
<div class="line"><span class="lineno"> 201</span> <span class="comment">// The final height is the height of the greatest subtree(left or right)</span></div>
<div class="line"><span class="lineno"> 202</span> <span class="comment">// plus 1(which is the root&#39;s level)</span></div>
<div class="line"><span class="lineno"> 203</span> <span class="keywordflow">if</span> (right_h &gt; left_h)</div>
<div class="line"><span class="lineno"> 204</span> {</div>
<div class="line"><span class="lineno"> 205</span> <span class="keywordflow">return</span> (right_h + 1);</div>
<div class="line"><span class="lineno"> 206</span> }</div>
<div class="line"><span class="lineno"> 207</span> <span class="keywordflow">else</span></div>
<div class="line"><span class="lineno"> 208</span> {</div>
<div class="line"><span class="lineno"> 209</span> <span class="keywordflow">return</span> (left_h + 1);</div>
<div class="line"><span class="lineno"> 210</span> }</div>
<div class="line"><span class="lineno"> 211</span> }</div>
<div class="line"><span class="lineno"> 212</span>}</div>
<div class="ttc" id="abinary__search__tree_8c_html_ae4a66d8b0c2b0d626aea45977e358c83"><div class="ttname"><a href="../../da/d02/binary__search__tree_8c.html#ae4a66d8b0c2b0d626aea45977e358c83">height</a></div><div class="ttdeci">int height(node *root)</div><div class="ttdoc">Utilitary procedure to measure the height of the binary tree.</div><div class="ttdef"><b>Definition</b> binary_search_tree.c:187</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../da/d02/binary__search__tree_8c_ae4a66d8b0c2b0d626aea45977e358c83_cgraph.svg" width="67" height="60"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div>
</div>
<a id="a0f18adaaca5ecc410cfa16dd2a3684dc" name="a0f18adaaca5ecc410cfa16dd2a3684dc"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a0f18adaaca5ecc410cfa16dd2a3684dc">&#9670;&#160;</a></span>inOrder()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void inOrder </td>
<td>(</td>
<td class="paramtype"><a class="el" href="../../d5/da1/structnode.html">node</a> *&#160;</td>
<td class="paramname"><em>root</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Traversal procedure to list the current keys in the tree in order of value (from the left to the right) </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">root</td><td>pointer to parent node </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><span class="lineno"> 239</span>{</div>
<div class="line"><span class="lineno"> 240</span> <span class="keywordflow">if</span> (root != NULL)</div>
<div class="line"><span class="lineno"> 241</span> {</div>
<div class="line"><span class="lineno"> 242</span> <a class="code hl_function" href="../../da/d02/binary__search__tree_8c.html#a0f18adaaca5ecc410cfa16dd2a3684dc">inOrder</a>(root-&gt;left);</div>
<div class="line"><span class="lineno"> 243</span> printf(<span class="stringliteral">&quot;\t[ %d ]\t&quot;</span>, root-&gt;data);</div>
<div class="line"><span class="lineno"> 244</span> <a class="code hl_function" href="../../da/d02/binary__search__tree_8c.html#a0f18adaaca5ecc410cfa16dd2a3684dc">inOrder</a>(root-&gt;right);</div>
<div class="line"><span class="lineno"> 245</span> }</div>
<div class="line"><span class="lineno"> 246</span>}</div>
<div class="ttc" id="abinary__search__tree_8c_html_a0f18adaaca5ecc410cfa16dd2a3684dc"><div class="ttname"><a href="../../da/d02/binary__search__tree_8c.html#a0f18adaaca5ecc410cfa16dd2a3684dc">inOrder</a></div><div class="ttdeci">void inOrder(node *root)</div><div class="ttdoc">Traversal procedure to list the current keys in the tree in order of value (from the left to the righ...</div><div class="ttdef"><b>Definition</b> binary_search_tree.c:238</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../da/d02/binary__search__tree_8c_a0f18adaaca5ecc410cfa16dd2a3684dc_cgraph.svg" width="75" height="60"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div>
</div>
<a id="a031cc98830742bca57c0da81a0fb779f" name="a031cc98830742bca57c0da81a0fb779f"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a031cc98830742bca57c0da81a0fb779f">&#9670;&#160;</a></span>insert()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname"><a class="el" href="../../d5/da1/structnode.html">node</a> * insert </td>
<td>(</td>
<td class="paramtype"><a class="el" href="../../d5/da1/structnode.html">node</a> *&#160;</td>
<td class="paramname"><em>root</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>data</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Insertion procedure, which inserts the input key in a new node in the tree. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">root</td><td>pointer to parent node </td></tr>
<tr><td class="paramname">data</td><td>value to store int he new node </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>pointer to parent node </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 47</span>{</div>
<div class="line"><span class="lineno"> 48</span> <span class="comment">// If the root of the subtree is null, insert key here</span></div>
<div class="line"><span class="lineno"> 49</span> <span class="keywordflow">if</span> (root == NULL)</div>
<div class="line"><span class="lineno"> 50</span> {</div>
<div class="line"><span class="lineno"> 51</span> root = <a class="code hl_function" href="../../da/d02/binary__search__tree_8c.html#adf2169c1008551b20c4a59fcf0c9ab3b">newNode</a>(<a class="code hl_struct" href="../../df/dea/structdata.html">data</a>);</div>
<div class="line"><span class="lineno"> 52</span> }</div>
<div class="line"><span class="lineno"> 53</span> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (<a class="code hl_struct" href="../../df/dea/structdata.html">data</a> &gt; root-&gt;data)</div>
<div class="line"><span class="lineno"> 54</span> {</div>
<div class="line"><span class="lineno"> 55</span> <span class="comment">// If it isn&#39;t null and the input key is greater than the root key,</span></div>
<div class="line"><span class="lineno"> 56</span> <span class="comment">// insert in the right leaf</span></div>
<div class="line"><span class="lineno"> 57</span> root-&gt;right = <a class="code hl_function" href="../../da/d02/binary__search__tree_8c.html#a031cc98830742bca57c0da81a0fb779f">insert</a>(root-&gt;right, <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>);</div>
<div class="line"><span class="lineno"> 58</span> }</div>
<div class="line"><span class="lineno"> 59</span> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (data &lt; root-&gt;<a class="code hl_struct" href="../../df/dea/structdata.html">data</a>)</div>
<div class="line"><span class="lineno"> 60</span> { <span class="comment">// If it isn&#39;t null and the input key is lower than the root key, insert</span></div>
<div class="line"><span class="lineno"> 61</span> <span class="comment">// in the left leaf</span></div>
<div class="line"><span class="lineno"> 62</span> root-&gt;left = <a class="code hl_function" href="../../da/d02/binary__search__tree_8c.html#a031cc98830742bca57c0da81a0fb779f">insert</a>(root-&gt;left, <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>);</div>
<div class="line"><span class="lineno"> 63</span> }</div>
<div class="line"><span class="lineno"> 64</span> <span class="comment">// Returns the modified tree</span></div>
<div class="line"><span class="lineno"> 65</span> <span class="keywordflow">return</span> root;</div>
<div class="line"><span class="lineno"> 66</span>}</div>
<div class="ttc" id="abinary__search__tree_8c_html_a031cc98830742bca57c0da81a0fb779f"><div class="ttname"><a href="../../da/d02/binary__search__tree_8c.html#a031cc98830742bca57c0da81a0fb779f">insert</a></div><div class="ttdeci">node * insert(node *root, int data)</div><div class="ttdoc">Insertion procedure, which inserts the input key in a new node in the tree.</div><div class="ttdef"><b>Definition</b> binary_search_tree.c:46</div></div>
<div class="ttc" id="abinary__search__tree_8c_html_adf2169c1008551b20c4a59fcf0c9ab3b"><div class="ttname"><a href="../../da/d02/binary__search__tree_8c.html#adf2169c1008551b20c4a59fcf0c9ab3b">newNode</a></div><div class="ttdeci">node * newNode(int data)</div><div class="ttdoc">The node constructor, which receives the key value input and returns a node pointer.</div><div class="ttdef"><b>Definition</b> binary_search_tree.c:28</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../da/d02/binary__search__tree_8c_a031cc98830742bca57c0da81a0fb779f_cgraph.svg" width="188" height="60"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div>
</div>
<a id="ae66f6b31b5ad750f1fe042a706a4e3d4" name="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ae66f6b31b5ad750f1fe042a706a4e3d4">&#9670;&#160;</a></span>main()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int main </td>
<td>(</td>
<td class="paramtype">void&#160;</td>
<td class="paramname"></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Main funcion. </p>
<div class="fragment"><div class="line"><span class="lineno"> 250</span>{</div>
<div class="line"><span class="lineno"> 251</span> <span class="comment">// this reference don&#39;t change.</span></div>
<div class="line"><span class="lineno"> 252</span> <span class="comment">// only the tree changes.</span></div>
<div class="line"><span class="lineno"> 253</span> <a class="code hl_struct" href="../../d5/da1/structnode.html">node</a> *root = NULL;</div>
<div class="line"><span class="lineno"> 254</span> <span class="keywordtype">int</span> opt = -1;</div>
<div class="line"><span class="lineno"> 255</span> <span class="keywordtype">int</span> <a class="code hl_struct" href="../../df/dea/structdata.html">data</a> = 0;</div>
<div class="line"><span class="lineno"> 256</span> </div>
<div class="line"><span class="lineno"> 257</span> <span class="comment">// event-loop.</span></div>
<div class="line"><span class="lineno"> 258</span> <span class="keywordflow">while</span> (opt != 0)</div>
<div class="line"><span class="lineno"> 259</span> {</div>
<div class="line"><span class="lineno"> 260</span> printf(</div>
<div class="line"><span class="lineno"> 261</span> <span class="stringliteral">&quot;\n\n[1] Insert Node\n[2] Delete Node\n[3] Find a Node\n[4] Get &quot;</span></div>
<div class="line"><span class="lineno"> 262</span> <span class="stringliteral">&quot;current Height\n[5] Print Tree in Crescent Order\n[0] Quit\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 263</span> scanf(<span class="stringliteral">&quot;%d&quot;</span>, &amp;opt); <span class="comment">// reads the choice of the user</span></div>
<div class="line"><span class="lineno"> 264</span> </div>
<div class="line"><span class="lineno"> 265</span> <span class="comment">// processes the choice</span></div>
<div class="line"><span class="lineno"> 266</span> <span class="keywordflow">switch</span> (opt)</div>
<div class="line"><span class="lineno"> 267</span> {</div>
<div class="line"><span class="lineno"> 268</span> <span class="keywordflow">case</span> 1:</div>
<div class="line"><span class="lineno"> 269</span> printf(<span class="stringliteral">&quot;Enter the new node&#39;s value:\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 270</span> scanf(<span class="stringliteral">&quot;%d&quot;</span>, &amp;<a class="code hl_struct" href="../../df/dea/structdata.html">data</a>);</div>
<div class="line"><span class="lineno"> 271</span> root = <a class="code hl_function" href="../../da/d02/binary__search__tree_8c.html#a031cc98830742bca57c0da81a0fb779f">insert</a>(root, <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>);</div>
<div class="line"><span class="lineno"> 272</span> <span class="keywordflow">break</span>;</div>
<div class="line"><span class="lineno"> 273</span> </div>
<div class="line"><span class="lineno"> 274</span> <span class="keywordflow">case</span> 2:</div>
<div class="line"><span class="lineno"> 275</span> printf(<span class="stringliteral">&quot;Enter the value to be removed:\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 276</span> <span class="keywordflow">if</span> (root != NULL)</div>
<div class="line"><span class="lineno"> 277</span> {</div>
<div class="line"><span class="lineno"> 278</span> scanf(<span class="stringliteral">&quot;%d&quot;</span>, &amp;<a class="code hl_struct" href="../../df/dea/structdata.html">data</a>);</div>
<div class="line"><span class="lineno"> 279</span> root = <span class="keyword">delete</span> (root, <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>);</div>
<div class="line"><span class="lineno"> 280</span> }</div>
<div class="line"><span class="lineno"> 281</span> <span class="keywordflow">else</span></div>
<div class="line"><span class="lineno"> 282</span> {</div>
<div class="line"><span class="lineno"> 283</span> printf(<span class="stringliteral">&quot;Tree is already empty!\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 284</span> }</div>
<div class="line"><span class="lineno"> 285</span> <span class="keywordflow">break</span>;</div>
<div class="line"><span class="lineno"> 286</span> </div>
<div class="line"><span class="lineno"> 287</span> <span class="keywordflow">case</span> 3:</div>
<div class="line"><span class="lineno"> 288</span> printf(<span class="stringliteral">&quot;Enter the searched value:\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 289</span> scanf(<span class="stringliteral">&quot;%d&quot;</span>, &amp;<a class="code hl_struct" href="../../df/dea/structdata.html">data</a>);</div>
<div class="line"><span class="lineno"> 290</span> <a class="code hl_function" href="../../da/d02/binary__search__tree_8c.html#adff4c6248834a9944a1fb03a20230c9c">find</a>(root, <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>) ? printf(<span class="stringliteral">&quot;The value is in the tree.\n&quot;</span>)</div>
<div class="line"><span class="lineno"> 291</span> : printf(<span class="stringliteral">&quot;The value is not in the tree.\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 292</span> <span class="keywordflow">break</span>;</div>
<div class="line"><span class="lineno"> 293</span> </div>
<div class="line"><span class="lineno"> 294</span> <span class="keywordflow">case</span> 4:</div>
<div class="line"><span class="lineno"> 295</span> printf(<span class="stringliteral">&quot;Current height of the tree is: %d\n&quot;</span>, <a class="code hl_function" href="../../da/d02/binary__search__tree_8c.html#ae4a66d8b0c2b0d626aea45977e358c83">height</a>(root));</div>
<div class="line"><span class="lineno"> 296</span> <span class="keywordflow">break</span>;</div>
<div class="line"><span class="lineno"> 297</span> </div>
<div class="line"><span class="lineno"> 298</span> <span class="keywordflow">case</span> 5:</div>
<div class="line"><span class="lineno"> 299</span> <a class="code hl_function" href="../../da/d02/binary__search__tree_8c.html#a0f18adaaca5ecc410cfa16dd2a3684dc">inOrder</a>(root);</div>
<div class="line"><span class="lineno"> 300</span> <span class="keywordflow">break</span>;</div>
<div class="line"><span class="lineno"> 301</span> }</div>
<div class="line"><span class="lineno"> 302</span> }</div>
<div class="line"><span class="lineno"> 303</span> </div>
<div class="line"><span class="lineno"> 304</span> <span class="comment">// deletes the tree from the heap.</span></div>
<div class="line"><span class="lineno"> 305</span> <a class="code hl_function" href="../../da/d02/binary__search__tree_8c.html#a01151353aa2d9688934ed39208133241">purge</a>(root);</div>
<div class="line"><span class="lineno"> 306</span> </div>
<div class="line"><span class="lineno"> 307</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 308</span>}</div>
<div class="ttc" id="abinary__search__tree_8c_html_a01151353aa2d9688934ed39208133241"><div class="ttname"><a href="../../da/d02/binary__search__tree_8c.html#a01151353aa2d9688934ed39208133241">purge</a></div><div class="ttdeci">void purge(node *root)</div><div class="ttdoc">Utilitary procedure to free all nodes in a tree.</div><div class="ttdef"><b>Definition</b> binary_search_tree.c:217</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../da/d02/binary__search__tree_8c_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg" width="296" height="354"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div>
</div>
<a id="adf2169c1008551b20c4a59fcf0c9ab3b" name="adf2169c1008551b20c4a59fcf0c9ab3b"></a>
<h2 class="memtitle"><span class="permalink"><a href="#adf2169c1008551b20c4a59fcf0c9ab3b">&#9670;&#160;</a></span>newNode()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname"><a class="el" href="../../d5/da1/structnode.html">node</a> * newNode </td>
<td>(</td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>data</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>The node constructor, which receives the key value input and returns a node pointer. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">data</td><td>data to store in a new node </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>new node with the provided data </dd></dl>
<dl class="section note"><dt>Note</dt><dd>the node must be deleted before program terminates to avoid memory leaks </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 29</span>{</div>
<div class="line"><span class="lineno"> 30</span> <span class="comment">// creates a slug</span></div>
<div class="line"><span class="lineno"> 31</span> <a class="code hl_struct" href="../../d5/da1/structnode.html">node</a> *tmp = (<a class="code hl_struct" href="../../d5/da1/structnode.html">node</a> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(<span class="keyword">sizeof</span>(<a class="code hl_struct" href="../../d5/da1/structnode.html">node</a>));</div>
<div class="line"><span class="lineno"> 32</span> </div>
<div class="line"><span class="lineno"> 33</span> <span class="comment">// initializes the slug</span></div>
<div class="line"><span class="lineno"> 34</span> tmp-&gt;<a class="code hl_variable" href="../../d5/da1/structnode.html#a2d890bb9f6af0ffd73fe79b21124c2a2">data</a> = <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>;</div>
<div class="line"><span class="lineno"> 35</span> tmp-&gt;<a class="code hl_variable" href="../../d5/da1/structnode.html#af7109e6ffd82cbbb705e486fd0ce92f0">left</a> = NULL;</div>
<div class="line"><span class="lineno"> 36</span> tmp-&gt;<a class="code hl_variable" href="../../d5/da1/structnode.html#a51e160f22dc6064bac4a4f9f1d931c2c">right</a> = NULL;</div>
<div class="line"><span class="lineno"> 37</span> </div>
<div class="line"><span class="lineno"> 38</span> <span class="keywordflow">return</span> tmp;</div>
<div class="line"><span class="lineno"> 39</span>}</div>
<div class="ttc" id="amalloc__dbg_8h_html_a725f50ecaf1959d96de79b36b4788fee"><div class="ttname"><a href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a></div><div class="ttdeci">#define malloc(bytes)</div><div class="ttdoc">This macro replace the standard malloc function with malloc_dbg.</div><div class="ttdef"><b>Definition</b> malloc_dbg.h:18</div></div>
<div class="ttc" id="astructnode_html_a51e160f22dc6064bac4a4f9f1d931c2c"><div class="ttname"><a href="../../d5/da1/structnode.html#a51e160f22dc6064bac4a4f9f1d931c2c">node::right</a></div><div class="ttdeci">struct node * right</div><div class="ttdoc">right child</div><div class="ttdef"><b>Definition</b> binary_search_tree.c:17</div></div>
<div class="ttc" id="astructnode_html_af7109e6ffd82cbbb705e486fd0ce92f0"><div class="ttname"><a href="../../d5/da1/structnode.html#af7109e6ffd82cbbb705e486fd0ce92f0">node::left</a></div><div class="ttdeci">struct node * left</div><div class="ttdoc">left child</div><div class="ttdef"><b>Definition</b> binary_search_tree.c:16</div></div>
</div><!-- fragment -->
</div>
</div>
<a id="a01151353aa2d9688934ed39208133241" name="a01151353aa2d9688934ed39208133241"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a01151353aa2d9688934ed39208133241">&#9670;&#160;</a></span>purge()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void purge </td>
<td>(</td>
<td class="paramtype"><a class="el" href="../../d5/da1/structnode.html">node</a> *&#160;</td>
<td class="paramname"><em>root</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Utilitary procedure to free all nodes in a tree. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">root</td><td>pointer to parent node </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><span class="lineno"> 218</span>{</div>
<div class="line"><span class="lineno"> 219</span> <span class="keywordflow">if</span> (root != NULL)</div>
<div class="line"><span class="lineno"> 220</span> {</div>
<div class="line"><span class="lineno"> 221</span> <span class="keywordflow">if</span> (root-&gt;left != NULL)</div>
<div class="line"><span class="lineno"> 222</span> {</div>
<div class="line"><span class="lineno"> 223</span> <a class="code hl_function" href="../../da/d02/binary__search__tree_8c.html#a01151353aa2d9688934ed39208133241">purge</a>(root-&gt;left);</div>
<div class="line"><span class="lineno"> 224</span> }</div>
<div class="line"><span class="lineno"> 225</span> <span class="keywordflow">if</span> (root-&gt;right != NULL)</div>
<div class="line"><span class="lineno"> 226</span> {</div>
<div class="line"><span class="lineno"> 227</span> <a class="code hl_function" href="../../da/d02/binary__search__tree_8c.html#a01151353aa2d9688934ed39208133241">purge</a>(root-&gt;right);</div>
<div class="line"><span class="lineno"> 228</span> }</div>
<div class="line"><span class="lineno"> 229</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(root);</div>
<div class="line"><span class="lineno"> 230</span> root = NULL; <span class="comment">// reset pointer</span></div>
<div class="line"><span class="lineno"> 231</span> }</div>
<div class="line"><span class="lineno"> 232</span>}</div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../da/d02/binary__search__tree_8c_a01151353aa2d9688934ed39208133241_cgraph.svg" width="64" height="60"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="../../dir_2e746e9d06bf2d8ff842208bcc6ebcfc.html">data_structures</a></li><li class="navelem"><a class="el" href="../../dir_e39df94f82619fa5256f1786519937be.html">binary_trees</a></li><li class="navelem"><a class="el" href="../../da/d02/binary__search__tree_8c.html">binary_search_tree.c</a></li>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.8 </li>
</ul>
</div>
</body>
</html>