int ld = height(p.left);//返回左子树的高度
int rd = height(p.right);//返回右子树的高度
return (ld >= rd) ? ld+1 : rd+1;//当前子树的高度为较高子树的高度加1
}else
return 0;
}
//获得父母结点
public BinaryNode<E> getParent(BinaryNode<E> node){//返回node的父母结点,若为空树、未找到或node为根,返回null
if((root == null) || (node == null) || (node == root))
return null;
else
return getParent(root,node);
}
private BinaryNode<E> getParent(BinaryNode<E> p,BinaryNode<E> node){//在以p为根结点的子树中查找并返回node结点的父母结点
BinaryNode<E> find = null;
if(p != null){
if(p.left == node || p.right == null)
find = p;//查找成功
else{
find = getParent(p.left,node);//在左子树中查找
if(find == null)
find = getParent(p.right,node);//若左子树中未找到,则继续在右子树中查找
}
}
return find;//返回找到的父母结点
}
//获得左、右孩子
public BinaryNode<E> getLChild(BinaryNode<E> node){//返回node的左孩子,若为空树、未找到或node为叶子,返回null
if((root == null) || (node == null) || (node.left == null) || (node.right == null))
return null;
else
return node.left;
}
public BinaryNode<E> getRChild(BinaryNode<E> node){//返回node的右孩子,若为空树、未找到或node为叶子,返回null
if((root == null) || (node == null) || (node.left == null) || (node.right == null))
return null;
else
return node.right;
&n