एक सरणी में रखो एक BST के गहरे पथ (पुनरावर्ती)

वोट
1

इम एक सरणी के लिए एक BST पुनरावर्ती एल्गोरिदम के उपयोग पर गहरी पथ डाल करने के लिए कोशिश कर रहा है, और im कई कठिनाइयों ... हो रही है क्योंकि केवल एक चीज है कि मैं पाने के सबसे लंबे समय तक पथ (ऊंचाई के बराबर) का आकार है, और मैं नहीं कर सकते BST की ऊंचाई के बारे में मान सरणी में डाल ...

कोई मदद?

क्षमा करें, मैं पूरी तरह से समस्या का पर्दाफाश नहीं किया। केवल एक चीज है कि मैं इस एल्गोरिथ्म करने के लिए जानते हैं कि यह हस्ताक्षर है:

//each node has 3 references : value, left and right

private int [] deepestPath(Node root){ ...}

(मैं aux तरीकों का उपयोग कर सकते हैं)

24/07/2009 को 23:04
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


2 जवाब

वोट
1

एक उपकरण के रूप नोड्स का उपयोग कर गहरी पथ को फिर से संगठित करने की कोशिश करो

आपको हो रही समस्या हो सकती है कि आप के रूप में आप पेड़ पार वास्तविक नोड्स स्टोर करने के लिए कोई रास्ता नहीं है कि है। आपको क्या करना होगा "याद" के लिए एक रास्ता है जो आप पत्ती है कि आप गहरी होने के लिए समझे जाते समय अपने द्वारा देखे गए नोड है।

अपने BST नोड्स में प्रतिनिधित्व किया है, तो आप एक संदर्भ भंडारण, प्रत्येक बच्चे में, अपनी मूल करने के लिए विचार करना चाह सकते। इस तरह जब आप गहरी पत्ते को मिला है, आप रिकर्सिवली पथ वापस जड़ तक (: पथ उलटे क्रम में हो जाएगा नोट) को फिर से संगठित कर सकते हैं। इस तरह:

if (isDeepest(node)) { // Once you find the deepest node...
  return reconstructPath(node); // ...reconstruct the path that took you there.
}

...

// reconstructPath is a method that takes a node (the deepest leaf) as 
// an argument and returns an array of the nodes from that node to the root.
private Array reconstructPath(Node node) {
  Array deepestPath = new Array();
  while(node.parent != node) { // Go up until you reach the root, which will be itself.
    deepestPath.add(node); // Add the node to end of the Array
    node = node.parent; // Go up one level to the parent of the node
  }
  deepestPath.reverse(); // reverse the order so it goes root->leaf
  return deepestPath;
}

इस अगर आप नोड्स का उपयोग नहीं करना चाहते करने के लिए अन्य तरीके हैं, लेकिन यह अपने सिर में समस्या कल्पना करने के लिए एक आसान तरीका है।

24/07/2009 को 23:29
का स्रोत उपयोगकर्ता

वोट
0

माता पिता संदर्भ के साथ

ताकि वह अपने माता-पिता के लिए एक संदर्भ है आप प्रत्येक नोड सेट करते हैं, तो आप सिर्फ गहरी नोड पा सकते हैं और उसके बाद माता-पिता के माध्यम से पता लगाने के द्वारा पेड़ की जड़ से ऊपर वहाँ से वापस चलते हैं। यह निश्चित रूप से एक अतिरिक्त होने की कीमत पर करने के लिए सबसे आसान काम है parentNodeप्रत्येक नोड में संदर्भ चर।

# Iterate through parents to trace the path in reverse.
node = deepestNode(tree)

while node.parent != None:
    node = node.parent

माता-पिता के संदर्भ के बिना

आप माता-पिता के संदर्भ की जरूरत नहीं है, तो आप पथ का ट्रैक पेड़ की जड़ से "वर्तमान" नोड के लिए के रूप में आप पेड़ के माध्यम से recurse रख सकते हैं। किसी भी समय आप बाहर नीचे, "सबसे लंबे समय तक पथ अब तक" के रूप में "अब तक सबसे लंबे समय तक पथ" पथ ​​अपने पिछले से अधिक लंबी है, तो उस पथ को बचाने के। प्रभावी ढंग से है कि आपकी कॉल स्टैक स्पष्ट बनाने का मतलब है।

यहाँ कुछ अजगर-इश कोड है:

# Public function. Sets up globals and then calls helper.
def deepestPath(tree):
    global longestPath, currentPath

    # Reset for a new search.
    longestPath = []
    currentPath = []

    _deepestPath(tree.root)

    return longestPath

# Helper function that does the real work.    
def _deepestPath(node):
    global longestPath, currentPath

    currentPath.append(node)

    # No children, we've bottomed out.
    if not node.left and not node.right:
        if currentPath.length > longestPath.length:
            # Save a copy of the current path.
            longestPath = list(currentPath)

    # Recurse into children.
    else:
        if node.left:  _deepestPath(node.left)
        if node.right: _deepestPath(node.right)

    currentPath.pop(node)
24/07/2009 को 23:31
का स्रोत उपयोगकर्ता

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