zoukankan      html  css  js  c++  java
  • cs61b homework7

    作业要求只要求实现insert,不过实现起来真的好麻烦啊,感觉可能是最开始分类分的繁琐了,所以后续代码就越加越长,注意在插入的过程中要调整树的结构,如果node中的keys数为3的话,如果该node是root,就拆成三个,如果非root,是将该node中的key2移上去,感觉这点实现起来特别麻烦。

    插一波代码(感觉这波代码过长了。。。)

     public void insert(int key) {
        Tree234Node node=root;
        if(node==null){
            root=new Tree234Node(null,key);
        }
        else{
        while(node!=null){
            if(node.keys==3){
                if(node==root){
                Tree234Node newnode1=new Tree234Node(root,1);
                newnode1.key1=root.key1;
                newnode1.child1=root.child1;
                newnode1.child2=root.child2;
                if(root.child1!=null)
                root.child1.parent=newnode1;
                if(root.child2!=null)
                root.child2.parent=newnode1;
                Tree234Node newnode2=new Tree234Node(root,1);
                newnode2.key1=root.key3;
                newnode2.child1=root.child3;
                newnode2.child2=root.child4;
                if(root.child3!=null)
                root.child3.parent=newnode2;
                if(root.child4!=null)
                root.child4.parent=newnode2;
                root.keys=1;
                root.key1=root.key2;
                root.child1=newnode1;
                root.child2=newnode2;
                node=root;
                }
                else{
                    if(node.parent.keys==1){
                        //System.out.println(node.parent.key1);
                        //System.out.println(node.key1+" "+node.key2+" "+node.key3);
                            node.parent.keys++;
                            Tree234Node newnode1=new Tree234Node(node.parent,1);
                            newnode1.key1=node.key1;
                            newnode1.child1=node.child1;
                            if(node.child1!=null)
                            node.child1.parent=newnode1;
                            newnode1.child2=node.child2;
                            if(node.child2!=null)
                            node.child2.parent=newnode1;
                            Tree234Node newnode2=new Tree234Node(node.parent,1);
                            newnode2.key1=node.key3;
                            newnode2.child1=node.child3;
                            if(node.child3!=null)
                            node.child3.parent=newnode2;
                            newnode2.child2=node.child4;
                            if(node.child4!=null)
                            node.child4.parent=newnode2;
                            if(node.key2>node.parent.key1){
                                node.parent.key2=node.key2;
                                node.parent.child2=newnode1;
                                node.parent.child3=newnode2;
                            }
                            else if(node.key2<node.parent.key1){
                                node.parent.key2=node.parent.key1;
                                node.parent.key1=node.key2;
                                node.parent.child3=node.parent.child2;
                                node.parent.child1=newnode1;
                                node.parent.child2=newnode2;
                            }
                            
                            
                        
                    }
                    else if(node.parent.keys==2){
                        node.parent.keys++;
                        Tree234Node newnode1=new Tree234Node(node.parent,1);
                        newnode1.key1=node.key1;
                        newnode1.child1=node.child1;
                        if(node.child1!=null)
                        node.child1.parent=newnode1;
                        newnode1.child2=node.child2;
                        if(node.child2!=null)
                        node.child2.parent=newnode1;
                        Tree234Node newnode2=new Tree234Node(node.parent,1);
                        newnode2.key1=node.key3;
                        newnode2.child1=node.child3;
                        if(node.child3!=null)
                        node.child3.parent=newnode2;
                        newnode2.child2=node.child4;
                        if(node.child4!=null)
                        node.child4.parent=newnode2;
                        if(node.key2<node.parent.key1){
                            node.parent.key3=node.parent.key2;
                            node.parent.key2=node.parent.key1;
                            node.parent.key1=node.key2;
                            node.parent.child4=node.parent.child3;
                            node.parent.child3=node.parent.child2;
                            node.parent.child1=newnode1;
                            node.parent.child2=newnode2;
                        }
                        else if(node.key2>node.parent.key2){
                            node.parent.key3=node.key2;
                            node.parent.child3=newnode1;
                            node.parent.child4=newnode2;
                        }
                        else{
                            node.parent.key3=node.parent.key2;
                            node.parent.key2=node.key2;
                            node.parent.child4=node.parent.child3;
                            node.parent.child2=newnode1;
                            node.parent.child3=newnode2;
                        }
                    }
                    node=node.parent;
                    
                    
                }
            }
            if(key==node.key1)
                return;
            else if(key<node.key1&&node.keys==1){
                if(node.child1==null){
                    node.keys++;
                    node.key2=node.key1;
                    node.key1=key;
                    size++;
                    break;
                }
                else{
                    node=node.child1;
                    //System.out.println(node.key1);
                }
            }
            else if(key>node.key1&&node.keys==1){
                if(node.child2==null){
                    node.keys++;
                    node.key2=key;
                    size++;
                    break;
                }
                else{
                    node=node.child2;
                }
            }
            else if(key==node.key2)
                return;
            else if(key<node.key1&&node.keys==2){
                if(node.child1==null){
                    node.keys++;
                    node.key3=node.key2;
                    node.key2=node.key1;
                    node.key1=key;
                    size++;
                    break;
                }
                else{
                    node=node.child1;
                }
            }
            else if(key>node.key1&&key<node.key2&&node.keys==2){
                if(node.child2==null){
                    node.keys++;
                    node.key3=node.key2;
                    node.key2=key;
                    size++;
                    break;
                }
                else{
                    node=node.child2;
                }
            }
            else if(key>node.key2&&node.keys==2){
                if(node.child3==null){
                    node.keys++;
                    node.key3=key;
                    size++;
                    break;
                }
                else{
                    node=node.child3;
                }
            }
            else if(node.keys==3){
                if(key<node.key1){
                    node=node.child1;
                }
                else if(node.key1<key&&key<node.key2)
                    node=node.child2;
                else if(key>node.key2&&key<node.key3)
                    node=node.child3;
                else if(key>node.key3)
                    node=node.child4;
                
            }
        }
        }
      }
    View Code

    运行结果:

    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)))
    
    Final tree:
              100
              99
         95
              86
              81
              80
         75
              66
         60
              51
                   100
                   99
              95
                   86
         84
                   81
                   80
              75
                   66
              60
                   51
    50
                   49
                   37
              28
                   23
         22
                   16
                   11
              7
                   4
                   1
  • 相关阅读:
    java中Object转String
    Spring中属性文件properties的读取与使用
    Java中parseInt()和valueOf(),toString()的区别
    php第二十一节课
    php第二十节课
    php第十九节课
    php第十八节课
    php第十七节课
    php第十五节课
    php第十四节课
  • 原文地址:https://www.cnblogs.com/lyz1995/p/7256916.html
Copyright © 2011-2022 走看看