CS 61B homework7

这一次非常自觉的边写边加上了注释hhhhh。为了避免重复代码,写了两个函数。

insertkey():向node插入key

  public void insertkey(int key,Tree234Node node){
      if(key < node.key1){
        if(node.keys==2){node.key3=node.key2;}
        node.key2=node.key1;
        node.key1=key;                
    }else if((node.keys == 1)||(key < node.key2)){
        if(node.keys==2){node.key3=node.key2;}
        node.key2=key;
    }else{
        node.key3=key;
    }    
      node.keys++;
  }

encounter3():判断是否node.keys==3(root除外因为root.keys==3这种情况在insert中考虑了),并作出相应的调整。

 public Tree234Node encounter3(Tree234Node node,String child,int key){
      if(node.keys==3){

        Tree234Node Split1 = new Tree234Node(node.parent,node.key1);
        Tree234Node Split2 = new Tree234Node(node.parent,node.key3);
        size++;
        if(node.child1!=null){ // node如果有孩子一定有四个
            node.child1.parent=Split1;
            Split1.child1=node.child1;
            
            node.child2.parent=Split1;
            Split1.child2=node.child2;
            
            node.child3.parent=Split2;
            Split2.child1=node.child3;
            
            node.child4.parent=Split2;
            Split2.child2=node.child4;
            }
// 当node出现keys==3时,需要提前确定此node属于其parent的哪个child这样分裂之后才方便设置其parent的各个child
if(child.equals("child1")){ //node.parent一定至少2个孩子 node.parent.child1=Split1; if(node.parent.child3!=null){node.parent.child4=node.parent.child3;} node.parent.child3=node.parent.child2; node.parent.child2=Split2; } if(child.equals("child2")){ node.parent.child2=Split1; if(node.parent.child3!=null){node.parent.child4=node.parent.child3;} node.parent.child3=Split2; } if(child.equals("child3")){ node.parent.child3=Split1; node.parent.child4=Split2; } insertkey(node.key2,node.parent); return key>node.key2?Split2:Split1; //要比较分裂之后的重新定位currentnode } return node; }

insert()

  public void insert(int key) {
    // Fill in your solution here.
      if(find(key)){return;}  //no copy
      Tree234Node CurrentNode = root;
      // find the right place to insert

        while ((CurrentNode!=null)&&(CurrentNode.child1!= null)||(CurrentNode!=null)&&CurrentNode.keys==3) { // not (empty or leaf) or root.keys==3            
            if(CurrentNode.keys==3){  //一定是root,因为每次currentnode换,我都会check3一下,唯一没有的是root
                Tree234Node NewRoot = new Tree234Node(null,CurrentNode.key2);
                Tree234Node RootChild1 = new Tree234Node(NewRoot,CurrentNode.key1);
                Tree234Node RootChild2 = new Tree234Node(NewRoot,CurrentNode.key3);
                size=size+2;

                if(root.child1!=null){   // 如果有孩子一定有四个
                root.child1.parent=RootChild1;
                RootChild1.child1=root.child1;
                
                root.child2.parent=RootChild1;
                RootChild1.child2=root.child2;
                
                root.child3.parent=RootChild2;
                RootChild2.child1=root.child3;
                
                root.child4.parent=RootChild2;
                RootChild2.child2=root.child4;
                }
                NewRoot.child1=RootChild1;
                NewRoot.child2=RootChild2;
                root = NewRoot;
                CurrentNode=root;
                continue;
            }
            
            if(key<CurrentNode.key1){ //此时的currentnode不可能有3keys
                CurrentNode=CurrentNode.child1;
                Tree234Node refreshNode = encounter3(CurrentNode,"child1",key);
                CurrentNode = refreshNode;
            }else if((CurrentNode.keys==1)||(key<CurrentNode.key2)){
                CurrentNode=CurrentNode.child2;
                Tree234Node refreshNode = encounter3(CurrentNode,"child2",key);
                CurrentNode = refreshNode;
            }else{
                CurrentNode=CurrentNode.child3;
                Tree234Node refreshNode = encounter3(CurrentNode,"child3",key);
                CurrentNode = refreshNode;
            }
          }
        // begin to insert
        if(size==0){  //  empty  
            Tree234Node NewNode = new Tree234Node(null,key);
            root = NewNode;
            size++;

        }else{  // not empty
            insertkey(key,CurrentNode);
        }
  }

结果

Inserting 84.
84

Inserting 7.
7 84

Inserting 22.
7 22 84

Inserting 95.
(7)22(84 95)

Inserting 50.
(7)22(50 84 95)

Inserting 11.
(7 11)22(50 84 95)

Inserting 37.
(7 11)22(37 50)84(95)

Inserting 60.
(7 11)22(37 50 60)84(95)

Inserting 1.
(1 7 11)22(37 50 60)84(95)

Inserting 23.
(1 7 11)22(23 37)50(60)84(95)

Inserting 16.
((1)7(11 16)22(23 37))50((60)84(95))

Inserting 100.
((1)7(11 16)22(23 37))50((60)84(95 100))

Inserting 28.
((1)7(11 16)22(23 28 37))50((60)84(95 100))

Inserting 86.
((1)7(11 16)22(23 28 37))50((60)84(86 95 100))

Inserting 49.
((1)7(11 16)22(23)28(37 49))50((60)84(86 95 100))

Inserting 81.
((1)7(11 16)22(23)28(37 49))50((60 81)84(86 95 100))

Inserting 51.
((1)7(11 16)22(23)28(37 49))50((51 60 81)84(86 95 100))

Inserting 99.
((1)7(11 16)22(23)28(37 49))50((51 60 81)84(86)95(99 100))

Inserting 75.
((1)7(11 16)22(23)28(37 49))50((51)60(75 81)84(86)95(99 100))

Inserting 66.
((1)7(11 16)22(23)28(37 49))50((51)60(66 75 81))84((86)95(99 100))

Inserting 4.
((1 4)7(11 16))22((23)28(37 49))50((51)60(66 75 81))84((86)95(99 100))

Inserting 80.
(((1 4)7(11 16))22((23)28(37 49)))50(((51)60(66)75(80 81))84((86)95(99 100)))

 

posted @ 2017-08-03 21:57  切力  阅读(156)  评论(0编辑  收藏  举报