Given a binary search tree and a value, find the minimum value of the binary search tree not less than a value.
Thinking: the feature of the Binary Search Tree is that the left child node is smaller than the parent node value, and the right child node is larger than the parent node value. If you want to obtain the binary tree to find the root node, consider the following points:
1. If currentnode. getdata () = value, the currentnode node is the requested node.
2. If currentnode. getdata () <value, the value of all nodes in the left subtree of the current node is smaller than value.
To narrow down the search range in the right subtree without considering it in the left subtree.
3. If currentnode. getdata ()> value, the search range is reduced to the current node and Its left subtree. For more details, if the maximum node value in the left subtree of the current node is less than value, then the current node is required. If the maximum node value in the left subtree of the current node is not less than value, narrow down the search range in the left subtree.
So we use recursion.
Node class:
public class Node {int data;Node left;Node right;public Node(){}public Node(int data){this.data=data;left=null;right=null;}public int getData() {return data;}public void setData(int data) {this.data = data;}public Node getLeft() {return left;}public void setLeft(Node left) {this.left = left;}public Node getRight() {return right;}public void setRight(Node right) {this.right = right;}}
Search Tree:
public class SearchTree {Node root;public Node createTree() {Scanner scan=new Scanner(System.in); int number=scan.nextInt();if(number==0){return null;}Node node=new Node(number); node.setLeft(createTree());node.setRight(createTree());root=node;return node; }public Node findTree(Node root, int value){if(root==null){return null;}Node iter=root;if(iter.data<value){return findTree(iter.right,value);}else if(iter.data==value){return iter;}else if(iter.data>value){if(iter.left!=null){int leftTreeMax=maxNode(iter.left);if(leftTreeMax>=value){return findTree(iter.left,value);}else{return iter;}}else{return iter;}}return null;}public int maxNode(Node root){Node iter=root;if(iter!=null){while(iter!=null && iter.right!=null){iter=iter.right;}return iter.getData();}else{return 0;}}public Node getRoot() {return root;}public void setRoot() {this.root = createTree();}}
Test class:
public class Test {public static void main(String [] args){SearchTree searchTree=new SearchTree();Node root=searchTree.createTree();//System.out.println("success");int value=98;System.out.println(">="+value+": "+searchTree.findTree(root, value).getData());value=99;System.out.println(">="+value+": "+searchTree.findTree(root, value).getData());value=106;System.out.println(">="+value+": "+searchTree.findTree(root, value).getData());value=108;System.out.println(">="+value+": "+searchTree.findTree(root, value).getData()); value=115;System.out.println(">="+value+": "+searchTree.findTree(root, value).getData());}}
Input: 100 90 70 60 0 0 85 0 95 92 0 0 98 0 0 110 105 0 0 0 107 0 0
Test results: