0

I have made a generic Binary Heap (MaxHeap) in which I have to search for a particular node based upon the value present in the node. I have made it such that the search function using Pre-OrderTraversal and it should give a run time of Order n where n is the number of nodes in the Heap. My code doesn't seem to work. It never goes into the second 'else if' present in the preOrderT function. Could you suggest what changes can be made in it?

My node class has been defined to contain an integer key (according to which the heap is arranged), a generic object value, and the references to the parent, leftChild and rightChild.

public Node<E> search(E p){
    Node<E> N;

    N= preOrderT(root, p);
    return N;
}

public Node<E> preOrderT(Node<E> N, E p){
    Node<E> M=null;

    if (N.value==p) M=N;
    else if (M==null && N.leftChild!=null){ M=preOrderT(N.leftChild, p);}
    else if (M==null && N.rightChild!=null){ M=preOrderT(N.rightChild, p);}
    return M;
}
Avi
  • 1
  • 1

3 Answers3

2

The issue is if there is a leftChild, then you never give it a chance to check the rightChild. Instead, once you've completed checking the leftChild, check the rightChild if the value hasn't been found yet.

Jeremy
  • 22,188
  • 4
  • 68
  • 81
  • I still don't understand why it doesn't go into the second 'else if'. Had the first else if loop given a null value for M, shouldn't the program go into the second else if? – Avi Mar 04 '17 at 19:29
  • Because if the first `else if` passes, the second `else if` will never run, regardless of what happens in the body of the first. – Jeremy Mar 04 '17 at 19:46
1

You need to update your function as:

public Node<E> preOrderT(Node<E> N, E p){
Node<E> M=null;

if (N.value==p) M=N;
else 
{
  if (N.leftChild!=null){ M=preOrderT(N.leftChild, p);}
  if (N.rightChild!=null){ M=preOrderT(N.rightChild, p);}
}
return M;

}

  • Thanks a ton. I had been stuck on this problem for two hours and couldn't figure out what had gone wrong. My code works after I made the suggested edits. – Avi Mar 04 '17 at 19:26
  • I still don't understand why it doesn't go into the second 'else if'. Had the first else if loop given a null value for M, shouldn't the program go into the second else if? – Avi Mar 04 '17 at 19:30
  • 'else if' is executed only when 'if' is not executed. In your case, you had this scenario: 'if' and 'else if' and again 'else if' so at a time only one of these three cases can get executed. In your case, last else if will get executed only if the first two conditions are not satisfied. That is, when "N.value is not equal to p" and "N.leftChild is equal to null" only then you'll check for third 'else if' condition. – Zobia Khawaja Mar 04 '17 at 19:39
0

change as below.

if (N.value==p) M=N;
    else {
      if (M==null && N.leftChild!=null){ M=preOrderT(N.leftChild, p);}
      if (M==null && N.rightChild!=null){ M=preOrderT(N.rightChild, p);}
}
Prasanna Ab
  • 116
  • 2
  • 6
  • Thanks a ton. I had been stuck on this problem for two hours and couldn't figure out what had gone wrong. My code works after I made the suggested edits. – Avi Mar 04 '17 at 19:27
  • I still don't understand why it doesn't go into the second 'else if'. Had the first else if loop given a null value for M, shouldn't the program go into the second else if? – Avi Mar 04 '17 at 19:30