newSearch(Node node, int data) {
Node candidate = null;
while (node != null) {
if (data < node.data) {
node = node.leftChild; // { *** Try left subtree *** }
} else {
candidate = node;// { *** Save last rightward node *** }
node = node.rightChild;// { *** Try right subtree *** }
}
}
if (candidate != null && candidate.data == data) {
return candidate;
} else {
return null;
}
}
here only one way comparision is done ie only less-than.
Node candidate = null;
while (node != null) {
if (data < node.data) {
node = node.leftChild; // { *** Try left subtree *** }
} else {
candidate = node;// { *** Save last rightward node *** }
node = node.rightChild;// { *** Try right subtree *** }
}
}
if (candidate != null && candidate.data == data) {
return candidate;
} else {
return null;
}
}
here only one way comparision is done ie only less-than.
No comments:
Post a Comment