लिंक्ड बाइनरी ट्री

वोट
-6

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

public boolean contains(Object e)
     {
         return search(root, e);
     }

private boolean search(BinaryNode n, Object e)
    {

        //If the current node is null,
         //then the value clearly isn't found and return false.

         if (n == null) 
         {
             return false;
    } 
        //If the node contains the search value,
        //then the value is found and return true.
         if(n.getValue() == e)
        {
            return true;
        }


         //If the node isn't null but also doesn't contain the search value,
         //then search both the left and right subtrees

         return false;

     }
27/04/2017 को 23:22
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


1 जवाब

वोट
0

यहाँ के एक कार्यान्वयन है Contains()कुछ golang कोड से मैं अपने डेस्कटॉप पर चारों ओर झूठ बोल रही थी। आप जावा के लिए यह बंदरगाह सकता है।

// Contains indicated whether or not a value is in the tree.
func (tree *Tree) Contains(value int) bool {
    return (tree.find(tree.Root, value) != nil)
}

// find will search for a node containing a given value, in a tree whose
// root node is root.
func (tree *Tree) find(root *Node, value int) *Node {
    if nil == root {
        return nil
    }

    if value > root.Data {
        return tree.find(root.Right, value)
    } else if value < root.Data {
        return tree.find(root.Left, value)
    } // else, root.Data == node.Data

    return root
}
28/04/2017 को 00:05
का स्रोत उपयोगकर्ता

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