cs61b lab10

运行结果:

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

 

posted @ 2017-07-29 17:35  想做码农的熊孩子  阅读(243)  评论(0编辑  收藏  举报