zoukankan      html  css  js  c++  java
  • CS 61B lab11

    测试结果:

      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。。。哈哈哈写晕了

    发现可以直接利用一下findhelper,写了一个另一个版本的remove,判断node为左孩子还是右孩子的方法还可以比较key。代码短了一点点哈哈

      public Entry remove(Object key) {
          BinaryTreeNode movingnode = findHelper((Comparable)key,root);
          if(findHelper((Comparable)key,root)==null){return null;}
          size--;
          BinaryTreeNode returnnode=movingnode;
          if(movingnode.leftChild==null&&movingnode.rightChild==null){
              if(movingnode.parent==null){
                  root=null;              
              }else if(((Comparable)movingnode.parent.entry.key).compareTo((Comparable)(movingnode.entry.key))<0){
                  movingnode.parent.rightChild=null;
              }else movingnode.parent.leftChild=null;;
    //          movingnode=null;
          }else if(movingnode.leftChild!=null&&movingnode.rightChild!=null){
              BinaryTreeNode targetnode=movingnode;
              movingnode=movingnode.rightChild;
              while(movingnode.leftChild!=null){
                  movingnode=movingnode.leftChild;
              }
              if(((Comparable)movingnode.parent.entry.key).compareTo((Comparable)(movingnode.entry.key))<0){
                  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;}
              }
              targetnode.entry=movingnode.entry;
          }else if(movingnode.leftChild!=null){
              if(movingnode.parent==null){
                  root=movingnode.leftChild;              
              }else if(((Comparable)movingnode.parent.entry.key).compareTo((Comparable)(movingnode.entry.key))>0){
                  movingnode.parent.leftChild=movingnode.leftChild;
                  if(movingnode.leftChild!=null){movingnode.leftChild.parent=movingnode.parent;}
              }else{  //root
                  movingnode.parent.rightChild=movingnode.leftChild;
                  if(movingnode.leftChild!=null){movingnode.leftChild.parent=movingnode.parent;}
              }
          }else{
              if(movingnode.parent==null){
                  root=movingnode.rightChild;              
              }else if(((Comparable)movingnode.parent.entry.key).compareTo((Comparable)(movingnode.entry.key))>0){
                  movingnode.parent.leftChild=movingnode.rightChild;
                  if(movingnode.rightChild!=null){movingnode.rightChild.parent=movingnode.parent;}
              }else{ //root
                  movingnode.parent.rightChild=movingnode.rightChild;
                  if(movingnode.rightChild!=null){ movingnode.rightChild.parent=movingnode.parent;}
              }
          }
          return returnnode.entry;
      }
    remove edition2

    太傻了。。。我到底在想些什么,刚刚看熊孩子的判断node为左孩子还是右孩子,直接判断node==node.parent.rightChild?不就行了。。我到底在想些什么。。。。。。

      public Entry remove(Object key) {
          BinaryTreeNode movingnode = findHelper((Comparable)key,root);
          if(findHelper((Comparable)key,root)==null){return null;}
          size--;
          BinaryTreeNode returnnode=movingnode;
          if(movingnode.leftChild==null&&movingnode.rightChild==null){
              if(movingnode.parent==null){
                  root=null;              
    //      }else if(((Comparable)movingnode.parent.entry.key).compareTo((Comparable)(movingnode.entry.key))<0){
              }else if(movingnode.parent.rightChild==movingnode){  
                  movingnode.parent.rightChild=null;
              }else movingnode.parent.leftChild=null;;
    //          movingnode=null;
          }else if(movingnode.leftChild!=null&&movingnode.rightChild!=null){
              BinaryTreeNode targetnode=movingnode;
              movingnode=movingnode.rightChild;
              while(movingnode.leftChild!=null){
                  movingnode=movingnode.leftChild;
              }
    //          if(((Comparable)movingnode.parent.entry.key).compareTo((Comparable)(movingnode.entry.key))<0){
              if(movingnode.parent.rightChild==movingnode){
                  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;}
              }
              targetnode.entry=movingnode.entry;
          }else if(movingnode.leftChild!=null){
              if(movingnode.parent==null){
                  root=movingnode.leftChild;              
    //          }else if(((Comparable)movingnode.parent.entry.key).compareTo((Comparable)(movingnode.entry.key))>0){
              }else if(movingnode.parent.leftChild==movingnode){
                  movingnode.parent.leftChild=movingnode.leftChild;
                  if(movingnode.leftChild!=null){movingnode.leftChild.parent=movingnode.parent;}
              }else{  //root
                  movingnode.parent.rightChild=movingnode.leftChild;
                  if(movingnode.leftChild!=null){movingnode.leftChild.parent=movingnode.parent;}
              }
          }else{
              if(movingnode.parent==null){
                  root=movingnode.rightChild;              
    //          }else if(((Comparable)movingnode.parent.entry.key).compareTo((Comparable)(movingnode.entry.key))>0){
              }else if(movingnode.parent.leftChild==movingnode){
                  movingnode.parent.leftChild=movingnode.rightChild;
                  if(movingnode.rightChild!=null){movingnode.rightChild.parent=movingnode.parent;}
              }else{ //root
                  movingnode.parent.rightChild=movingnode.rightChild;
                  if(movingnode.rightChild!=null){ movingnode.rightChild.parent=movingnode.parent;}
              }
          }
          return returnnode.entry;
      }
    baochiweixiao
  • 相关阅读:
    Unity c# 状态机的简单入门
    python实战教程之自动扫雷(自己存下来学习之用)
    Kubernetes的三种外部访问方式:NodePort、LoadBalancer和Ingress-十一(1)
    Ubuntu安装eclipse以及创建快捷方式
    Dockerfile-HEALTHCHECK指令
    各个版本Microsoft Visual C++运行库下载
    docker 远程连接设置
    centos7安装redis3.2.12
    Windows下允许redis远程访问
    UltraISO制作U盘启动盘-centos7
  • 原文地址:https://www.cnblogs.com/developerchen/p/7286263.html
Copyright © 2011-2022 走看看