{"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 < 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 > 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=\u201dbanner\u201d]\n[pastacode lang=\u201dc\u201d manual=\u201d%2F%2F%20Program%20to%20find%20ceil%20of%20a%20given%20value%20in%20BST%0A%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstdlib.h%3E%0A%20%0A%2F*%20A%20binary%20tree%20node%20has%20key%2C%20left%20child%20and%20right%20child%20*%2F%0Astruct%20node%0A%7B%0A%20%20%20%20int%20key%3B%0A%20%20%20%20struct%20node*%20left%3B%0A%20%20%20%20struct%20node*%20right%3B%0A%7D%3B%0A%20%0A%2F*%20Helper%20function%20that%20allocates%20a%20new%20node%20with%20the%20given%20key%20and%0A%20%20%20NULL%20left%20and%20right%20%20pointers.*%2F%0Astruct%20node*%20newNode(int%20key)%0A%7B%0A%20%20%20%20struct%20node*%20node%20%3D%20(struct%20node*)malloc(sizeof(struct%20node))%3B%0A%20%20%20%20node-%3Ekey%20%3D%20key%3B%0A%20%20%20%20node-%3Eleft%20%3D%20NULL%3B%0A%20%20%20%20node-%3Eright%20%3D%20NULL%3B%0A%20%20%20%20return(node)%3B%0A%7D%0A%20%0A%2F%2F%20Function%20to%20find%20ceil%20of%20a%20given%20input%20in%20BST.%20If%20input%20is%20more%0A%2F%2F%20than%20the%20max%20key%20in%20BST%2C%20return%20-1%0Aint%20Ceil(node%20*root%2C%20int%20input)%0A%7B%0A%20%20%20%20%2F%2F%20Base%20case%0A%20%20%20%20if(%20root%20%3D%3D%20NULL%20)%0A%20%20%20%20%20%20%20%20return%20-1%3B%0A%20%0A%20%20%20%20%2F%2F%20We%20found%20equal%20key%0A%20%20%20%20if(%20root-%3Ekey%20%3D%3D%20input%20)%0A%20%20%20%20%20%20%20%20return%20root-%3Ekey%3B%0A%20%0A%20%20%20%20%2F%2F%20If%20root\u2019s%20key%20is%20smaller%2C%20ceil%20must%20be%20in%20right%20subtree%0A%20%20%20%20if(%20root-%3Ekey%20%3C%20input%20)%0A%20%20%20%20%20%20%20%20return%20Ceil(root-%3Eright%2C%20input)%3B%0A%20%0A%20%20%20%20%2F%2F%20Else%2C%20either%20left%20subtree%20or%20root%20has%20the%20ceil%20value%0A%20%20%20%20int%20ceil%20%3D%20Ceil(root-%3Eleft%2C%20input)%3B%0A%20%20%20%20return%20(ceil%20%3E%3D%20input)%20%3F%20ceil%20%3A%20root-%3Ekey%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20node%20*root%20%3D%20newNode(8)%3B%0A%20%0A%20%20%20%20root-%3Eleft%20%3D%20newNode(4)%3B%0A%20%20%20%20root-%3Eright%20%3D%20newNode(12)%3B%0A%20%0A%20%20%20%20root-%3Eleft-%3Eleft%20%3D%20newNode(2)%3B%0A%20%20%20%20root-%3Eleft-%3Eright%20%3D%20newNode(6)%3B%0A%20%0A%20%20%20%20root-%3Eright-%3Eleft%20%3D%20newNode(10)%3B%0A%20%20%20%20root-%3Eright-%3Eright%20%3D%20newNode(14)%3B%0A%20%0A%20%20%20%20for(int%20i%20%3D%200%3B%20i%20%3C%2016%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20printf(%22%25d%20%20%25d%5Cn%22%2C%20i%2C%20Ceil(root%2C%20i))%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D%0A\u201d message=\u201dC Program\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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=\u201dbanner\u201d]\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}]}}