Συνδέεται Binary Tree

ψήφοι
-6

Έχω πρόβλημα αναφέρονται για το πώς να πάρει λειτουργία αναζήτησης μου να εργαστώ για το δυαδικό δέντρο μου επιστρέφει ισχύει και για τη ρίζα, αλλά δεν ξέρω πώς να πάνε για διέρχονται από το υπόλοιπο του δέντρου.

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 είχα βρίσκονται γύρω στην επιφάνεια εργασίας μου. Θα μπορούσατε να το λιμάνι στην Java.

// 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