There is no subscript in the binary search tree, so for the search operation of the binary search tree, a contain method is defined to determine whether the binary search tree contains an element and returns a Boolean variable. The search operation is also a recursive process. The specific code is as follows:
...
// 查看以node为根的二分搜索树中是否包含键值为key的节点, 使用递归算法
private boolean contain(Node node, Key key){
if( node == null )
return false;
if( key.compareTo(node.key) == 0 )
return true;
else if( key.compareTo(node.key) < 0 )
return contain( node.left , key );
else // key > node->key
return contain( node.right , key );
}
...
The following example looks for 43 elements in the binary search tree

(1) Element 43 is larger than root node 42 and needs to be compared at the right child node.

(2) Element 43 is smaller than 59 and needs to be compared in the left child node.

(3) Element 43 is smaller than 51 and needs to be compared in the left child node.

(4) Look for the left child node 43 of 51, which is exactly equal to the end.
If you need to find the value for key, the code is as follows:
...
// 在以node为根的二分搜索树中查找key所对应的value, 递归算法
// 若value不存在, 则返回NULL
private Value search(Node node, Key key){
if( node == null )
return null;
if( key.compareTo(node.key) == 0 )
return node.value;
else if( key.compareTo(node.key) < 0 )
return search( node.left , key );
else // key > node->key
return search( node.right, key );
}
...
5.17.1. Java instance code ¶
源码包下载: Download
In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress. Src/runoob/binary/BinarySearchTreeSearch.java file code: ¶
package runoob.binary;
/*\*
\* 二分搜索树查找
*/
public class BinarySearchTreeSearch
Principles, Technologies, and Methods of Geographic Information Systems
102