{"id":28322,"date":"2017-10-15T18:34:41","date_gmt":"2017-10-15T13:04:41","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=28322"},"modified":"2017-10-15T18:34:41","modified_gmt":"2017-10-15T13:04:41","slug":"java-programming-floor-ceil-bst","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-programming-floor-ceil-bst\/","title":{"rendered":"Java 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=\u201djava\u201d manual=\u201d%2F%2F%20Java%20program%20to%20find%20ceil%20of%20a%20given%20value%20in%20BST%0A%20%0Aclass%20Node%20%7B%0A%20%0A%20%20%20%20int%20data%3B%0A%20%20%20%20Node%20left%2C%20right%3B%0A%20%0A%20%20%20%20Node(int%20d)%20%7B%0A%20%20%20%20%20%20%20%20data%20%3D%20d%3B%0A%20%20%20%20%20%20%20%20left%20%3D%20right%20%3D%20null%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0Aclass%20BinaryTree%20%7B%0A%20%0A%20%20%20%20static%20Node%20root%3B%0A%20%20%20%20%20%0A%20%20%20%20%2F%2F%20Function%20to%20find%20ceil%20of%20a%20given%20input%20in%20BST.%20If%20input%20is%20more%0A%20%20%20%20%2F%2F%20than%20the%20max%20key%20in%20BST%2C%20return%20-1%0A%20%20%20%20int%20Ceil(Node%20node%2C%20int%20input)%20%7B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Base%20case%0A%20%20%20%20%20%20%20%20if%20(node%20%3D%3D%20null)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20-1%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20We%20found%20equal%20key%0A%20%20%20%20%20%20%20%20if%20(node.data%20%3D%3D%20input)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20node.data%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20root\u2019s%20key%20is%20smaller%2C%20ceil%20must%20be%20in%20right%20subtree%0A%20%20%20%20%20%20%20%20if%20(node.data%20%3C%20input)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20Ceil(node.right%2C%20input)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Else%2C%20either%20left%20subtree%20or%20root%20has%20the%20ceil%20value%0A%20%20%20%20%20%20%20%20int%20ceil%20%3D%20Ceil(node.left%2C%20input)%3B%0A%20%20%20%20%20%20%20%20return%20(ceil%20%3E%3D%20input)%20%3F%20ceil%20%3A%20node.data%3B%0A%20%20%20%20%7D%0A%20%20%20%20%20%0A%20%20%20%20%2F%2F%20Driver%20program%20to%20test%20the%20above%20functions%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%20%20%20%20%20%20%20%20BinaryTree%20tree%20%3D%20new%20BinaryTree()%3B%0A%20%20%20%20%20%20%20%20tree.root%20%3D%20new%20Node(8)%3B%0A%20%20%20%20%20%20%20%20tree.root.left%20%3D%20new%20Node(4)%3B%0A%20%20%20%20%20%20%20%20tree.root.right%20%3D%20new%20Node(12)%3B%0A%20%20%20%20%20%20%20%20tree.root.left.left%20%3D%20new%20Node(2)%3B%0A%20%20%20%20%20%20%20%20tree.root.left.right%20%3D%20new%20Node(6)%3B%0A%20%20%20%20%20%20%20%20tree.root.right.left%20%3D%20new%20Node(10)%3B%0A%20%20%20%20%20%20%20%20tree.root.right.right%20%3D%20new%20Node(14)%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%2016%3B%20i%2B%2B)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(i%20%2B%20%22%20%22%20%2B%20tree.Ceil(root%2C%20i))%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D\u201d message=\u201dJava 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 &#8211; 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":[82776,82775,82772,82773,82774,82770,82771,82769],"class_list":["post-28322","post","type-post","status-publish","format-standard","hentry","category-binary-search-tree","tag-convert-double-to-int-in-java","tag-double-to-int-in-java","tag-java-floor-int","tag-java-math-ceil-int","tag-java-math-ceil-integer","tag-math-floor-in-java","tag-math-rint-in-java","tag-round-function-in-java"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/28322","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=28322"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/28322\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=28322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=28322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=28322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}