{"id":26969,"date":"2018-01-02T20:35:49","date_gmt":"2018-01-02T15:05:49","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26969"},"modified":"2018-01-02T20:35:49","modified_gmt":"2018-01-02T15:05:49","slug":"java-program-find-node-minimum-value-binary-search-tree","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-program-find-node-minimum-value-binary-search-tree\/","title":{"rendered":"Java Program Find the node with minimum value in a Binary Search Tree"},"content":{"rendered":"<p>This is quite simple. Just traverse the node from root to left recursively until left is NULL. The node whose left is NULL is the node with minimum value.<\/p>\n<p>For the above tree, we start with 20, then we move left 8, we keep on moving to left until we see NULL. Since left of 4 is NULL, 4 is the node with minimum value.<\/p>\n[pastacode lang=\u201djava\u201d manual=\u201d%2F%2F%20Java%20program%20to%20find%20minimum%20value%20node%20in%20Binary%20Search%20Tree%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%0A%20%20%20%20static%20Node%20head%3B%0A%20%20%20%20%20%0A%20%20%20%20%2F*%20Given%20a%20binary%20search%20tree%20and%20a%20number%2C%20%0A%20%20%20%20%20inserts%20a%20new%20node%20with%20the%20given%20number%20in%20%0A%20%20%20%20%20the%20correct%20place%20in%20the%20tree.%20Returns%20the%20new%20%0A%20%20%20%20%20root%20pointer%20which%20the%20caller%20should%20then%20use%20%0A%20%20%20%20%20(the%20standard%20trick%20to%20avoid%20using%20reference%20%0A%20%20%20%20%20parameters).%20*%2F%0A%20%20%20%20Node%20insert(Node%20node%2C%20int%20data)%20%7B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%2F*%201.%20If%20the%20tree%20is%20empty%2C%20return%20a%20new%2C%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20single%20node%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%20(new%20Node(data))%3B%0A%20%20%20%20%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F*%202.%20Otherwise%2C%20recur%20down%20the%20tree%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(data%20%3C%3D%20node.data)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20node.left%20%3D%20insert(node.left%2C%20data)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20node.right%20%3D%20insert(node.right%2C%20data)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20return%20the%20(unchanged)%20node%20pointer%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20node%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20Given%20a%20non-empty%20binary%20search%20tree%2C%20%20%0A%20%20%20%20%20return%20the%20minimum%20data%20value%20found%20in%20that%20%0A%20%20%20%20%20tree.%20Note%20that%20the%20entire%20tree%20does%20not%20need%20%0A%20%20%20%20%20to%20be%20searched.%20*%2F%0A%20%20%20%20int%20minvalue(Node%20node)%20%7B%0A%20%20%20%20%20%20%20%20Node%20current%20%3D%20node%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20loop%20down%20to%20find%20the%20leftmost%20leaf%20*%2F%0A%20%20%20%20%20%20%20%20while%20(current.left%20!%3D%20null)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20current%20%3D%20current.left%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20return%20(current.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%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%20Node%20root%20%3D%20null%3B%0A%20%20%20%20%20%20%20%20root%20%3D%20tree.insert(root%2C%204)%3B%0A%20%20%20%20%20%20%20%20tree.insert(root%2C%202)%3B%0A%20%20%20%20%20%20%20%20tree.insert(root%2C%201)%3B%0A%20%20%20%20%20%20%20%20tree.insert(root%2C%203)%3B%0A%20%20%20%20%20%20%20%20tree.insert(root%2C%206)%3B%0A%20%20%20%20%20%20%20%20tree.insert(root%2C%205)%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.println(%22The%20minimum%20value%20of%20BST%20is%20%22%20%2B%20tree.minvalue(root))%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>Time Complexity:<\/strong> O(n) Worst case happens for left skewed trees.<br \/>\nSimilarly we can get the maximum value by recursively traversing the right node of a binary search tree.<\/p>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>Java Program &#8211; Find the node with minimum value in a Binary Search Tree &#8211; Just traverse the node from root to left recursively until left is NULL.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80126,1,2139],"tags":[80551,80554,80548,80569,80571,80570,80543,80572,70290,80565,71255,80574,80544,80541,70280,80560],"class_list":["post-26969","post","type-post","status-publish","format-standard","hentry","category-binary-search-tree","category-coding","category-java","tag-binary-search-tree-traversal-inorder-preorder-postorder-example","tag-define-bst","tag-definition-of-binary-search-tree","tag-find-max-value-in-binary-tree-java","tag-find-maximum-in-binary-search-tree","tag-find-minimum-value-in-binary-tree-java","tag-how-to-construct-a-binary-tree","tag-isbst","tag-java-program-for-binary-search","tag-min-search","tag-min-tree","tag-program-of-binary-search-tree","tag-properties-of-binary-tree-in-data-structure","tag-root-binary","tag-search-tree","tag-write-an-algorithm-for-binary-search-with-example"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26969","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=26969"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26969\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}