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

时间: 2024-08-10 21:21:33

CS 61B lab11的相关文章

CS 61B homewok8

测试结果: part one public static LinkedQueue mergeSortedQueues(LinkedQueue q1, LinkedQueue q2) { // Replace the following line with your solution. LinkedQueue mergedQueue = new LinkedQueue(); int flag = 0; Object q1item=0; Object q2item=0; try{ while((!q

CS 61B lab10

part one parent() public TreeNode parent() throws InvalidNodeException { // REPLACE THE FOLLOWING LINE WITH YOUR SOLUTION TO PART I. if(!this.valid){ throw new InvalidNodeException(); } if(this.parent==null) return new SibTreeNode(); return this.pare

CS 61B Lab5

其实我不太确定题目是否是这个意思,我就按照我的理解来答题,欢迎来讨论. part one a b c d 答案都在以下代码中 分析:array的情况和variable情况是一样的,除了代码中array 的cast有点点不一样...在compile过程中,array之间相互assign看的是static type,type一致或者是子类assign给了父类或者父类cast成子类之后assign给子类,这三种情况是可以通过compile time的.但是在run time过程中,看的是dynamic

CS 61B Project1

先上两张结果测试图,这次的project就如作业一开始所说,真的非常time-consuming,要早点做...先放个坑,下午来填.

(转)Awesome Courses

Awesome Courses  Introduction There is a lot of hidden treasure lying within university pages scattered across the internet. This list is an attempt to bring to light those awesome courses which make their high-quality material i.e. assignments, lect

CS文件类头注释

1.修改unity生成CS文件的模板(模板位置:Unity\Editor\Data\Resources\ScriptTemplates 文件名:81-C# Script-NewBehaviourScript.cs) 本人将模板修改为如下图(红框内的内容) 备注:在"#"之间的为可替换的参数 2.修改模板可替换参数,在工程项目Asset文件夹在创建Editor文件 在文件夹下添加AddFileHeadComment.cs文件 内容如下 参数内容根据个人需求修改

CS 和 BS 的区别和优缺点

bs是浏览器(browser)和服务器(server) cs是静态客户端程序(client)和服务器(server) 区别在于,虽然同样是通过一个程序连接到服务器进行网络通讯,但是bs结构的,客户端运行在浏览器里,比如你看百度,就是通过浏览器.还有一些bs结构的应用,比如中国电信,以及一些电子商务平台.用bs结构的好处是,不必专门开发一个客户端界面,可用asp,php,jsp等比较快速开发web应用的程序开发. cs结构的,要做一个客户端.网络游戏基本上大多是cs结构,比如你玩传奇,要专门开个传

微软SQLHelper.cs类 中文版

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Xml; using System.Collections; namespace LiuYanBanT { public class SqlHelper

AssemblyInfo.cs文件详解

一.前言 .net工程的Properties文件夹下自动生成一个名为AssemblyInfo.cs的文件,一般情况下我们很少直接改动该文件.但我们实际上通过另一个形式操作该文件.那就是通过在鼠标右键点击项目的属性进入“应用程序”->“程序集信息”,然后修改信息. 二.作用 通过特性(Attribute)来设置程序集(dll文件)的常规信息,供查看或作为配置信息供程序内部使用. 三.详解 // 程序集标题 [assembly:AssemblyTitle("程序集标题")] // 程