Friday, September 13, 2013

one way search in BST (better than binary search)

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.

No comments:

Post a Comment