1  public class BTree {
 2    Node myRoot;
 3    BTree left;
 4    BTree right;
 5
 6    BTree(Node n) {
 7      this.myRoot = n;
 8      this.left = null;
 9      this.right = null;
10    }
11
12    BTree(Node n, BTree left){
13      this.myRoot = n;
14      this.left = left;
15      this.right = null;
16    }
17
18    BTree(Node n, BTree left, BTree right){
19      this.myRoot = n;
20      this.left = left;
21      this.right = right;
22    }
23
24    public void setLeftBranch(BTree newLeft){
25      this.left = newLeft;
26    }
27
28    public void setRightBranch(BTree newRight){
29      this.right = newRight;
30    }
31    public String toString(){
32      if (left == null && right==null) {
33        return myRoot.toString();
34      }
35      else if (left != null && right == null){
36        return new String(left.toString()+myRoot.toString());
37      }else if (left == null && right != null){
38        return new String(myRoot.toString() + right.toString());
39      }else {
40        return new String(left.toString()+myRoot.toString()+right.toString());
41      }
42    }
43
44    public int numberOfNodes(){
45      int count = 0 ;
46      if (left == null && right==null) {
47        count++;
48        return count;
49      }
50      else if (left != null && right == null){
51        count++;
52        return count += left.numberOfNodes();
53      }else if (left == null && right != null){
54        count++;
55        return count += right.numberOfNodes();
56      }else {
57        count++;
58        count += left.numberOfNodes();
59        return  count += right.numberOfNodes();
60      }
61    }
62
63  }

Generated with vim2html
Copyright © 2003-2004 by Chip Cuccio <http://norlug.org/~chipster/finger>