Find the closest element in Binary Search Tree
Given a binary search tree and a target node K. The task is to find the node with a minimum absolute difference with the given target value K.
Examples:
Input : k = 4
Output: 4Input : k = 18
Output: 17Input : k = 12
Output: 9Input: k=2
Output: 3
Find closest element in Binary Search Tree by storing Inorder Traversal:
Store Inorder traversal of given binary search tree in an auxiliary array and then by taking absolute difference of each element find the node having minimum absolute difference with given target value K.
Time Complexity: O(N)
Auxiliary Space: O(N)
Find closest element in Binary Search Tree using DFS:
Below is the idea to solve the problem:
Traverse the BST starting from root in a way to search the target node, Keep a variable min_dif and update it with the min of min_dif and abs(current node -> data – target node -> data). If current node is greater than the target node then move to the left of current node else move to the right of current node.
Follow the below steps to implement the idea:
- initialize a variable min_dif to store the minimum difference from the target node.
- Traverse the tree starting from root
- If ptr is equal to NULL then return.
- If ptr->key is equal to k then set min_diff_key = 0 and terminate.
- Else if min_diff > abs(ptr->key – k) then set min_diff = abs(ptr->key – k) and set min_diff_key = ptr->key as the pointer to minimum difference node.
- If k < ptr->key then call for ptr->left.
- Else call for ptr->right.
Below is the Implementation of the above approach:
C++
// Recursive C++ program to find key closest to k // in given Binary Search Tree. #include<bits/stdc++.h> using namespace std; /* A binary tree node has key, pointer to left child and a pointer to right child */ struct Node { int key; struct Node* left, *right; }; /* Utility that allocates a new node with the given key and NULL left and right pointers. */ struct Node* newnode( int key) { struct Node* node = new ( struct Node); node->key = key; node->left = node->right = NULL; return (node); } // Function to find node with minimum absolute // difference with given K // min_diff --> minimum difference till now // min_diff_key --> node having minimum absolute // difference with K void maxDiffUtil( struct Node *ptr, int k, int &min_diff, int &min_diff_key) { if (ptr == NULL) return ; // If k itself is present if (ptr->key == k) { min_diff_key = k; return ; } // update min_diff and min_diff_key by checking // current node value if (min_diff > abs (ptr->key - k)) { min_diff = abs (ptr->key - k); min_diff_key = ptr->key; } // if k is less than ptr->key then move in // left subtree else in right subtree if (k < ptr->key) maxDiffUtil(ptr->left, k, min_diff, min_diff_key); else maxDiffUtil(ptr->right, k, min_diff, min_diff_key); } // Wrapper over maxDiffUtil() int maxDiff(Node *root, int k) { // Initialize minimum difference int min_diff = INT_MAX, min_diff_key = -1; // Find value of min_diff_key (Closest key // in tree with k) maxDiffUtil(root, k, min_diff, min_diff_key); return min_diff_key; } // Driver program to run the case int main() { struct Node *root = newnode(9); root->left = newnode(4); root->right = newnode(17); root->left->left = newnode(3); root->left->right = newnode(6); root->left->right->left = newnode(5); root->left->right->right = newnode(7); root->right->right = newnode(22); root->right->right->left = newnode(20); int k = 18; cout << maxDiff(root, k); return 0; } |
Java
// Recursive Java program to find key closest to k // in given Binary Search Tree. import java.util.*; class solution { static int min_diff, min_diff_key; /* A binary tree node has key, pointer to left child and a pointer to right child */ static class Node { int key; Node left, right; }; /* Utility that allocates a new node with the given key and null left and right pointers. */ static Node newnode( int key) { Node node = new Node(); node.key = key; node.left = node.right = null ; return (node); } // Function to find node with minimum absolute // difference with given K // min_diff -. minimum difference till now // min_diff_key -. node having minimum absolute // difference with K static void maxDiffUtil(Node ptr, int k) { if (ptr == null ) return ; // If k itself is present if (ptr.key == k) { min_diff_key = k; return ; } // update min_diff and min_diff_key by checking // current node value if (min_diff > Math.abs(ptr.key - k)) { min_diff = Math.abs(ptr.key - k); min_diff_key = ptr.key; } // if k is less than ptr.key then move in // left subtree else in right subtree if (k < ptr.key) maxDiffUtil(ptr.left, k); else maxDiffUtil(ptr.right, k); } // Wrapper over maxDiffUtil() static int maxDiff(Node root, int k) { // Initialize minimum difference min_diff = 999999999 ; min_diff_key = - 1 ; // Find value of min_diff_key (Closest key // in tree with k) maxDiffUtil(root, k); return min_diff_key; } // Driver program to run the case public static void main(String args[]) { Node root = newnode( 9 ); root.left = newnode( 4 ); root.right = newnode( 17 ); root.left.left = newnode( 3 ); root.left.right = newnode( 6 ); root.left.right.left = newnode( 5 ); root.left.right.right = newnode( 7 ); root.right.right = newnode( 22 ); root.right.right.left = newnode( 20 ); int k = 18 ; System.out.println( maxDiff(root, k)); } } //contributed by Arnab Kundu |
Python3
# Recursive Python program to find key # closest to k in given Binary Search Tree. # Utility that allocates a new node with the # given key and NULL left and right pointers. class newnode: # Constructor to create a new node def __init__( self , data): self .key = data self .left = None self .right = None # Function to find node with minimum # absolute difference with given K # min_diff --> minimum difference till now # min_diff_key --> node having minimum absolute # difference with K def maxDiffUtil(ptr, k, min_diff, min_diff_key): if ptr = = None : return # If k itself is present if ptr.key = = k: min_diff_key[ 0 ] = k return # update min_diff and min_diff_key by # checking current node value if min_diff > abs (ptr.key - k): min_diff = abs (ptr.key - k) min_diff_key[ 0 ] = ptr.key # if k is less than ptr->key then move # in left subtree else in right subtree if k < ptr.key: maxDiffUtil(ptr.left, k, min_diff, min_diff_key) else : maxDiffUtil(ptr.right, k, min_diff, min_diff_key) # Wrapper over maxDiffUtil() def maxDiff(root, k): # Initialize minimum difference min_diff, min_diff_key = 999999999999 , [ - 1 ] # Find value of min_diff_key (Closest # key in tree with k) maxDiffUtil(root, k, min_diff, min_diff_key) return min_diff_key[ 0 ] # Driver Code if __name__ = = '__main__' : root = newnode( 9 ) root.left = newnode( 4 ) root.right = newnode( 17 ) root.left.left = newnode( 3 ) root.left.right = newnode( 6 ) root.left.right.left = newnode( 5 ) root.left.right.right = newnode( 7 ) root.right.right = newnode( 22 ) root.right.right.left = newnode( 20 ) k = 18 print (maxDiff(root, k)) # This code is contributed by PranchalK |
C#
using System; // Recursive C# program to find key closest to k // in given Binary Search Tree. public class solution { public static int min_diff, min_diff_key; /* A binary tree node has key, pointer to left child and a pointer to right child */ public class Node { public int key; public Node left, right; } /* Utility that allocates a new node with the given key and null left and right pointers. */ public static Node newnode( int key) { Node node = new Node(); node.key = key; node.left = node.right = null ; return (node); } // Function to find node with minimum absolute // difference with given K // min_diff -. minimum difference till now // min_diff_key -. node having minimum absolute // difference with K public static void maxDiffUtil(Node ptr, int k) { if (ptr == null ) { return ; } // If k itself is present if (ptr.key == k) { min_diff_key = k; return ; } // update min_diff and min_diff_key by checking // current node value if (min_diff > Math.Abs(ptr.key - k)) { min_diff = Math.Abs(ptr.key - k); min_diff_key = ptr.key; } // if k is less than ptr.key then move in // left subtree else in right subtree if (k < ptr.key) { maxDiffUtil(ptr.left, k); } else { maxDiffUtil(ptr.right, k); } } // Wrapper over maxDiffUtil() public static int maxDiff(Node root, int k) { // Initialize minimum difference min_diff = 999999999; min_diff_key = -1; // Find value of min_diff_key (Closest key // in tree with k) maxDiffUtil(root, k); return min_diff_key; } // Driver program to run the case public static void Main( string [] args) { Node root = newnode(9); root.left = newnode(4); root.right = newnode(17); root.left.left = newnode(3); root.left.right = newnode(6); root.left.right.left = newnode(5); root.left.right.right = newnode(7); root.right.right = newnode(22); root.right.right.left = newnode(20); int k = 18; Console.WriteLine(maxDiff(root, k)); } } // This code is contributed by Shrikant13 |
Javascript
// Recursive JavaScript program to find key closest to k // in given Binary Search Tree. // A binary tree node has key, pointer to left child and a pointer to right child // allocates a new node with given key and NULL left and right pointers. class Node{ constructor(key){ this .key = key; this .left = null ; this .right = null ; } } let min_diff = Number.MAX_VALUE; let min_diff_key = -1; // Function to find node with minimum absolute // difference with given K // min_diff --> minimum difference till now // min_diff_key --> node having minimum absolute // difference with K function maxDiffUtil(ptr, k){ if (ptr == null ) return ; // if k itself is present if (ptr.key == k){ min_diff_key = k; return ; } // update min_diff and min_diff_key by checking // current node value if (min_diff > Math.abs(ptr.key - k)){ min_diff = Math.abs(ptr.key - k); min_diff_key = ptr.key; } // if k is less than ptr->key then move in // left subtree else in right subtree if (k < ptr.key) maxDiffUtil(ptr.left, k); else maxDiffUtil(ptr.right, k); } // Wrapper over maxDiffUtil() function maxDiff(root, k) { // Find value of min_diff_key (Closest key // in tree with k) maxDiffUtil(root, k); return min_diff_key; } // Driver program to run the case let root = new Node(9); root.left = new Node(4); root.right = new Node(17); root.left.left = new Node(3); root.left.right = new Node(6); root.left.right.left = new Node(5); root.left.right.right = new Node(7); root.right.right = new Node(22); root.right.right.left = new Node(20); let k = 18; console.log(maxDiff(root, k)); // This code is contributed by Yash Agarwal(yashagarwal2852002) |
17
Time complexity: O(H) where h is the height of the given Binary Search Tree.
Auxiliary Space: O(1).
This article is contributed by Shashank Mishra ( Gullu ). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Login to comment...