2011-11-29 13 views
5

में केवल 3 संख्याएं प्राप्त करना यह रिकर्सन और बाइनरी पेड़ से निपटने वाले स्कूल के लिए एक प्रयोगशाला का हिस्सा है। अगर मैं 4 या 5 नंबर डालने के लिए जाता हूं और परिणाम आउटपुट करता हूं तो मुझे केवल 3 संख्याएं मिलती हैं। यहाँ डालने के लिए कोड है:द्विआधारी पेड़ में 4 या 5 संख्याएं डालने पर आउटपुट

Node *insert(Node *t, int key) { 
    Node *insertParent; 
    Node *result=NULL; 

    if (t!=NULL) { 
     result=search(t,key,insertParent); 
    } else { 
     t=new Node; 
     t->data=key; 
     t->leftchild=NULL; 
     t->rightchild=NULL; 
     return t; 
    } 

    if (result==NULL) { 
     if (insertParent->data>key) { 
      insertParent->leftchild=new Node; 
      insertParent->leftchild->data=key; 
      insertParent->leftchild->leftchild=NULL; 
      insertParent->leftchild->rightchild=NULL; 
      return insertParent->leftchild; 
     } else if (insertParent->data<key) { 
      insertParent->rightchild=new Node; 
      insertParent->rightchild->data=key; 
      insertParent->rightchild->leftchild=NULL; 
      insertParent->rightchild->rightchild=NULL; 
      return insertParent->rightchild; 
     } 
    } else 
     return NULL; 
} 

लेकिन मेरा मानना ​​है कि मुसीबत, संदर्भ माता पिता द्वारा विशेष रूप से नोड सूचक खोज समारोह के भीतर है:

Node* search(Node *t, int key, Node *&parent) { 
    if (t!=NULL) { 
     parent=t; 
     if (t->data==key) 
      return t; 
     else if (t->data>key) 
      return search(t->leftchild,key,t); 
     else 
      return search(t->rightchild,key,t); 
    } else 
     return NULL; 
} 

मैं एक समारोह है कि पेड़ आउटपुट है और है इसे मैन्युअल रूप से बनाए गए पेड़ के खिलाफ चेक किया गया है और यह ठीक काम करता है:

void inorder(Node *t) 
{ 
    if (t!=NULL) { 
     if (t->leftchild!=NULL) 
      inorder(t->leftchild); 

     cout << t->data << ", "; 

     if (t->rightchild!=NULL) 
      inorder(t->rightchild);      
    } 
} 

एक उत्तर की तलाश नहीं है बस मुझे देखना चाहिए।

उत्तर

0

तो मैंने पाया कि मेरी समस्या खोज फ़ंक्शन के साथ थी। इसे संदर्भ पैरेंट नोड चर के साथ करना होगा। मुझे चार अन्य/ifelse कथन का उपयोग करने के लिए यह तय करने में सक्षम होना था कि किस तरह से जाना है, बार-बार या नहीं।

Node *insert(Node *t, int key) { 
    Node *insertParent=NULL; 
    Node *result=NULL; 

    if (t!=NULL) { 
     result=search(t,key,insertParent); 
    } else { 
     t=new Node; 
     t->data=key; 
     t->leftchild=NULL; 
     t->rightchild=NULL; 
     return t; 
    } 

    if (insertParent==NULL && result!=NULL) { 
     return NULL; 
    } else if (insertParent!=NULL && result==NULL) { 
     //key not found need to add 
     if (insertParent->data>key) { 
      insertParent->leftchild=new Node; 
      insertParent->leftchild->data=key; 
      insertParent->leftchild->leftchild=NULL; 
      insertParent->leftchild->rightchild=NULL; 
      return insertParent->leftchild; 
     } else { 
      insertParent->rightchild=new Node; 
      insertParent->rightchild->data=key; 
      insertParent->rightchild->leftchild=NULL; 
      insertParent->rightchild->rightchild=NULL; 
      return insertParent->rightchild; 
     } 
    } 
} 

सब जो मदद करने के लिए धन्यवाद ...

