mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
623 lines
53 KiB
HTML
623 lines
53 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">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
|
|
<meta name="generator" content="Doxygen 1.9.1"/>
|
|
<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 style="height: 56px;">
|
|
<td id="projectalign" style="padding-left: 0.5em;">
|
|
<div id="projectname">Algorithms_in_C
|
|
 <span id="projectnumber">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.1 -->
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
|
var searchBox = new SearchBox("searchBox", "../../search",false,'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:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
|
$(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:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
|
$(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">
|
|
<iframe src="javascript:void(0)" frameborder="0"
|
|
name="MSearchResults" id="MSearchResults">
|
|
</iframe>
|
|
</div>
|
|
|
|
<div class="header">
|
|
<div class="summary">
|
|
<a href="#nested-classes">Data Structures</a> |
|
|
<a href="#typedef-members">Typedefs</a> |
|
|
<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 <stdio.h></code><br />
|
|
<code>#include <stdlib.h></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="127"><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 name="nested-classes"></a>
|
|
Data Structures</h2></td></tr>
|
|
<tr class="memitem:"><td class="memItemLeft" align="right" valign="top">struct  </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/da1/structnode.html">node</a></td></tr>
|
|
<tr class="memdesc:"><td class="mdescLeft"> </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"> </td></tr>
|
|
</table><table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="typedef-members"></a>
|
|
Typedefs</h2></td></tr>
|
|
<tr class="memitem:af4aeda155dbe167f1c1cf38cb65bf324"><td class="memItemLeft" align="right" valign="top"><a id="af4aeda155dbe167f1c1cf38cb65bf324"></a>
|
|
typedef struct <a class="el" href="../../d5/da1/structnode.html">node</a> </td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d02/binary__search__tree_8c.html#af4aeda155dbe167f1c1cf38cb65bf324">node</a></td></tr>
|
|
<tr class="memdesc:af4aeda155dbe167f1c1cf38cb65bf324"><td class="mdescLeft"> </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"> </td></tr>
|
|
</table><table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="func-members"></a>
|
|
Functions</h2></td></tr>
|
|
<tr class="memitem:ac73c73be92dbbeeaad942c0103b9540d"><td class="memItemLeft" align="right" valign="top"><a class="el" href="../../d5/da1/structnode.html">node</a> * </td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d02/binary__search__tree_8c.html#ac73c73be92dbbeeaad942c0103b9540d">newNode</a> (int <a class="el" href="../../df/dea/structdata.html">data</a>)</td></tr>
|
|
<tr class="memdesc:ac73c73be92dbbeeaad942c0103b9540d"><td class="mdescLeft"> </td><td class="mdescRight">The node constructor, which receives the key value input and returns a node pointer. <a href="../../da/d02/binary__search__tree_8c.html#ac73c73be92dbbeeaad942c0103b9540d">More...</a><br /></td></tr>
|
|
<tr class="separator:ac73c73be92dbbeeaad942c0103b9540d"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a73152b9ccb4aa5cd4c1bacd4188bb2de"><td class="memItemLeft" align="right" valign="top"><a class="el" href="../../d5/da1/structnode.html">node</a> * </td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d02/binary__search__tree_8c.html#a73152b9ccb4aa5cd4c1bacd4188bb2de">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:a73152b9ccb4aa5cd4c1bacd4188bb2de"><td class="mdescLeft"> </td><td class="mdescRight">Insertion procedure, which inserts the input key in a new node in the tree. <a href="../../da/d02/binary__search__tree_8c.html#a73152b9ccb4aa5cd4c1bacd4188bb2de">More...</a><br /></td></tr>
|
|
<tr class="separator:a73152b9ccb4aa5cd4c1bacd4188bb2de"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:ad297e528a7bb8604ca93af149d609150"><td class="memItemLeft" align="right" valign="top"><a class="el" href="../../d5/da1/structnode.html">node</a> * </td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d02/binary__search__tree_8c.html#ad297e528a7bb8604ca93af149d609150">getMax</a> (<a class="el" href="../../d5/da1/structnode.html">node</a> *root)</td></tr>
|
|
<tr class="memdesc:ad297e528a7bb8604ca93af149d609150"><td class="mdescLeft"> </td><td class="mdescRight">Utilitary procedure to find the greatest key in the left subtree. <a href="../../da/d02/binary__search__tree_8c.html#ad297e528a7bb8604ca93af149d609150">More...</a><br /></td></tr>
|
|
<tr class="separator:ad297e528a7bb8604ca93af149d609150"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a748f3966920e2fd197906be1e151b127"><td class="memItemLeft" align="right" valign="top"><a class="el" href="../../d5/da1/structnode.html">node</a> * </td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d02/binary__search__tree_8c.html#a748f3966920e2fd197906be1e151b127">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:a748f3966920e2fd197906be1e151b127"><td class="mdescLeft"> </td><td class="mdescRight">Deletion procedure, which searches for the input key in the tree and removes it if present. <a href="../../da/d02/binary__search__tree_8c.html#a748f3966920e2fd197906be1e151b127">More...</a><br /></td></tr>
|
|
<tr class="separator:a748f3966920e2fd197906be1e151b127"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:adff4c6248834a9944a1fb03a20230c9c"><td class="memItemLeft" align="right" valign="top">int </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"> </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. <a href="../../da/d02/binary__search__tree_8c.html#adff4c6248834a9944a1fb03a20230c9c">More...</a><br /></td></tr>
|
|
<tr class="separator:adff4c6248834a9944a1fb03a20230c9c"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:ae4a66d8b0c2b0d626aea45977e358c83"><td class="memItemLeft" align="right" valign="top">int </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"> </td><td class="mdescRight">Utilitary procedure to measure the height of the binary tree. <a href="../../da/d02/binary__search__tree_8c.html#ae4a66d8b0c2b0d626aea45977e358c83">More...</a><br /></td></tr>
|
|
<tr class="separator:ae4a66d8b0c2b0d626aea45977e358c83"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a01151353aa2d9688934ed39208133241"><td class="memItemLeft" align="right" valign="top">void </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"> </td><td class="mdescRight">Utilitary procedure to free all nodes in a tree. <a href="../../da/d02/binary__search__tree_8c.html#a01151353aa2d9688934ed39208133241">More...</a><br /></td></tr>
|
|
<tr class="separator:a01151353aa2d9688934ed39208133241"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a0f18adaaca5ecc410cfa16dd2a3684dc"><td class="memItemLeft" align="right" valign="top">void </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"> </td><td class="mdescRight">Traversal procedure to list the current keys in the tree in order of value (from the left to the right) <a href="../../da/d02/binary__search__tree_8c.html#a0f18adaaca5ecc410cfa16dd2a3684dc">More...</a><br /></td></tr>
|
|
<tr class="separator:a0f18adaaca5ecc410cfa16dd2a3684dc"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top"><a id="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
|
|
int </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"> </td><td class="mdescRight">Main funcion. <br /></td></tr>
|
|
<tr class="separator:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memSeparator" colspan="2"> </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="a748f3966920e2fd197906be1e151b127"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a748f3966920e2fd197906be1e151b127">◆ </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> * </td>
|
|
<td class="paramname"><em>root</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>data</em> </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"><a name="l00089"></a><span class="lineno"> 89</span> {</div>
|
|
<div class="line"><a name="l00090"></a><span class="lineno"> 90</span>  <span class="comment">// If the root is null, nothing to be done</span></div>
|
|
<div class="line"><a name="l00091"></a><span class="lineno"> 91</span>  <span class="keywordflow">if</span> (root == NULL)</div>
|
|
<div class="line"><a name="l00092"></a><span class="lineno"> 92</span>  {</div>
|
|
<div class="line"><a name="l00093"></a><span class="lineno"> 93</span>  <span class="keywordflow">return</span> root;</div>
|
|
<div class="line"><a name="l00094"></a><span class="lineno"> 94</span>  }</div>
|
|
<div class="line"><a name="l00095"></a><span class="lineno"> 95</span>  <span class="keywordflow">else</span> <span class="keywordflow">if</span> (<a class="code" href="../../df/dea/structdata.html">data</a> > root->data)</div>
|
|
<div class="line"><a name="l00096"></a><span class="lineno"> 96</span>  { <span class="comment">// If the input key is greater than the root's, search in the right</span></div>
|
|
<div class="line"><a name="l00097"></a><span class="lineno"> 97</span>  <span class="comment">// subtree</span></div>
|
|
<div class="line"><a name="l00098"></a><span class="lineno"> 98</span>  root->right = <span class="keyword">delete</span> (root->right, <a class="code" href="../../df/dea/structdata.html">data</a>);</div>
|
|
<div class="line"><a name="l00099"></a><span class="lineno"> 99</span>  }</div>
|
|
<div class="line"><a name="l00100"></a><span class="lineno"> 100</span>  <span class="keywordflow">else</span> <span class="keywordflow">if</span> (<a class="code" href="../../df/dea/structdata.html">data < root-></a><a class="code" href="../../df/dea/structdata.html">data</a>)</div>
|
|
<div class="line"><a name="l00101"></a><span class="lineno"> 101</span>  { <span class="comment">// If the input key is lower than the root's, search in the left subtree</span></div>
|
|
<div class="line"><a name="l00102"></a><span class="lineno"> 102</span>  root->left = <span class="keyword">delete</span> (root->left, <a class="code" href="../../df/dea/structdata.html">data</a>);</div>
|
|
<div class="line"><a name="l00103"></a><span class="lineno"> 103</span>  }</div>
|
|
<div class="line"><a name="l00104"></a><span class="lineno"> 104</span>  <span class="keywordflow">else</span> <span class="keywordflow">if</span> (<a class="code" href="../../df/dea/structdata.html">data</a> == root->data)</div>
|
|
<div class="line"><a name="l00105"></a><span class="lineno"> 105</span>  {</div>
|
|
<div class="line"><a name="l00106"></a><span class="lineno"> 106</span>  <span class="comment">// If the input key matches the root's, check the following cases</span></div>
|
|
<div class="line"><a name="l00107"></a><span class="lineno"> 107</span>  <span class="comment">// termination condition</span></div>
|
|
<div class="line"><a name="l00108"></a><span class="lineno"> 108</span>  <span class="keywordflow">if</span> ((root->left == NULL) && (root->right == NULL))</div>
|
|
<div class="line"><a name="l00109"></a><span class="lineno"> 109</span>  { <span class="comment">// Case 1: the root has no leaves, remove the node</span></div>
|
|
<div class="line"><a name="l00110"></a><span class="lineno"> 110</span>  free(root);</div>
|
|
<div class="line"><a name="l00111"></a><span class="lineno"> 111</span>  <span class="keywordflow">return</span> NULL;</div>
|
|
<div class="line"><a name="l00112"></a><span class="lineno"> 112</span>  }</div>
|
|
<div class="line"><a name="l00113"></a><span class="lineno"> 113</span>  <span class="keywordflow">else</span> <span class="keywordflow">if</span> (root->left == NULL)</div>
|
|
<div class="line"><a name="l00114"></a><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"><a name="l00115"></a><span class="lineno"> 115</span>  <span class="comment">// remove</span></div>
|
|
<div class="line"><a name="l00116"></a><span class="lineno"> 116</span>  <span class="comment">// the old root</span></div>
|
|
<div class="line"><a name="l00117"></a><span class="lineno"> 117</span>  <a class="code" href="../../d5/da1/structnode.html">node</a> *tmp = root;</div>
|
|
<div class="line"><a name="l00118"></a><span class="lineno"> 118</span>  root = root->right;</div>
|
|
<div class="line"><a name="l00119"></a><span class="lineno"> 119</span>  free(tmp);</div>
|
|
<div class="line"><a name="l00120"></a><span class="lineno"> 120</span>  <span class="keywordflow">return</span> root;</div>
|
|
<div class="line"><a name="l00121"></a><span class="lineno"> 121</span>  }</div>
|
|
<div class="line"><a name="l00122"></a><span class="lineno"> 122</span>  <span class="keywordflow">else</span> <span class="keywordflow">if</span> (root->right == NULL)</div>
|
|
<div class="line"><a name="l00123"></a><span class="lineno"> 123</span>  {</div>
|
|
<div class="line"><a name="l00124"></a><span class="lineno"> 124</span>  <a class="code" href="../../d5/da1/structnode.html">node</a> *tmp = root;</div>
|
|
<div class="line"><a name="l00125"></a><span class="lineno"> 125</span>  root = root->left;</div>
|
|
<div class="line"><a name="l00126"></a><span class="lineno"> 126</span>  free(tmp);</div>
|
|
<div class="line"><a name="l00127"></a><span class="lineno"> 127</span>  <span class="keywordflow">return</span> root;</div>
|
|
<div class="line"><a name="l00128"></a><span class="lineno"> 128</span>  }</div>
|
|
<div class="line"><a name="l00129"></a><span class="lineno"> 129</span>  <span class="keywordflow">else</span></div>
|
|
<div class="line"><a name="l00130"></a><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"><a name="l00131"></a><span class="lineno"> 131</span>  <span class="comment">// subtree and switch with the root's</span></div>
|
|
<div class="line"><a name="l00132"></a><span class="lineno"> 132</span>  </div>
|
|
<div class="line"><a name="l00133"></a><span class="lineno"> 133</span>  <span class="comment">// finds the biggest node in the left branch.</span></div>
|
|
<div class="line"><a name="l00134"></a><span class="lineno"> 134</span>  <a class="code" href="../../d5/da1/structnode.html">node</a> *tmp = <a class="code" href="../../da/d02/binary__search__tree_8c.html#ad297e528a7bb8604ca93af149d609150">getMax</a>(root->left);</div>
|
|
<div class="line"><a name="l00135"></a><span class="lineno"> 135</span>  </div>
|
|
<div class="line"><a name="l00136"></a><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"><a name="l00137"></a><span class="lineno"> 137</span>  <span class="comment">// (lefts)</span></div>
|
|
<div class="line"><a name="l00138"></a><span class="lineno"> 138</span>  root->data = tmp-><a class="code" href="../../d5/da1/structnode.html#a2d890bb9f6af0ffd73fe79b21124c2a2">data</a>;</div>
|
|
<div class="line"><a name="l00139"></a><span class="lineno"> 139</span>  root->left = <span class="keyword">delete</span> (root->left, tmp-><a class="code" href="../../d5/da1/structnode.html#a2d890bb9f6af0ffd73fe79b21124c2a2">data</a>);</div>
|
|
<div class="line"><a name="l00140"></a><span class="lineno"> 140</span>  }</div>
|
|
<div class="line"><a name="l00141"></a><span class="lineno"> 141</span>  }</div>
|
|
<div class="line"><a name="l00142"></a><span class="lineno"> 142</span>  <span class="keywordflow">return</span> root;</div>
|
|
<div class="line"><a name="l00143"></a><span class="lineno"> 143</span> }</div>
|
|
<div class="ttc" id="abinary__search__tree_8c_html_ad297e528a7bb8604ca93af149d609150"><div class="ttname"><a href="../../da/d02/binary__search__tree_8c.html#ad297e528a7bb8604ca93af149d609150">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="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_a748f3966920e2fd197906be1e151b127_cgraph.svg" width="182" height="38"><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"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#adff4c6248834a9944a1fb03a20230c9c">◆ </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> * </td>
|
|
<td class="paramname"><em>root</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>data</em> </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"><a name="l00153"></a><span class="lineno"> 153</span> {</div>
|
|
<div class="line"><a name="l00154"></a><span class="lineno"> 154</span>  <span class="comment">// If the root is null, the key's not present</span></div>
|
|
<div class="line"><a name="l00155"></a><span class="lineno"> 155</span>  <span class="keywordflow">if</span> (root == NULL)</div>
|
|
<div class="line"><a name="l00156"></a><span class="lineno"> 156</span>  {</div>
|
|
<div class="line"><a name="l00157"></a><span class="lineno"> 157</span>  <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"><a name="l00158"></a><span class="lineno"> 158</span>  }</div>
|
|
<div class="line"><a name="l00159"></a><span class="lineno"> 159</span>  <span class="keywordflow">else</span> <span class="keywordflow">if</span> (<a class="code" href="../../df/dea/structdata.html">data</a> > root->data)</div>
|
|
<div class="line"><a name="l00160"></a><span class="lineno"> 160</span>  {</div>
|
|
<div class="line"><a name="l00161"></a><span class="lineno"> 161</span>  <span class="comment">// If the input key is greater than the root's, search in the right</span></div>
|
|
<div class="line"><a name="l00162"></a><span class="lineno"> 162</span>  <span class="comment">// subtree</span></div>
|
|
<div class="line"><a name="l00163"></a><span class="lineno"> 163</span>  <span class="keywordflow">return</span> <a class="code" href="../../da/d02/binary__search__tree_8c.html#adff4c6248834a9944a1fb03a20230c9c">find</a>(root->right, <a class="code" href="../../df/dea/structdata.html">data</a>);</div>
|
|
<div class="line"><a name="l00164"></a><span class="lineno"> 164</span>  }</div>
|
|
<div class="line"><a name="l00165"></a><span class="lineno"> 165</span>  <span class="keywordflow">else</span> <span class="keywordflow">if</span> (<a class="code" href="../../df/dea/structdata.html">data < root-></a><a class="code" href="../../df/dea/structdata.html">data</a>)</div>
|
|
<div class="line"><a name="l00166"></a><span class="lineno"> 166</span>  {</div>
|
|
<div class="line"><a name="l00167"></a><span class="lineno"> 167</span>  <span class="comment">// If the input key is lower than the root's, search in the left subtree</span></div>
|
|
<div class="line"><a name="l00168"></a><span class="lineno"> 168</span>  <span class="keywordflow">return</span> <a class="code" href="../../da/d02/binary__search__tree_8c.html#adff4c6248834a9944a1fb03a20230c9c">find</a>(root->left, <a class="code" href="../../df/dea/structdata.html">data</a>);</div>
|
|
<div class="line"><a name="l00169"></a><span class="lineno"> 169</span>  }</div>
|
|
<div class="line"><a name="l00170"></a><span class="lineno"> 170</span>  <span class="keywordflow">else</span> <span class="keywordflow">if</span> (<a class="code" href="../../df/dea/structdata.html">data</a> == root->data)</div>
|
|
<div class="line"><a name="l00171"></a><span class="lineno"> 171</span>  {</div>
|
|
<div class="line"><a name="l00172"></a><span class="lineno"> 172</span>  <span class="comment">// If the input and the root key match, return 1</span></div>
|
|
<div class="line"><a name="l00173"></a><span class="lineno"> 173</span>  <span class="keywordflow">return</span> 1;</div>
|
|
<div class="line"><a name="l00174"></a><span class="lineno"> 174</span>  }</div>
|
|
<div class="line"><a name="l00175"></a><span class="lineno"> 175</span>  <span class="keywordflow">else</span></div>
|
|
<div class="line"><a name="l00176"></a><span class="lineno"> 176</span>  { <span class="comment">// unknown result!!</span></div>
|
|
<div class="line"><a name="l00177"></a><span class="lineno"> 177</span>  <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"><a name="l00178"></a><span class="lineno"> 178</span>  }</div>
|
|
<div class="line"><a name="l00179"></a><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>
|
|
</div>
|
|
<a id="ad297e528a7bb8604ca93af149d609150"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#ad297e528a7bb8604ca93af149d609150">◆ </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> * </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"><a name="l00073"></a><span class="lineno"> 73</span> {</div>
|
|
<div class="line"><a name="l00074"></a><span class="lineno"> 74</span>  <span class="comment">// If there's no leaf to the right, then this is the maximum key value</span></div>
|
|
<div class="line"><a name="l00075"></a><span class="lineno"> 75</span>  <span class="keywordflow">if</span> (root->right != NULL)</div>
|
|
<div class="line"><a name="l00076"></a><span class="lineno"> 76</span>  {</div>
|
|
<div class="line"><a name="l00077"></a><span class="lineno"> 77</span>  <span class="keywordflow">return</span> <a class="code" href="../../da/d02/binary__search__tree_8c.html#ad297e528a7bb8604ca93af149d609150">getMax</a>(root->right);</div>
|
|
<div class="line"><a name="l00078"></a><span class="lineno"> 78</span>  }</div>
|
|
<div class="line"><a name="l00079"></a><span class="lineno"> 79</span>  <span class="keywordflow">return</span> root;</div>
|
|
<div class="line"><a name="l00080"></a><span class="lineno"> 80</span> }</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="ae4a66d8b0c2b0d626aea45977e358c83"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#ae4a66d8b0c2b0d626aea45977e358c83">◆ </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> * </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"><a name="l00188"></a><span class="lineno"> 188</span> {</div>
|
|
<div class="line"><a name="l00189"></a><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"><a name="l00190"></a><span class="lineno"> 190</span>  <span class="keywordflow">if</span> (root == NULL)</div>
|
|
<div class="line"><a name="l00191"></a><span class="lineno"> 191</span>  {</div>
|
|
<div class="line"><a name="l00192"></a><span class="lineno"> 192</span>  <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"><a name="l00193"></a><span class="lineno"> 193</span>  }</div>
|
|
<div class="line"><a name="l00194"></a><span class="lineno"> 194</span>  <span class="keywordflow">else</span></div>
|
|
<div class="line"><a name="l00195"></a><span class="lineno"> 195</span>  {</div>
|
|
<div class="line"><a name="l00196"></a><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"><a name="l00197"></a><span class="lineno"> 197</span>  <span class="comment">// the greatest</span></div>
|
|
<div class="line"><a name="l00198"></a><span class="lineno"> 198</span>  <span class="keywordtype">int</span> right_h = <a class="code" href="../../da/d02/binary__search__tree_8c.html#ae4a66d8b0c2b0d626aea45977e358c83">height</a>(root->right);</div>
|
|
<div class="line"><a name="l00199"></a><span class="lineno"> 199</span>  <span class="keywordtype">int</span> left_h = <a class="code" href="../../da/d02/binary__search__tree_8c.html#ae4a66d8b0c2b0d626aea45977e358c83">height</a>(root->left);</div>
|
|
<div class="line"><a name="l00200"></a><span class="lineno"> 200</span>  </div>
|
|
<div class="line"><a name="l00201"></a><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"><a name="l00202"></a><span class="lineno"> 202</span>  <span class="comment">// plus 1(which is the root's level)</span></div>
|
|
<div class="line"><a name="l00203"></a><span class="lineno"> 203</span>  <span class="keywordflow">if</span> (right_h > left_h)</div>
|
|
<div class="line"><a name="l00204"></a><span class="lineno"> 204</span>  {</div>
|
|
<div class="line"><a name="l00205"></a><span class="lineno"> 205</span>  <span class="keywordflow">return</span> (right_h + 1);</div>
|
|
<div class="line"><a name="l00206"></a><span class="lineno"> 206</span>  }</div>
|
|
<div class="line"><a name="l00207"></a><span class="lineno"> 207</span>  <span class="keywordflow">else</span></div>
|
|
<div class="line"><a name="l00208"></a><span class="lineno"> 208</span>  {</div>
|
|
<div class="line"><a name="l00209"></a><span class="lineno"> 209</span>  <span class="keywordflow">return</span> (left_h + 1);</div>
|
|
<div class="line"><a name="l00210"></a><span class="lineno"> 210</span>  }</div>
|
|
<div class="line"><a name="l00211"></a><span class="lineno"> 211</span>  }</div>
|
|
<div class="line"><a name="l00212"></a><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>
|
|
</div>
|
|
<a id="a0f18adaaca5ecc410cfa16dd2a3684dc"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a0f18adaaca5ecc410cfa16dd2a3684dc">◆ </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> * </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"><a name="l00239"></a><span class="lineno"> 239</span> {</div>
|
|
<div class="line"><a name="l00240"></a><span class="lineno"> 240</span>  <span class="keywordflow">if</span> (root != NULL)</div>
|
|
<div class="line"><a name="l00241"></a><span class="lineno"> 241</span>  {</div>
|
|
<div class="line"><a name="l00242"></a><span class="lineno"> 242</span>  <a class="code" href="../../da/d02/binary__search__tree_8c.html#a0f18adaaca5ecc410cfa16dd2a3684dc">inOrder</a>(root->left);</div>
|
|
<div class="line"><a name="l00243"></a><span class="lineno"> 243</span>  printf(<span class="stringliteral">"\t[ %d ]\t"</span>, root->data);</div>
|
|
<div class="line"><a name="l00244"></a><span class="lineno"> 244</span>  <a class="code" href="../../da/d02/binary__search__tree_8c.html#a0f18adaaca5ecc410cfa16dd2a3684dc">inOrder</a>(root->right);</div>
|
|
<div class="line"><a name="l00245"></a><span class="lineno"> 245</span>  }</div>
|
|
<div class="line"><a name="l00246"></a><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>
|
|
</div>
|
|
<a id="a73152b9ccb4aa5cd4c1bacd4188bb2de"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a73152b9ccb4aa5cd4c1bacd4188bb2de">◆ </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> * </td>
|
|
<td class="paramname"><em>root</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>data</em> </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"><a name="l00047"></a><span class="lineno"> 47</span> {</div>
|
|
<div class="line"><a name="l00048"></a><span class="lineno"> 48</span>  <span class="comment">// If the root of the subtree is null, insert key here</span></div>
|
|
<div class="line"><a name="l00049"></a><span class="lineno"> 49</span>  <span class="keywordflow">if</span> (root == NULL)</div>
|
|
<div class="line"><a name="l00050"></a><span class="lineno"> 50</span>  {</div>
|
|
<div class="line"><a name="l00051"></a><span class="lineno"> 51</span>  root = <a class="code" href="../../da/d02/binary__search__tree_8c.html#ac73c73be92dbbeeaad942c0103b9540d">newNode</a>(<a class="code" href="../../df/dea/structdata.html">data</a>);</div>
|
|
<div class="line"><a name="l00052"></a><span class="lineno"> 52</span>  }</div>
|
|
<div class="line"><a name="l00053"></a><span class="lineno"> 53</span>  <span class="keywordflow">else</span> <span class="keywordflow">if</span> (<a class="code" href="../../df/dea/structdata.html">data</a> > root->data)</div>
|
|
<div class="line"><a name="l00054"></a><span class="lineno"> 54</span>  {</div>
|
|
<div class="line"><a name="l00055"></a><span class="lineno"> 55</span>  <span class="comment">// If it isn't null and the input key is greater than the root key,</span></div>
|
|
<div class="line"><a name="l00056"></a><span class="lineno"> 56</span>  <span class="comment">// insert in the right leaf</span></div>
|
|
<div class="line"><a name="l00057"></a><span class="lineno"> 57</span>  root->right = <a class="code" href="../../da/d02/binary__search__tree_8c.html#a73152b9ccb4aa5cd4c1bacd4188bb2de">insert</a>(root->right, <a class="code" href="../../df/dea/structdata.html">data</a>);</div>
|
|
<div class="line"><a name="l00058"></a><span class="lineno"> 58</span>  }</div>
|
|
<div class="line"><a name="l00059"></a><span class="lineno"> 59</span>  <span class="keywordflow">else</span> <span class="keywordflow">if</span> (<a class="code" href="../../df/dea/structdata.html">data < root-></a><a class="code" href="../../df/dea/structdata.html">data</a>)</div>
|
|
<div class="line"><a name="l00060"></a><span class="lineno"> 60</span>  { <span class="comment">// If it isn't null and the input key is lower than the root key, insert</span></div>
|
|
<div class="line"><a name="l00061"></a><span class="lineno"> 61</span>  <span class="comment">// in the left leaf</span></div>
|
|
<div class="line"><a name="l00062"></a><span class="lineno"> 62</span>  root->left = <a class="code" href="../../da/d02/binary__search__tree_8c.html#a73152b9ccb4aa5cd4c1bacd4188bb2de">insert</a>(root->left, <a class="code" href="../../df/dea/structdata.html">data</a>);</div>
|
|
<div class="line"><a name="l00063"></a><span class="lineno"> 63</span>  }</div>
|
|
<div class="line"><a name="l00064"></a><span class="lineno"> 64</span>  <span class="comment">// Returns the modified tree</span></div>
|
|
<div class="line"><a name="l00065"></a><span class="lineno"> 65</span>  <span class="keywordflow">return</span> root;</div>
|
|
<div class="line"><a name="l00066"></a><span class="lineno"> 66</span> }</div>
|
|
<div class="ttc" id="abinary__search__tree_8c_html_a73152b9ccb4aa5cd4c1bacd4188bb2de"><div class="ttname"><a href="../../da/d02/binary__search__tree_8c.html#a73152b9ccb4aa5cd4c1bacd4188bb2de">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_ac73c73be92dbbeeaad942c0103b9540d"><div class="ttname"><a href="../../da/d02/binary__search__tree_8c.html#ac73c73be92dbbeeaad942c0103b9540d">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_a73152b9ccb4aa5cd4c1bacd4188bb2de_cgraph.svg" width="191" height="38"><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="ac73c73be92dbbeeaad942c0103b9540d"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#ac73c73be92dbbeeaad942c0103b9540d">◆ </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 </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"><a name="l00029"></a><span class="lineno"> 29</span> {</div>
|
|
<div class="line"><a name="l00030"></a><span class="lineno"> 30</span>  <span class="comment">// creates a slug</span></div>
|
|
<div class="line"><a name="l00031"></a><span class="lineno"> 31</span>  <a class="code" href="../../d5/da1/structnode.html">node</a> *tmp = (<a class="code" href="../../d5/da1/structnode.html">node</a> *)malloc(<span class="keyword">sizeof</span>(<a class="code" href="../../d5/da1/structnode.html">node</a>));</div>
|
|
<div class="line"><a name="l00032"></a><span class="lineno"> 32</span>  </div>
|
|
<div class="line"><a name="l00033"></a><span class="lineno"> 33</span>  <span class="comment">// initializes the slug</span></div>
|
|
<div class="line"><a name="l00034"></a><span class="lineno"> 34</span>  tmp-><a class="code" href="../../d5/da1/structnode.html#a2d890bb9f6af0ffd73fe79b21124c2a2">data</a> = <a class="code" href="../../df/dea/structdata.html">data</a>;</div>
|
|
<div class="line"><a name="l00035"></a><span class="lineno"> 35</span>  tmp-><a class="code" href="../../d5/da1/structnode.html#af7109e6ffd82cbbb705e486fd0ce92f0">left</a> = NULL;</div>
|
|
<div class="line"><a name="l00036"></a><span class="lineno"> 36</span>  tmp-><a class="code" href="../../d5/da1/structnode.html#a51e160f22dc6064bac4a4f9f1d931c2c">right</a> = NULL;</div>
|
|
<div class="line"><a name="l00037"></a><span class="lineno"> 37</span>  </div>
|
|
<div class="line"><a name="l00038"></a><span class="lineno"> 38</span>  <span class="keywordflow">return</span> tmp;</div>
|
|
<div class="line"><a name="l00039"></a><span class="lineno"> 39</span> }</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"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a01151353aa2d9688934ed39208133241">◆ </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> * </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"><a name="l00218"></a><span class="lineno"> 218</span> {</div>
|
|
<div class="line"><a name="l00219"></a><span class="lineno"> 219</span>  <span class="keywordflow">if</span> (root != NULL)</div>
|
|
<div class="line"><a name="l00220"></a><span class="lineno"> 220</span>  {</div>
|
|
<div class="line"><a name="l00221"></a><span class="lineno"> 221</span>  <span class="keywordflow">if</span> (root->left != NULL)</div>
|
|
<div class="line"><a name="l00222"></a><span class="lineno"> 222</span>  {</div>
|
|
<div class="line"><a name="l00223"></a><span class="lineno"> 223</span>  <a class="code" href="../../da/d02/binary__search__tree_8c.html#a01151353aa2d9688934ed39208133241">purge</a>(root->left);</div>
|
|
<div class="line"><a name="l00224"></a><span class="lineno"> 224</span>  }</div>
|
|
<div class="line"><a name="l00225"></a><span class="lineno"> 225</span>  <span class="keywordflow">if</span> (root->right != NULL)</div>
|
|
<div class="line"><a name="l00226"></a><span class="lineno"> 226</span>  {</div>
|
|
<div class="line"><a name="l00227"></a><span class="lineno"> 227</span>  <a class="code" href="../../da/d02/binary__search__tree_8c.html#a01151353aa2d9688934ed39208133241">purge</a>(root->right);</div>
|
|
<div class="line"><a name="l00228"></a><span class="lineno"> 228</span>  }</div>
|
|
<div class="line"><a name="l00229"></a><span class="lineno"> 229</span>  free(root);</div>
|
|
<div class="line"><a name="l00230"></a><span class="lineno"> 230</span>  root = NULL; <span class="comment">// reset pointer</span></div>
|
|
<div class="line"><a name="l00231"></a><span class="lineno"> 231</span>  }</div>
|
|
<div class="line"><a name="l00232"></a><span class="lineno"> 232</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>
|
|
</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.1 </li>
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|