码迷,mamicode.com
首页 > 其他好文 > 详细

CS 61B lab11

时间:2017-08-04 18:25:06      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:replace   style   follow   isp   pareto   height   roo   bad   remove   

测试结果:

技术分享

技术分享
  private BinaryTreeNode findHelper(Comparable key, BinaryTreeNode node) {
    // Replace the following line with your solution.
      BinaryTreeNode movingnode = node;
      while(movingnode!=null){
          if(key.compareTo((Comparable)(movingnode.entry.key))>0){
              movingnode=movingnode.rightChild;
          }else if(key.compareTo((Comparable)(movingnode.entry.key))<0){
              movingnode=movingnode.leftChild;
          }else return movingnode;
      }
    return null;
  }
find
技术分享
  public Entry remove(Object key) {
    // Replace the following line with your solution.
      if(find(key)==null) return null;
      BinaryTreeNode movingnode = root;
      int flag = 0;   //为了知道找到的node是parent的左孩子还是右孩子还是root本身
      //find the node
      while(movingnode!=null){
          if(((Comparable)key).compareTo((Comparable)(movingnode.entry.key))>0){
              movingnode=movingnode.rightChild;
              flag=1;
          }else if(((Comparable)key).compareTo((Comparable)(movingnode.entry.key))<0){
              movingnode=movingnode.leftChild;
              flag=2;
          }else{
              BinaryTreeNode returnnode=movingnode;  //为了能返回    
              size--;
              if(movingnode.leftChild==null&&movingnode.rightChild==null){
                  if(flag==1){
                      movingnode.parent.rightChild=null;
                  }else if(flag==2){
                      movingnode.parent.leftChild=null;
                  }else root=null;
//                  movingnode=null;
              }else if(movingnode.leftChild!=null&&movingnode.rightChild!=null){
                  BinaryTreeNode targetnode=movingnode;
                  movingnode=movingnode.rightChild;
                  flag=1;
                  while(movingnode.leftChild!=null){
                      movingnode=movingnode.leftChild;
                      flag=2;
                  }
                  targetnode.entry=movingnode.entry;
                  if(flag==1){
                      movingnode.parent.rightChild=movingnode.rightChild;
                      if(movingnode.rightChild!=null){movingnode.rightChild.parent=movingnode.parent;}
                  }else{
                      movingnode.parent.leftChild=movingnode.rightChild;
                      if(movingnode.rightChild!=null){movingnode.rightChild.parent=movingnode.parent;}
                  }
              }else if(movingnode.leftChild!=null){
                  if(flag==1){
                      movingnode.parent.rightChild=movingnode.leftChild;
                      if(movingnode.leftChild!=null){movingnode.leftChild.parent=movingnode.parent;}
                  }else if(flag==2){
                      movingnode.parent.leftChild=movingnode.leftChild;
                      if(movingnode.leftChild!=null){movingnode.leftChild.parent=movingnode.parent;}
                  }else{  //root
                      root=movingnode.leftChild;
                  }
              }else{
                  if(flag==1){
                      movingnode.parent.rightChild=movingnode.rightChild;
                      if(movingnode.rightChild!=null){ movingnode.rightChild.parent=movingnode.parent;}
                  }else if(flag==2){
                      movingnode.parent.leftChild=movingnode.rightChild;
                      if(movingnode.rightChild!=null){movingnode.rightChild.parent=movingnode.parent;}
                  }else{ //root
                      root=movingnode.rightChild;
                  }
              }
              
              return returnnode.entry;
          }
      }
    return null;
  }
remove

期间发现自己一个错。。。天真的以为把node=null可以把它原本指向也变成null。。。哈哈哈写晕了

 

CS 61B lab11

标签:replace   style   follow   isp   pareto   height   roo   bad   remove   

原文地址:http://www.cnblogs.com/developerchen/p/7286263.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!