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;
}