Sunday 15 January 2012

java - Implementing the add method for a Binary Search Tree -


My partner and I implement a binary search tree for a data structure & amp; Algorithm course We are experiencing problems with our addition method This code is shown below:

  Public class BinarySearchTree & lt; Expands the type Comparable & lt;? Super type & gt; & Gt; The sorted set applies & lt; Type & gt; {Binary node & lt; Type & gt; ThisRoot; / ** * Manufacturer * for this binary search / public binary search () {thisRoot = null; } / ** * Adds the specified item to this binary search, if it is not already included in this binary search * * @ Ultimate Type Item * * @ Return Boolean * / Public Boolean Aid (Type Item) {// If the specified item is empty, the exception is teaser. If (item == empty) throw new NullPointerException (); // Otherwise, add item return addItem (item, thisRoot); } Private boolean ed itam (type item, binary node & lt; type & gt; thisRoot) {// base case - check whether this route is unavailable or not. If this is empty, then // we have reached base case where the item is not included in this binary search. Enter item if (thisRoot == faucet) {thisRoot = New BinaryNode & lt; Type & gt; (item); Back true; } // Less Step - // Call the Assistant method recycled until the specified item was found or added. // If the item is less than the data in this node, then go to the left in this binary search. If (item.compareTo (thisRoot.getData ()) <0 return addItem (item, thisRoot.getLeft ()); // if the item is larger than the data in this node, then // go to the right in this binary search Else if item (item.compareTo (thisRoot.getData ()) gt0 return item (item, thisRoot.getRight ()); Else // item already contained in this BinarySearchTree is false;}  < / Pre> 

In the case of our test, we have the result that we are not expecting we initially created an empty binary searchatory and called the addition method From here we have passed an integer object (10) in the method. After doing this, the recursive addItem method should have been applied. This route should be currently referred to as zero ( As we have created a blank binary searchtree) and thus this route now looks at the new BinaryNode object. However, the method is not included in the BST after the call 10. This route still points to the null. To Tips or insight, it may be why, we would appreciate it.

inside addItem method, thisRoot just one Local variables (the second argument of the method is bound to be argued). Resetting this will not change anything other than the method you have New BinaryNode & lt; Type & gt; (Item) that you create for the left or right indicator of an existing node.

(If I am unclear, that's because I do not want to answer.)

No comments:

Post a Comment