How to find the closest element to a given key value in a binary search tree?

后端 未结 10 962
刺人心
刺人心 2020-12-25 14:58

Given a bst with integer values as keys how do I find the closest node to that key in a bst ? The BST is represented using a object of nodes (Java). Closest will be for eg 4

相关标签:
10条回答
  • 2020-12-25 15:15

    Here is the working solution in java which uses the characteristics of BST and additional integer to store minimum difference

    public class ClosestValueBinaryTree {
            static int closestValue;
    
            public static void closestValueBST(Node22 node, int target) {
                if (node == null) {
                    return;
                }
                if (node.data - target == 0) {
                    closestValue = node.data;
                    return;
                }
                if (Math.abs(node.data - target) < Math.abs(closestValue - target)) {
                    closestValue = node.data;
                }
                if (node.data - target < 0) {
                    closestValueBST(node.right, target);
                } else {
                    closestValueBST(node.left, target);
                }
            }
        }
    

    Run time complexity - O(logN)

    Space time complexity - O(1)

    0 讨论(0)
  • 2020-12-25 15:17

    Traverse takes O(n) time. Can we proceed it in top-bottom? like this recursive code:

    Tnode * closestBST(Tnode * root, int val){
        if(root->val == val)
            return root;
        if(val < root->val){
            if(!root->left)
                return root;
            Tnode * p = closestBST(root->left, val);
            return abs(p->val-val) > abs(root->val-val) ? root : p;
        }else{
            if(!root->right)
                return root;
            Tnode * p = closestBST(root->right, val);
            return abs(p->val-val) > abs(root->val-val) ? root : p;
        }   
        return null;
    }
    
    0 讨论(0)
  • 2020-12-25 15:18
    void closestNode(Node root, int k , Node result) {
        if(root == null) 
        {
           return;      //currently result is null , so it  will be the result
        }
        if(result == null || Math.abs(root.data - k) < Math.abs(result.data - k) )
        {
          result == root;
        }
        if(k < root.data)
        {
        closestNode(root.left, k, result)
        } 
        else 
        {
            closestNode(root.right, k, result);
        }
    
    }
    
    0 讨论(0)
  • 2020-12-25 15:26

    Below one works with different samples which I have.

    public Node findNearest(Node root, int k) {
        if (root == null) {
            return null;
        }
        int minDiff = 0;
        Node minAt = root;
        minDiff = Math.abs(k - root.data);
    
        while (root != null) {
            if (k == root.data) {
                return root;
            }
            if (k < root.data) {
                minAt = updateMin(root, k, minDiff, minAt);
                root = root.left;
            } else if (k > root.data) {
                minAt = updateMin(root, k, minDiff, minAt);
                root = root.right;
            }
    
        }
        return minAt;
    }
    
    private Node updateMin(Node root, int k, int minDiff, Node minAt) {
        int curDif;
        curDif = Math.abs(k - root.data);
        if (curDif < minDiff) {
            minAt = root;
        }
        return minAt;
    }
    
    0 讨论(0)
  • 2020-12-25 15:27

    It can be solved in O(log*n*) time.

    • If the value in a node is same as the given value, it's the closest node;
    • If the value in a node is greater than the given value, move to the left child;
    • If the value in a node is less than the given value, move to the right child.

    The algorithm can be implemented with the following C++ code:

    BinaryTreeNode* getClosestNode(BinaryTreeNode* pRoot, int value)
    {
        BinaryTreeNode* pClosest = NULL;
        int minDistance = 0x7FFFFFFF;
        BinaryTreeNode* pNode = pRoot;
        while(pNode != NULL){
            int distance = abs(pNode->m_nValue - value);
            if(distance < minDistance){
                minDistance = distance;
                pClosest = pNode;
            }
    
            if(distance == 0)
                break;
    
            if(pNode->m_nValue > value)
                pNode = pNode->m_pLeft;
            else if(pNode->m_nValue < value)
                pNode = pNode->m_pRight;
        }
    
        return pClosest;
    }
    

    You may visit my blog for more details.

    0 讨论(0)
  • 2020-12-25 15:28

    Traverse the tree as you would to find the element. While you do that record the value that is closest to your key. Now when you didn't find a node for the key itself return the recorded value.

    So if you were looking for the key 3 in the following tree you would end up on the node 6 without finding a match but your recorded value would be 2 since this was the closest key of all nodes that you had traversed (2,7,6).

                     2
                  1      7
                       6   8
    
    0 讨论(0)
提交回复
热议问题