Java 树重平衡

Java 树重平衡,java,tree,height,implementation,Java,Tree,Height,Implementation,在第7行中,我得到了nullpointerexception,我如何修复它?有什么想法吗 private void updateHeights(BSTreeNode v) { BSTreeNode u = v; while (u != null) { int bfc=updateNodeHeight(u); u = u.parent; if (bfc<=-2) { if( getHeight(u.left.left) >= getHe

在第7行中,我得到了nullpointerexception,我如何修复它?有什么想法吗

private void updateHeights(BSTreeNode v) {
  BSTreeNode u = v;

  while (u != null) {
    int bfc=updateNodeHeight(u);
    u = u.parent;

    if (bfc<=-2) {

      if( getHeight(u.left.left) >= getHeight(u.left.right) ) { // Null Pointer Exception
        u = rotateRight(u);

      } else {
        u=LR(u);
      }
    }
  }
}   
private void updateheight(bScreenode v){
b屏蔽极u=v;
while(u!=null){
int bfc=更新高度(u);
u=u.父母;
如果(bfc=getHeight(u.left.right)){//Null指针异常
u=旋转右(u);
}否则{
u=LR(u);
}
}
}
}   

有两个可能的问题和尽可能多的解决方案。首先是在
getHeight()
中放置一个空检入,我假设这是根据您的编程风格(有条件时的检入)完成的。这就给u.left留下了一个可能的空指针问题。因此:

确保u.left不为null,如下所示:

getHeight(u.left != null && ((u.left.left)>=getHeight(u.left.right))

u、 left.left对象可能为null。。。检查=空条件和sysout和SEE这并不能提供问题的答案。若要评论或要求作者澄清,请在其帖子下方留下评论。-有两个可能的问题,以及解决它们的方法。首先是在
getHeight()
中放置一个空签入,我假设它是基于while条件签入完成的。这使得
u.left
可能存在空指针问题。因此,我将此作为一个答案发布。我并不反对你的答案,只是一般需要一个详细的答案。这就是我在“查看”模式下标记的原因。我建议你最好解释“为什么”,你的答案可能是解决方案,使之成为一个完美的解决方案我很感激你的反馈,我已经更新了我的答案!玛丽·圣诞节:)现在没事了!祝你圣诞快乐