कुंजी कैसे मैं एक BST में उस कुंजी के लिए निकटतम नोड मिल रहा है के रूप में पूर्णांक मूल्यों के साथ एक BST को देखते हुए? BST (जावा) नोड्स के एक वस्तु का उपयोग कर प्रस्तुत किया जाता है। निकटतम जैसे 4,5,9 के लिए हो जाएगा और यदि कुंजी 6 यह 5 वापस आ जाएगी ..
कैसे एक द्विआधारी खोज वृक्ष में किसी कुंजी के मूल्य के सबसे करीब तत्व को खोजने के लिए?
सबसे आसान समाधान कब से अपने पेड़ recurse करने के लिए है
- आप तत्व को खोजने
- आप एक पत्ता तक पहुँचते हैं। इस मामले में आप अगर निकटतम मूल्य पत्ती या पत्ती की मूल है तुलना की एक जोड़ी निर्धारित करने के लिए करना चाहिए।
आप पर निर्भर कार्यान्वयन।
पेड़ पार के रूप में आप तत्व को खोजने के लिए होगा। आपको लगता है कि रिकॉर्ड मूल्य है कि अपने प्रमुख के सबसे करीब है करते हैं। अब जब आप कुंजी ही दर्ज की गई मान के लिए एक नोड नहीं मिला।
तो अगर आप कुंजी देख रहे थे 3आप नोड पर खत्म होगा निम्नलिखित पेड़ में 6एक मैच खोजने के बिना लेकिन अपने रिकॉर्ड मूल्य होगा 2, क्योंकि यह सभी नोड्स है कि आप चल था (के निकटतम महत्वपूर्ण था 2, 7, 6)।
2
1 7
6 8
Traverse हे (एन) समय लगता है। हम इसे ऊपर से नीचे में आगे बढ़ना कर सकते हैं? इस पुनरावर्ती कोड की तरह:
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;
}
(लॉग * n *) समय यह हे में हल किया जा सकता।
- यदि एक नोड में मान दिया गया मान के समान है, यह सबसे करीब नोड है,
- यदि एक नोड में मूल्य को देखते हुए मूल्य से अधिक है, छोड़ दिया बच्चे को ले जाने के;
- यदि एक नोड में मूल्य को देखते हुए मूल्य से कम है, सही बच्चे को ले जाते हैं।
एल्गोरिथ्म निम्नलिखित सी ++ कोड के साथ लागू किया जा सकता है:
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;
}
आप पर जा सकते हैं अपने ब्लॉग अधिक जानकारी के लिए।
यह एक पंक्ति और एक ArrayList का उपयोग किया जा सकता है। कतार के पेड़ पर एक चौड़ाई पहले खोज करने के लिए इस्तेमाल किया जाएगा। ArrayList चौड़ाई पहले के आदेश में पेड़ के तत्व स्टोर करने के लिए इस्तेमाल किया जाएगा। यहाँ एक ही लागू करने के लिए कोड है
Queue queue = new LinkedList();
ArrayList list = new ArrayList();
int i =0;
public Node findNextRightNode(Node root,int key)
{
System.out.print("The breadth first search on Tree : \t");
if(root == null)
return null;
queue.clear();
queue.add(root);
while(!queue.isEmpty() )
{
Node node = (Node)queue.remove();
System.out.print(node.data + " ");
list.add(node);
if(node.left != null) queue.add(node.left);
if(node.right !=null) queue.add(node.right);
}
Iterator iter = list.iterator();
while(iter.hasNext())
{
if(((Node)iter.next()).data == key)
{
return ((Node)iter.next());
}
}
return null;
}
दृष्टिकोण के साथ समस्या यह है कि अनुक्रम जिसमें तत्वों BST बनाने के लिए दर्ज किए गए थे से अधिक निर्भर करता है कि "सही ट्रावर्सल और निकटतम खोजने छोड़ दिया"। अगर हम BST अनुक्रम 22, 15, 16, 6,14,3,1,90 के लिए खोज 11, उपरोक्त विधि 15 वापस आ जाएगी, जबकि सही उत्तर 14. एकमात्र तरीका प्रत्यावर्तन का उपयोग करना चाहिए सभी नोड्स पार करने के लिए है, पुनरावर्ती क्रिया के परिणाम के रूप करीबी को वापस लौटाते। यह हमारे सबसे करीब मूल्य दे देंगे
यहाँ पायथन में एक पुनरावर्ती समाधान है:
def searchForClosestNodeHelper(root, val, closestNode):
if root is None:
return closestNode
if root.val == val:
return root
if closestNode is None or abs(root.val - val) < abs(closestNode.val - val):
closestNode = root
if val < root.val:
return searchForClosestNodeHelper(root.left, val, closestNode)
else:
return searchForClosestNodeHelper(root.right, val, closestNode)
def searchForClosestNode(root, val):
return searchForClosestNodeHelper(root, val, None)
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);
}
}
नीचे एक जो मेरे पास है विभिन्न नमूनों के साथ काम करता है।
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;
}
यहाँ एक BST में निकटतम तत्व को खोजने के लिए पूर्ण जावा कोड है।
package binarytree;
class BSTNode {
BSTNode left,right;
int data;
public BSTNode(int data) {
this.data = data;
this.left = this.right = null;
}
}
class BST {
BSTNode root;
public static BST createBST() {
BST bst = new BST();
bst.root = new BSTNode(9);
bst.root.left = new BSTNode(4);
bst.root.right = new BSTNode(17);
bst.root.left.left = new BSTNode(3);
bst.root.left.right= new BSTNode(6);
bst.root.left.right.left= new BSTNode(5);
bst.root.left.right.right= new BSTNode(7);
bst.root.right.right = new BSTNode(22);
bst.root.right.right.left = new BSTNode(20);
return bst;
}
}
public class ClosestElementInBST {
public static void main(String[] args) {
BST bst = BST.createBST();
int target = 18;
BSTNode currentClosest = null;
BSTNode closestNode = findClosestElement(bst.root, target, currentClosest);
if(closestNode != null) {
System.out.println("Found closest node: " + closestNode.data);
}
else {
System.out.println("Couldn't find closest node.");
}
}
private static BSTNode findClosestElement(BSTNode node, int target, BSTNode currentClosest) {
if(node == null) return currentClosest;
if(currentClosest == null ||
(currentClosest != null && (Math.abs(currentClosest.data - target) > Math.abs(node.data - target)))) {
currentClosest = node;
}
if(node.data == target) return node;
else if(target < node.data) {
return findClosestElement(node.left, target, currentClosest);
}
else { //target > node.data
currentClosest = node;
return findClosestElement(node.right, target, currentClosest);
}
}
}
यहाँ जो BST और अतिरिक्त पूर्णांक की विशेषताओं का उपयोग करता है कम से कम अंतर स्टोर करने के लिए जावा में काम कर समाधान है
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);
}
}
}
चलने का समय जटिलता - ओ (logn)
अंतरिक्ष समय जटिलता - ओ (1)













