二分搜索的查找节点
查看是否包含键key
public boolean contain(Key key) {
return contain(root, key);
}
private boolean contain(Node node, Key key) {
if (node == null) {
return false;
}
if (node.key.compareTo(key) == 0) {
return true;
} else if (node.key.compareTo(key) > 0) {
return contain(node.left, key);
} else {
return contain(node.right, key);
}
}
查找某个键
public Value search(Key key) {
return search(root, key);
}
private Value search(Node node, Key key) {
if (node.value == null) {
return null;
}
if (node.key.compareTo(key) == 0) {
return node.value;
} else if (node.key.compareTo(key) > 0) {
return search(node.left, key);
} else {
return search(node.right, key);
}
}