{"id":28316,"date":"2017-10-15T18:22:55","date_gmt":"2017-10-15T12:52:55","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=28316"},"modified":"2017-10-15T18:22:55","modified_gmt":"2017-10-15T12:52:55","slug":"c-programming-floor-ceil-bst","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-floor-ceil-bst\/","title":{"rendered":"C Programming &#8211; Floor and Ceil from a BST"},"content":{"rendered":"<p>There are numerous applications we need to find floor (ceil) value of a key in a binary search tree or sorted array. <span id=\"more-12996\"><\/span>For example, consider designing memory\u00a0management system in which free nodes are arranged in BST. Find best fit for the input request.<\/p>\n<p><em>Ceil Value Node<\/em>: Node with smallest data larger than or equal to key value.<\/p>\n<p>Imagine we are moving down the tree, and assume we are root node. The comparison yields three possibilities,<\/p>\n<p><strong>A)<\/strong> Root data is equal to key. We are done, root data is ceil value.<\/p>\n<p><strong>B)<\/strong> Root data &lt; key value, certainly the ceil value can\u2019t be in left subtree. Proceed to search on right subtree as reduced problem instance.<\/p>\n<p><strong>C)<\/strong> Root data &gt; key value, the ceil value <em>may be<\/em> in left subtree. We may find a node with is larger data than key value in left subtree, if not the root itself will be ceil node.<\/p>\n<p>Here is the code for ceil value.[ad type=&#8221;banner&#8221;]\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C Program<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ Program to find ceil of a given value in BST<br\/>#include &lt;stdio.h&gt;<br\/>#include &lt;stdlib.h&gt;<br\/> <br\/>\/* A binary tree node has key, left child and right child *\/<br\/>struct node<br\/>{<br\/>    int key;<br\/>    struct node* left;<br\/>    struct node* right;<br\/>};<br\/> <br\/>\/* Helper function that allocates a new node with the given key and<br\/>   NULL left and right  pointers.*\/<br\/>struct node* newNode(int key)<br\/>{<br\/>    struct node* node = (struct node*)malloc(sizeof(struct node));<br\/>    node-&gt;key = key;<br\/>    node-&gt;left = NULL;<br\/>    node-&gt;right = NULL;<br\/>    return(node);<br\/>}<br\/> <br\/>\/\/ Function to find ceil of a given input in BST. If input is more<br\/>\/\/ than the max key in BST, return -1<br\/>int Ceil(node *root, int input)<br\/>{<br\/>    \/\/ Base case<br\/>    if( root == NULL )<br\/>        return -1;<br\/> <br\/>    \/\/ We found equal key<br\/>    if( root-&gt;key == input )<br\/>        return root-&gt;key;<br\/> <br\/>    \/\/ If root&#039;s key is smaller, ceil must be in right subtree<br\/>    if( root-&gt;key &lt; input )<br\/>        return Ceil(root-&gt;right, input);<br\/> <br\/>    \/\/ Else, either left subtree or root has the ceil value<br\/>    int ceil = Ceil(root-&gt;left, input);<br\/>    return (ceil &gt;= input) ? ceil : root-&gt;key;<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>    node *root = newNode(8);<br\/> <br\/>    root-&gt;left = newNode(4);<br\/>    root-&gt;right = newNode(12);<br\/> <br\/>    root-&gt;left-&gt;left = newNode(2);<br\/>    root-&gt;left-&gt;right = newNode(6);<br\/> <br\/>    root-&gt;right-&gt;left = newNode(10);<br\/>    root-&gt;right-&gt;right = newNode(14);<br\/> <br\/>    for(int i = 0; i &lt; 16; i++)<br\/>        printf(&quot;%d  %d\\n&quot;, i, Ceil(root, i));<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>0  2\r\n1  2\r\n2  2\r\n3  4\r\n4  4\r\n5  6\r\n6  6\r\n7  8\r\n8  8\r\n9  10\r\n10  10\r\n11  12\r\n12  12\r\n13  14\r\n14  14\r\n15  -1<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Floor and Ceil from a BST &#8211; Binary Search Tree -There are numerous applications we need to find floor (ceil) value of a key in a binary search tree <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80126],"tags":[82762,82768,82766,82764,82763,82765,82767],"class_list":["post-28316","post","type-post","status-publish","format-standard","hentry","category-binary-search-tree","tag-ceil-and-floor-in-c-language","tag-ceil-function-in-c","tag-ceil-in-c","tag-floor-function-in-c","tag-roundup-function-in-c","tag-undefined-reference-to-ceil","tag-undefined-reference-to-floor"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/28316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=28316"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/28316\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=28316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=28316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=28316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}