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

cs61b lab10

时间:2017-07-29 18:18:20      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:else   ret   gif   like   exception   tree   close   val   splay   

运行结果:

Creating 2-node tree.
Testing parent().

Testing insertChild().  Adding two more nodes to the 2-node tree.
Adding two more nodes to the 4-node tree.
1132
2131
The tree looks like this:
1
  11
  12
  13
    131
    132
[The above sequence should be 1, 11, 12, 13, 131, 132.]

Testing removeLeaf().  Removing one node from 6-node tree.
Removing another node from 5-node tree.
Attempting to remove non-leaf node from 4-node tree.
  Operation should have no effect.
Attempting to remove invalid node from 4-node tree.
The tree looks like this:
1
  12
  13
    131
    132
[The above sequence should be 1, 12, 13, 131.]
Removing remaining nodes from 4-node tree.

part1:实现parent()

技术分享
 public TreeNode parent() throws InvalidNodeException {
    if(isValidNode()){
        if(this==myTree.root){
            return new SibTreeNode();
        }
        else{
            return this.parent;
        }
    }
    else{
        throw new InvalidNodeException();
    }
  }
View Code

part2:insertChild()分的情况比较多,代码也调整了很久,也比较长。

技术分享
 1  public void insertChild(Object item, int c) throws InvalidNodeException {
 2     if(isValidNode()){
 3         if(this.firstChild==null||!this.firstChild.valid){
 4             SibTreeNode treenode=new SibTreeNode(myTree,item);
 5             this.firstChild=treenode;
 6             treenode.parent=this;
 7             myTree.size++;
 8         }
 9         else{
10             SibTreeNode node=this.firstChild;
11             if(c==1){
12                 SibTreeNode newnode=new SibTreeNode(myTree,item);
13                 newnode.parent=this;
14                 newnode.nextSibling=node;
15                 this.firstChild=newnode;
16                 myTree.size++;
17                     }
18             else if(c<=this.children()){
19             while(c>2){
20                 node=node.nextSibling;
21                 c--;
22             }
23             SibTreeNode newnode=new SibTreeNode(myTree,item);
24             newnode.parent=this;
25             newnode.nextSibling=node.nextSibling;
26             node.nextSibling=newnode;
27             myTree.size++;
28         }
29         else{
30             while(node.nextSibling!=null){
31                 node=node.nextSibling;
32             }
33             SibTreeNode newnode=new SibTreeNode(myTree,item);
34             newnode.parent=this;
35             node.nextSibling=newnode;
36             myTree.size++;
37         }
38         }
39         
40     }
41     else{
42         throw new InvalidNodeException();
43     }
44   }
View Code

part3:removeLeaf()也是需要考虑leaf在child中的具体位置,分了几种情况。

技术分享
 1 public void removeLeaf() throws InvalidNodeException {
 2       if(isValidNode()){
 3     if(this.firstChild!=null&&this.firstChild.isValidNode())
 4         return;
 5     else{
 6         if(this==myTree.root){
 7             this.valid=false;
 8             myTree.size--;
 9         }
10         else if(this==this.parent.firstChild){
11          this.parent.firstChild=this.nextSibling;
12          this.valid=false;
13          myTree.size--;
14      }
15      else if(this.nextSibling==null||!this.nextSibling.isValidNode())
16         {
17          this.valid=false;
18          myTree.size--;
19         }
20     else{
21         SibTreeNode node=this.parent.firstChild;
22         while(node.nextSibling!=this)
23             node=node.nextSibling;
24         node.nextSibling=this.nextSibling;
25         this.valid=false;
26         myTree.size--;
27     }
28   }
29   }
30       else{
31           throw new InvalidNodeException();
32       }
33 }
View Code

 

cs61b lab10

标签:else   ret   gif   like   exception   tree   close   val   splay   

原文地址:http://www.cnblogs.com/lyz1995/p/7256898.html

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