findMin lazy deletion binary search tree

user11452926 :

I found this code for findMIn method in lazy deletion for binary search tree. First of all, is this method correct? If it is can someone explain it to me please.

private BinaryNode<AnyType> findMin( BinaryNode<AnyType> t )
{
if (t == null) return null;

BinaryNode<E> tmp= findMin(t.left); // get minimum from the left node

if (tmp != null) return tmp; // if mimimum is not null return minimmum

if (!t.deleted) return t; // if minimum is null in left node and t is not deleted
// return t then

return findMin(t.right); // if t is deleted and minimum of left node of t is null, then search in right side

}

I rewrote it to include the following but it is not working.

 private BinaryNode<AnyType> findMin( BinaryNode<AnyType> t)

      {
      ArrayList<BinaryNode<AnyType>> trv = new ArrayList<BinaryNode<AnyType>>();

         return findMinVal(t, trv);
      }
private BinaryNode<AnyType> findMin( BinaryNode<AnyType> t, ArrayList<BinaryNode<AnyType>> trv )

     {
             if(t == null) {
                 return t;
             } else {
                 BinaryNode<AnyType> left= findMin(t.left);
                 if(!t.deleted) {
                     trv.add(t);
                 }
                 BinaryNode<AnyType> right= findMin(t.right);

                 return trv.get(0);
             }
Prerna Gupta :

This may throw java.lang.IndexOutOfBoundsException, if while doing trv.get(0) arraylist trv size is 0.

In order to avoid it you can check that if size of trv is not 0 then you can return first element of this list else return null inside findMin function

Here is the updated code:

private BinaryNode<AnyType> findMin( BinaryNode<AnyType> t, ArrayList<BinaryNode<AnyType>> trv )

     {
             if(t == null) {
                 return t;
             } else {
                 BinaryNode<AnyType> left= findMin(t.left);
                 if(!t.deleted) {
                     trv.add(t);
                 }
                 BinaryNode<AnyType> right= findMin(t.right);

                 return trv.size()==0 ? null :trv.get(0);
             }
     }

As, an improvement instead of returning BinaryNode<AnyType> from findMin function you can return void since anyhow your result is stored in trv arrylist and instead of checking everytime return trv.size()==0 ? null :trv.get(0); inside findMin function (this will be checked for every recursive call), you can check only once inside helper function.

Here is updated optimized code:

  private BinaryNode<AnyType> findMin( BinaryNode<AnyType> t)

      {
      ArrayList<BinaryNode<AnyType>> trv = new ArrayList<BinaryNode<AnyType>>();

         findMinVal(t, trv);
         return trv.size()==0 ? null :trv.get(0);
      }

private BinaryNode<AnyType> findMin( BinaryNode<AnyType> t, ArrayList<BinaryNode<AnyType>> trv )

     {
             if(t!=null) {
                 findMin(t.left);
                 if(!t.deleted) {
                     trv.add(t);
                 }
                 findMin(t.right);
             }
}

In the above orignal code two if condition are there :

if (tmp != null) return tmp; This is used for printing the returned value since we are not using arraylist kind of data structure in orignal code to store the result so as soon as we get result from findMin recursive call which is not null we are returning it.

if (!t.deleted) return t; This is used for same reason as it is used by your code as if(!t.deleted) {trv.add(t)}i.e. we are checking if findMin(left) returned null and if its root node is not deleted then return that node since it will smaller than this root node right childrens and hence no need to check further.

But, there is no use of if (tmp != null) return tmp; since anyhow each time we will go to if (!t.deleted) return t; and from there as well result can be returned . So this first if can be removed.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=170970&siteId=1