Class Name: 
kiasan.examples.binsearchtree.BinarySearchTree

Report Rendered: Mon May 04 12:43:42 CDT 2009, by Sireum/Kiasan for Java v0.1.20090504

Methods Covered:
PercentRatio
Class / Method T E Instruction Coverage Branch Coverage Time
21 0
  41/41
  100%
  8/8
  100%
0.030s
5 0
  25/25
  100%
  6/6
  100%
0.015s
5 0
  21/28
  75%
  4/6
  66.67%
0.015s
21 0
  55/55
  100%
  6/6
  100%
0.238s
21 0
  76/83
  91.57%
  14/16
  87.5%
0.034s
Total
73 0
  218/232
  93.97%
  38/42
  90.48%
0.332s
Source Code:
1   package kiasan.examples.binsearchtree;
2   
3   import kiasan.examples.common.Range;
4   
5   // BinarySearchTree class
6   //
7   // CONSTRUCTION: with no initializer
8   //
9   // ******************PUBLIC OPERATIONS*********************
10  // void insert( x )       --> Insert x
11  // void remove( x )       --> Remove x
12  // Comparable find( x )   --> Return item that matches x
13  // Comparable findMin( )  --> Return smallest item
14  // Comparable findMax( )  --> Return largest item
15  // boolean isEmpty( )     --> Return true if empty; else false
16  // void makeEmpty( )      --> Remove all items
17  // void printTree( )      --> Print tree in sorted order
18  
19  /**
20   * Implements an unbalanced binary search tree. Note that all "matching" is
21   * based on the compareTo method.
22   * 
23   * @author Mark Allen Weiss
24   */
25  public class BinarySearchTree {
26  
27    // Test program
28    public static void main(final String[] args) {
29      final BinarySearchTree t = new BinarySearchTree();
30      final int NUMS = 4000;
31      final int GAP = 37;
32  
33      System.out.println("Checking... (no more output means success)");
34  
35      for (int i = GAP; i != 0; i = (+ GAP) % NUMS) {
36        t.insert(i);
37      }
38  
39      for (int i = 1; i < NUMS; i += 2) {
40        t.remove(i);
41      }
42  
43      if (NUMS < 40) {
44        t.printTree();
45        // if( t.findMin( ))).intValue( ) != 2 ||
46        // ((MyInteger)(t.findMax( ))).intValue( ) != NUMS - 2 )
47        // System.out.println( "FindMin or FindMax error!" );
48        //
49        // for( int i = 2; i < NUMS; i+=2 )
50        // if( ((MyInteger)(t.find( new MyInteger( i ) ))).intValue( ) != i )
51        // System.out.println( "Find error1!" );
52        //
53        // for( int i = 1; i < NUMS; i+=2 )
54        // {
55        // if( t.find( new MyInteger( i ) ) != null )
56        // System.out.println( "Find error2!" );
57        // }
58      }
59    }
60  
61    /** The tree root. */
62    public BinaryNode root;
63  
64    /**
65     * Construct the tree.
66     */
67    public BinarySearchTree() {
68      this.root = null;
69    }
70  
71    /**
72     * Internal method to get element field.
73     * 
74     * @param t
75     *          the node.
76     * @return the element field or null if t is null.
77     */
78    private int elementAt(final BinaryNode t) {
79      if (== null) {
80        return -1;
81      }
82      return t.element;
83    }
84  
85    /**
86     * Find an item in the tree.
87     * 
88     * @param x
89     *          the item to search for.
90     * @return the matching item or null if not found.
91     */
92    //@ requires this.repOK();
93    //@ ensures this.repOK();
94    public int find(final int x) {
95      return elementAt(find(x, this.root));
96    }
97  
98    /**
99     * Internal method to find an item in a subtree.
100    * 
101    * @param x
102    *          is item to search for.
103    * @param t
104    *          the node that roots the tree.
105    * @return node containing the matched item.
106    */
107   private BinaryNode find(final int x, final BinaryNode t) {
108     if (== null) {
109       return null;
110     }
111     if (< t.element) {
112       return find(x, t.left);
113     } else if (> t.element) {
114       return find(x, t.right);
115     } else {
116       return t; // Match
117     }
118   }
119 
120   /**
121    * Find the largest item in the tree.
122    * 
123    * @return the largest item of null if empty.
124    */
125   //@ requires this.repOK();
126   //@ ensures this.repOK();
127   public int findMax() {
128     return elementAt(findMax(this.root));
129   }
130 
131   /**
132    * Internal method to find the largest item in a subtree.
133    * 
134    * @param t
135    *          the node that roots the tree.
136    * @return node containing the largest item.
137    */
138   private BinaryNode findMax(BinaryNode t) {
139     if (!= null) {
140       while (t.right != null) {
141         t = t.right;
142       }
143     }
144 
145     return t;
146   }
147 
148   /**
149    * Find the smallest item in the tree.
150    * 
151    * @return smallest item or null if empty.
152    */
153   //@ requires this.repOK();
154   //@ ensures this.repOK();
155   public int findMin() {
156     return elementAt(findMin(this.root));
157   }
158 
159   /**
160    * Internal method to find the smallest item in a subtree.
161    * 
162    * @param t
163    *          the node that roots the tree.
164    * @return node containing the smallest item.
165    */
166   private BinaryNode findMin(final BinaryNode t) {
167     if (== null) {
168       return null;
169     } else if (t.left == null) {
170       return t;
171     }
172     return findMin(t.left);
173   }
174 
175   /**
176    * Insert into the tree; duplicates are ignored.
177    * 
178    * @param x
179    *          the item to insert.
180    */
181 
182   //@ requires this.repOK();
183   //@ ensures this.repOK();
184   public void insert(final int x) {
185     this.root = insert(x, this.root);
186   }
187 
188   /**
189    * Internal method to insert into a subtree.
190    * 
191    * @param x
192    *          the item to insert.
193    * @param t
194    *          the node that roots the tree.
195    * @return the new root.
196    */
197   private BinaryNode insert(final int x, BinaryNode t) {
198     /* 1 */if (== null) {
199       /* 2 */= new BinaryNode(x, null, null);
200     } else if (< t.element) {
201       /* 4 */t.left = insert(x, t.left);
202     } else if (> t.element) {
203       /* 6 */t.right = insert(x, t.right);
204     } else {
205       /* 8 */; // Duplicate; do nothing
206     }
207     /* 9 */
208     return t;
209   }
210 
211   /**
212    * Test if the tree is logically empty.
213    * 
214    * @return true if empty, false otherwise.
215    */
216   public boolean isEmpty() {
217     return this.root == null;
218   }
219 
220   /**
221    * Make the tree logically empty.
222    */
223   public void makeEmpty() {
224     this.root = null;
225   }
226 
227   /**
228    * Print the tree contents in sorted order.
229    */
230   public void printTree() {
231     if (isEmpty()) {
232       System.out.println("Empty tree");
233     } else {
234       printTree(this.root);
235     }
236   }
237 
238   /**
239    * Internal method to print a subtree in sorted order.
240    * 
241    * @param t
242    *          the node that roots the tree.
243    */
244   private void printTree(final BinaryNode t) {
245     if (!= null) {
246       printTree(t.left);
247       System.out.println(t.element);
248       printTree(t.right);
249     }
250   }
251 
252   /**
253    * Remove from the tree. Nothing is done if x is not found.
254    * 
255    * @param x
256    *          the item to remove.
257    */
258   //@ requires this.repOK();
259   //@ ensures this.repOK();
260   public void remove(final int x) {
261     this.root = remove(x, this.root);
262   }
263 
264   /**
265    * Internal method to remove from a subtree.
266    * 
267    * @param x
268    *          the item to remove.
269    * @param t
270    *          the node that roots the tree.
271    * @return the new root.
272    */
273   private BinaryNode remove(final int x, BinaryNode t) {
274     if (== null) {
275       return t; // Item not found; do nothing
276     }
277     if (< t.element) {
278       t.left = remove(x, t.left);
279     } else if (> t.element) {
280       t.right = remove(x, t.right);
281     } else if ((t.left != null) && (t.right != null)) // Two children
282     {
283       t.element = findMin(t.right).element;
284       t.right = remove(t.element, t.right);
285     } else {
286       t = (t.left != null) ? t.left : t.right;
287     }
288     return t;
289   }
290 
291   boolean repOK() {
292     if (this.root == null) {
293       return true;
294     } else {
295       return repOK(this.root);
296     }
297   }
298 
299   private boolean repOK(final BinaryNode t) {
300     return repOK(t, new Range());
301   }
302 
303   private boolean repOK(final BinaryNode t, final Range range) {
304     if (== null) {
305       return true;
306     }
307     if (!range.inRange(t.element)) {
308       return false;
309     }
310     boolean ret = true;
311     ret = ret && repOK(t.left, range.setUpper(t.element));
312     ret = ret && repOK(t.right, range.setLower(t.element));
313     return ret;
314   }
315 }