{"id":27409,"date":"2018-02-03T21:52:20","date_gmt":"2018-02-03T16:22:20","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27409"},"modified":"2018-02-03T21:52:20","modified_gmt":"2018-02-03T16:22:20","slug":"java-program-print-bst-keys-given-range","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-program-print-bst-keys-given-range\/","title":{"rendered":"Java Program Print BST keys in the given range"},"content":{"rendered":"<p>Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Print all the keys of tree in range k1 to k2. i.e. print all x such that k1<=x<=k2 and x is a key of given BST. Print all the keys in increasing order.<\/p>\n<p>For example, if k1 = 10 and k2 = 22, then your function should print 12, 20 and 22.<\/p>\n<p><strong>Algorithm:<\/strong><br \/>\n1) If value of root\u2019s key is greater than k1, then recursively call in left subtree.<br \/>\n2) If value of root\u2019s key is in range, then print the root\u2019s key.<br \/>\n3) If value of root\u2019s key is smaller than k2, then recursively call in right subtree.<\/p>\n<p><strong>Implementation:<\/strong><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201d%2F%2F%20Java%20program%20to%20print%20BST%20in%20given%20range%0A%20%0A%2F%2F%20A%20binary%20tree%20node%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%20%20%20%20%0A%20%20%20%20static%20Node%20root%3B%0A%20%20%20%20%20%0A%20%20%20%20%2F*%20The%20functions%20prints%20all%20the%20keys%20which%20in%20the%20given%20range%20%5Bk1..k2%5D.%0A%20%20%20%20%20The%20function%20assumes%20than%20k1%20%3C%20k2%20*%2F%0A%20%20%20%20void%20Print(Node%20node%2C%20int%20k1%2C%20int%20k2)%20%7B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%2F*%20base%20case%20*%2F%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%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Since%20the%20desired%20o%2Fp%20is%20sorted%2C%20recurse%20for%20left%20subtree%20first%0A%20%20%20%20%20%20%20%20%20If%20root-%3Edata%20is%20greater%20than%20k1%2C%20then%20only%20we%20can%20get%20o%2Fp%20keys%0A%20%20%20%20%20%20%20%20%20in%20left%20subtree%20*%2F%0A%20%20%20%20%20%20%20%20if%20(k1%20%3C%20node.data)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20Print(node.left%2C%20k1%2C%20k2)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20if%20root\u2019s%20data%20lies%20in%20range%2C%20then%20prints%20root\u2019s%20data%20*%2F%0A%20%20%20%20%20%20%20%20if%20(k1%20%3C%3D%20node.data%20%26%26%20k2%20%3E%3D%20node.data)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(node.data%20%2B%20%22%20%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20If%20root-%3Edata%20is%20smaller%20than%20k2%2C%20then%20only%20we%20can%20get%20o%2Fp%20keys%0A%20%20%20%20%20%20%20%20%20in%20right%20subtree%20*%2F%0A%20%20%20%20%20%20%20%20if%20(k2%20%3E%20node.data)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20Print(node.right%2C%20k1%2C%20k2)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20%20%0A%20%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%20int%20k1%20%3D%2010%2C%20k2%20%3D%2025%3B%0A%20%20%20%20%20%20%20%20tree.root%20%3D%20new%20Node(20)%3B%0A%20%20%20%20%20%20%20%20tree.root.left%20%3D%20new%20Node(8)%3B%0A%20%20%20%20%20%20%20%20tree.root.right%20%3D%20new%20Node(22)%3B%0A%20%20%20%20%20%20%20%20tree.root.left.left%20%3D%20new%20Node(4)%3B%0A%20%20%20%20%20%20%20%20tree.root.left.right%20%3D%20new%20Node(12)%3B%0A%20%0A%20%20%20%20%20%20%20%20tree.Print(root%2C%20k1%2C%20k2)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20This%20code%20has%20been%20contributed%20by%20Mayank%20Jaiswal\u201d message=\u201dJava Programming\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre>12 20 22<\/pre>\n<p><strong>Time Complexity:<\/strong> O(n) where n is the total number of keys in tree.<\/p>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>Java Program Print BST keys in the given range &#8211; Binary Search Tree &#8211; Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree.\n<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,80126,1,2139],"tags":[81560,81558,81559,81566,81565,81561,81563,81562,81564],"class_list":["post-27409","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-binary-search-tree","category-coding","category-java","tag-binary-search-tree-print-java","tag-count-bst-nodes-that-lie-in-a-given-range","tag-count-bst-subtrees-that-lie-in-given-range","tag-java-program-print-bst-keys-in-the-given-range","tag-print-a-binary-search-tree-in-sorted-order","tag-print-binary-search-tree-c","tag-remove-bst-keys-outside-the-given-range","tag-search-range-in-binary-search-tree-leetcode","tag-what-is-the-minimum-height-of-binary-tree-with-n-nodes"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27409","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27409"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27409\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}