{"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 < 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=\u201dpython\u201d manual=\u201d%23%20Python%20program%20to%20find%20ceil%20of%20a%20given%20value%20in%20BST%0A%20%0A%23%20A%20Binary%20tree%20node%0Aclass%20Node%3A%0A%20%20%20%20%20%0A%20%20%20%20%23%20Constructor%20to%20create%20a%20new%20node%0A%20%20%20%20def%20__init__(self%2C%20data)%3A%0A%20%20%20%20%20%20%20%20self.key%20%3D%20data%0A%20%20%20%20%20%20%20%20self.left%20%3D%20None%0A%20%20%20%20%20%20%20%20self.right%20%3D%20None%0A%20%0A%23%20Function%20to%20find%20ceil%20of%20a%20given%20input%20in%20BST.%20If%20input%0A%23%20is%20more%20than%20the%20max%20key%20in%20BST%2C%20return%20-1%0Adef%20ceil(root%2C%20inp)%3A%0A%20%20%20%20%20%0A%20%20%20%20%23%20Base%20Case%0A%20%20%20%20if%20root%20%3D%3D%20None%3A%0A%20%20%20%20%20%20%20%20return%20-1%0A%20%20%20%20%20%0A%20%20%20%20%23%20We%20found%20equal%20key%0A%20%20%20%20if%20root.key%20%3D%3D%20inp%20%3A%0A%20%20%20%20%20%20%20%20return%20root.key%20%0A%20%20%20%20%20%0A%20%20%20%20%23%20If%20root\u2019s%20key%20is%20smaller%2C%20ceil%20must%20be%20in%20right%20subtree%0A%20%20%20%20if%20root.key%20%3C%20inp%3A%0A%20%20%20%20%20%20%20%20return%20ceil(root.right%2C%20inp)%0A%20%20%20%20%20%0A%20%20%20%20%23%20Else%2C%20either%20left%20subtre%20or%20root%20has%20the%20ceil%20value%0A%20%20%20%20val%20%3D%20ceil(root.left%2C%20inp)%0A%20%20%20%20return%20val%20if%20val%20%3E%3D%20inp%20else%20root.key%20%0A%20%0A%23%20Driver%20program%20to%20test%20above%20function%0Aroot%20%3D%20Node(8)%0A%20%0Aroot.left%20%3D%20Node(4)%0Aroot.right%20%3D%20Node(12)%0A%20%0Aroot.left.left%20%3D%20Node(2)%0Aroot.left.right%20%3D%20Node(6)%0A%20%0Aroot.right.left%20%3D%20Node(10)%0Aroot.right.right%20%3D%20Node(14)%0A%20%0Afor%20i%20in%20range(16)%3A%0A%20%20%20%20print%20%22%25d%20%25d%22%20%25(i%2C%20ceil(root%2C%20i))%0A%20\u2033 message=\u201dPython 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-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}]}}