/**
-
Internal method to find the largest item in a subtree
-
@param t the node that roots the subtree
-
@return the node containing the largest item
*/
private BinaryNode findMax(BinaryNode t){
if (t == null){
return null;
}
if (t.right == null){
return t;
}
return findMax(t.right);
}
[](()1.8、insert
这个主要是根据二叉搜索树的性质,注意当树为空的情况,就可以加入新的节点了,还有当该值已经存在时,默认不进行操作;
/**
-
Internal method to insert into a subtree
-
@param x the item to insert
-
@param t the node that roots the subtree
-
@return the new root of the subtree
*/
private BinaryNode insert(AnyType x, BinaryNode t){
if (t == null){
return new BinaryNode<>(x,null,null);
}
int compareResult = myCompare(x,t.element);
if (compareResult < 0){
t.left = insert(x,t.left);
}
else if (compareResult > 0){
t.right = insert(x,t.right);
}
else{
//Duplicate; do nothing
}
return t;
}