{"id":28318,"date":"2017-10-15T18:42:53","date_gmt":"2017-10-15T13:12:53","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=28318"},"modified":"2017-10-15T18:42:53","modified_gmt":"2017-10-15T13:12:53","slug":"python-programming-floor-ceil-bst","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/python-programming-floor-ceil-bst\/","title":{"rendered":"Python 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\">Python Program<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python program to find ceil of a given value in BST<br\/> <br\/># A Binary tree node<br\/>class Node:<br\/>     <br\/>    # Constructor to create a new node<br\/>    def __init__(self, data):<br\/>        self.key = data<br\/>        self.left = None<br\/>        self.right = None<br\/> <br\/># Function to find ceil of a given input in BST. If input<br\/># is more than the max key in BST, return -1<br\/>def ceil(root, inp):<br\/>     <br\/>    # Base Case<br\/>    if root == None:<br\/>        return -1<br\/>     <br\/>    # We found equal key<br\/>    if root.key == inp :<br\/>        return root.key <br\/>     <br\/>    # If root&#039;s key is smaller, ceil must be in right subtree<br\/>    if root.key &lt; inp:<br\/>        return ceil(root.right, inp)<br\/>     <br\/>    # Else, either left subtre or root has the ceil value<br\/>    val = ceil(root.left, inp)<br\/>    return val if val &gt;= inp else root.key <br\/> <br\/># Driver program to test above function<br\/>root = Node(8)<br\/> <br\/>root.left = Node(4)<br\/>root.right = Node(12)<br\/> <br\/>root.left.left = Node(2)<br\/>root.left.right = Node(6)<br\/> <br\/>root.right.left = Node(10)<br\/>root.right.right = Node(14)<br\/> <br\/>for i in range(16):<br\/>    print &quot;%d %d&quot; %(i, ceil(root, i))<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-There are numerous applications we need to find floor (ceil) value of a key in a binary search tree or sorted array. <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80126,2139],"tags":[82782,82784,82778,82777,82780,82781,82783,82779],"class_list":["post-28318","post","type-post","status-publish","format-standard","hentry","category-binary-search-tree","category-java","tag-numpy-floor","tag-order-of-precedence-in-python","tag-python-ceil","tag-python-import-math","tag-python-math-domain-error","tag-python-math-operators","tag-python-pow","tag-python-square"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/28318","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=28318"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/28318\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=28318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=28318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=28318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}