2 द्विआधारी खोज के पेड़ के चौराहे

वोट
1

अरे, तो मैं एक नया पेड़ जो मूल रूप से 2 दिया द्विआधारी खोज के पेड़ के चौराहे (चौराहे के गणितीय परिभाषा) है बनाना चाहते हैं। मैं एक विधि है कि पेड़ की एक विशेष स्तर पर सभी नोड्स बाहर प्रिंट है और मैं एक विधि है कि बाहर tree.I की गहराई पा सकते हैं अपने काम चिपकाने हूँ अब तक हालांकि यह अधूरा है और मैं के साथ अटक कर रहा हूँ है logic.Help सराहना की जाएगी।

    public static Bst<Customer> intersect (Bst<Customer> a, Bst<Customer> b){
    Bst<Customer> result = new Bst<Customer>();
    BTNode<Customer> cur1;
    BTNode<Customer> cur2;
    BTNode<Customer> cur3;
    cur1=a.root;
    cur2=b.root;
    cur3=result.root;
    int Resultdepth;
    if(a.maxDepth()<b.maxDepth())
        Resultdepth=a.maxDepth();
    else
        Resultdepth=b.maxDepth();

    if(cur1==null || cur2==null){ // Handeling the root case intially
        result = null;
    }
    else 
      cur3.item.set_account_id(cur1.item.get_accountid()+ cur2.item.get_accountid());

    cur1=cur1.left;
    cur2=cur2.left;
    cur3=cur3.left;       

    while(<some check>){

    }


    return result;

}


    public int maxDepth(){
        return mD(root);
    }

    int mD(BTNode<E> node){
       if (node==null) {
            return(0);
        }
       else {
            int lDepth = mD(node.left);
            int rDepth = mD(node.right);
            // use the larger + 1
            return(Math.max(lDepth, rDepth) + 1);
        }
    }

     // for printing the nodes at a particular level and giving the starting level
      public void PrintAt(BTNode<E> cur, int level, int desiredLevel) {
         if (cur == null) {
            return;
        }
         if (level == desiredLevel) {
             System.out.print(cur.item.toString() + );
          }
         else {
             PrintAt(cur.left, level+1, desiredLevel);
             PrintAt(cur.right, level+1, desiredLevel);
          }
}
20/04/2011 को 11:22
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


4 जवाब

वोट
0

दो पेड़ों के चौराहे शायद नोड्स है कि दोनों पेड़ों में हो रहा है?

यह देखते हुए कि आप पेड़ यह करने के लिए पता लगाने के लिए होगा, क्यों नहीं बस एक में आदेश ट्रावर्सल, कर नोड्स की दुकान और फिर एक चौराहे आपरेशन का आदेश दिया सूची में करते हैं?

20/04/2011 को 11:33
का स्रोत उपयोगकर्ता

वोट
3

आप एक ही समय में और "सिंक में" क्रम में दोनों पेड़ Traversal किया है।

मैं अपनी कक्षा के लिए Iterable इंटरफ़ेस को लागू करने का सुझाव चाहते हैं। तो फिर तुम दोनों पेड़ से पहले मान मिलता है। वे बराबर हैं तो इस पर नए पेड़ में डाल दिया, और दोनों iterators से अगले मूल्यों को प्राप्त। यदि नहीं, तो छोटे मूल्यों के साथ इटरेटर पुनरावृति जब तक मूल्य मिल के रूप में कम से कम के रूप में अन्य पुनरावर्तक से अंतिम मान के रूप में बड़ा है। धोये और दोहराएं।

20/04/2011 को 12:19
का स्रोत उपयोगकर्ता

वोट
0

इस तरह के एक चौराहे के लिए मेरा सुझाव सरल है:

यह देखते हुए पेड़ एक और पेड़ बी, पेड़ सी = एक \ बी एक दूसरे को काटना खोजने के लिए:

1: कॉपी या तो पेड़ एक या बी हमें स्पष्टता के लिए एक मान लेते हैं।
यह प्रतिलिपि अब आपके पेड़ सी अब यह ट्रिम 'जाने है।
2: सी = C.root_node और ख के लिए = B.root_node:
यदि ख == ग,
नोड्स b.left साथ प्रक्रिया दोहराएँ, c.left
नोड्स b.right साथ प्रक्रिया दोहराएँ, c.right
बाकी,
ग निकालें ( जिससे बाद के सभी बच्चों को दूर करने, यह वे असमान हैं निहित है)

यदि यह कार्यान्वयन काम करेगा, यह iterators और तरह के उपयोग से बचने, और एक सरल पुनरावर्ती ट्रेवर्सल करने के लिए नीचे उबाल होगा। ( इस तरह! )

पूछें कि क्या आप आगे स्पष्टीकरण चाहते हैं।

सादर।

20/04/2011 को 22:38
का स्रोत उपयोगकर्ता

वोट
0

दो द्विआधारी खोज के पेड़ के चौराहे पाने की पुनरावर्ती कार्यान्वयन के लिए, मैं निम्नलिखित कोड के साथ आया था। मैं समय जटिलता के बहुत यकीन नहीं है, लेकिन यह सब ठीक काम करता है।

शून्य BST :: findIntersection (सेल * root1, सेल * root2) {

if(root1 == NULL ) { 
//  cout<<"Tree 1 node is null , returning"<<endl;  
    return;
}
if(root2 == NULL) {
//  cout<<"Tree 2 node is null , returning"<<endl;  
    return;
}
//cout<<"Comparing tree 1 : "<<root1->data<< "   and tree 2 : " << root2->data<<endl;
if(root1->data==root2->data) {
//  cout<<"tree 1 equal to tree 2 "<<endl;
    insert(root1->data);
//  cout<<"Inserting in new tree : "<<root1->data<<endl;
    findIntersection(root1->left,root2->left);
    findIntersection(root1->right, root2->right);
}
else if(root1->data>root2->data) {
//  cout<<"tree 1 > tree 2 "<<endl;
    findIntersection(root1,root2->right);
    findIntersection(root1->left, root2);
}
else  {
//  cout<<"tree 1 < tree 2 "<<endl;
    findIntersection(root1->right,root2);
    findIntersection(root1, root2->left);
}

}

17/09/2012 को 14:58
का स्रोत उपयोगकर्ता

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more