(इसके अलावा:

Node* search(Node *t, int key, Node *&parent) { 
    if (t!=NULL) { 
     if (t->data==key) { 
      parent=NULL; 
      return t; 
     } else if (t->data>key && t->leftchild!=NULL) { 
      return search(t->leftchild,key,parent); // think this needs returned 
     } else if (t->data>key && t->leftchild==NULL) { 
      parent=t; 
      return NULL; 
     } else if (t->data<key && t->rightchild!=NULL) { 
      return search(t->rightchild,key,parent); //think this needs returned 
     } else if (t->data<key && t->rightchild==NULL) { 
      parent=t; 
      return NULL; 
     } 
    } else { 
     parent=NULL; 
     return NULL; 
    } 
} 

खोज समारोह में यह परिवर्तन डालने समारोह में बदलाव जरूरी हो मैं अपने ही सवाल का जवाब दे। क्या यह मुझे करना चाहिए या मुझे अपनी पोस्ट संपादित करनी चाहिए? क्या सोचेंगे कि लोग पूरी प्रक्रिया को देखेंगे और मुझे पुराने गैर-कामकाजी कोड को हटा नहीं देंगे ...)

+0

आपको प्रश्न को स्पष्ट करने के लिए मूल प्रश्न संपादित करना चाहिए, लेकिन जवाब चेक मार्क प्राप्त करना चाहिए। यदि आप अपने स्वयं के प्रश्न का उत्तर देने जा रहे हैं, तो बस "यहां काम करने वाला कोड" न सोचें, लेकिन जैसे कि आप कोई चर्चा पढ़ने से सीखने की कोशिश कर रहे थे। जैसा कि आप नए हैं, मैंने कुछ विचारों को आपके मूल बनाम कुछ सुझाव देने में मदद करने के लिए किया है: http://stackoverflow.com/revisions/8306096/1 – HostileFork

+0

मैंने आपके उत्तर को अपडेट करने के लिए भी अपडेट किया है, जिसके बारे में मैं बात कर रहा हूं। ध्यान दें कि यदि आप अपना 'इनऑर्डर' प्रिंटिंग फ़ंक्शन साझा करने जा रहे हैं, तो आप इसे प्रश्न में भी डाल सकते हैं क्योंकि यह उत्तर देने के दौरान सीखा "नई" चीज़ नहीं थी ... इसलिए मैंने इसे स्थानांतरित कर दिया सवाल। जब आप कोड के लंबे स्क्रॉलिंग बॉक्स को चिपकाने के बजाय वर्णनात्मक टेक्स्ट के साथ शॉर्ट कोड नमूने को इंटरव्यू करते हैं तो आप देख सकते हैं कि कितना आसान है। इसके अलावा स्टैक ओवरफ्लो विकिपीडिया की तरह है और क्यू एंड ए में संशोधन के इतिहास को बचाता है ताकि चीजें खो न जाए: http://stackoverflow.com/revisions/8317919/1 – HostileFork

3

आपका संदेह सही है। एक बार जब आप एक से अधिक नोड गहरे खोजते हैं तो ट्रेस कैसे शीर्ष-स्तर 'पैरेंट' पैरामीटर अपडेट हो जाता है।

0
Node* search(Node *t, int key, Node *&parent) 
{ 
    if(t!=NULL) 
    { 
    parent=t; 
    if (t->data==key) 
     return t; 

    else if (t->data>key) 
     return search(t->leftchild, key, parent); // use “parent” because you want to assign parent to this variable 

    else 
     return search(t->rightchild,key, parent); 
    } 
    else  return NULL; 

} 
+1

"एक ऐसे उत्तर की तलाश नहीं है जो सिर्फ उस क्षेत्र की तलाश में है जिसे मुझे देखना चाहिए।" - अच्छा काम जवाब नहीं दे रहा है। – AShelly

+0

@ एशेलली अभी भी समस्या नहीं है जहां समस्या है? आप सम्मिलित बिंदु पर "insertParent" असाइन करना चाहते हैं। सही? इसलिए, आपको रिकर्सिव बाइनरी खोज के दौरान इसका पता लगाना चाहिए, अन्यथा आपको गलत पैरेंट पॉइंटर मिला है। जिस क्षेत्र को आप देखना चाहते हैं वह सी ++ की धारणा "संदर्भ" है। – jianyi

+0

टी के लिए पैरेंट डालने से समस्या हल नहीं होती है –

संबंधित मुद्दे