जावा जेनेरिक द्विआधारी खोज वृक्ष प्रकार मुद्दा

वोट
1

मैं इस होमवर्क कि मानो मुझे भ्रमित कर रहा है पर काम कर रहा हूँ ...

मैं निम्नलिखित BinarySearchTree वर्ग के साथ प्रदान की कर रहा हूँ

import java.util.NoSuchElementException;

/**
 *
 * @param <T> The type of data stored in the nodes of the tree, must implement  Comparable<T> with the compareTo method.
 */
public class BinarySearchTree<T extends Comparable<T>> {


    BinaryTree<T> tree;

    int size;
    public BinarySearchTree() {
        tree = new BinaryTree<T>();
        size = 0;
    }

    public boolean isEmpty() {
        return tree.isEmpty();
    }

    protected BinaryTree<T> recursiveSearch(BinaryTree<T> root, T key) {
        if (root == null) {
            return null;
        }
        int c = key.compareTo(root.data);
        if (c == 0) {
            return root;
        }
        if (c < 0) {
            return recursiveSearch(root.left, key);
        } else {
            return recursiveSearch(root.right, key);
        }
    }

    public T search(T key) {
        if (tree.isEmpty()) { 
            return null;
        }
        return recursiveSearch(tree, key).data;
    }

    public void insert(T item) {

        if (tree.isEmpty()) { // insert here
            tree.makeRoot(item);
            size++;
            return;
        }

        // do an iterative descent
        BinaryTree<T> root = tree;
        boolean done=false;
        BinaryTree<T> newNode = null;
        while (!done) {
            int c = item.compareTo(root.data);
            if (c == 0) { // duplicate found, cannot be inserted
                throw new OrderViolationException();
            }
            if (c < 0) { // insert in left subtree
                if (root.left == null) { // insert here as left child
                    newNode = new BinaryTree<T>();
                    root.left = newNode;
                    done=true;
                } else { // go further down left subtree
                    root = root.left;
                }
            } else { // insert in right subtree
                if (root.right == null) { // insert here as right child 
                    newNode = new BinaryTree<T>();
                    root.right = newNode;
                    done=true;
                } else { // go further down right subtree
                    root = root.right;
                }
            }
        }
        // set fields of new node
        newNode.data = item;
        newNode.parent = root;
        size++;
    }

    /**
     * @param deleteNode Node whose parent will receive new node as right or left child,
     *                  depending on whether this node is its parent's right or left child. 
     * @param attach The node to be attached to parent of deleteNode.
     */
    protected void deleteHere(BinaryTree<T> deleteNode, BinaryTree<T> attach) {

        // deleteNode has only one subtree, attach
        BinaryTree<T> parent = deleteNode.parent;
        deleteNode.clear();  // clear the fields
        if (parent == null) {
            return;
        }
        if (deleteNode == parent.left) {
            // left child of parent, attach as left subtree
            parent.detachLeft();
            parent.attachLeft(attach);
            return;
        }
        // attach as right subtree
        parent.detachRight();
        parent.attachRight(attach);
    }


    protected BinaryTree<T> findPredecessor(BinaryTree<T> node) {
        if (node.left == null) {
            return null;
        }
        BinaryTree<T> pred = node.left; // turn left once
        while (pred.right != null) { // keep turning right
            pred = pred.right;
        }
        return pred;
    }


    public T delete(T key) {
        if (tree.isEmpty()) { // can't delete from an empty tree
            throw new NoSuchElementException();
        }

        // find node containing key 
        BinaryTree<T> deleteNode = recursiveSearch(tree, key);
        if (deleteNode == null) { // data not found, can't delete
            throw new NoSuchElementException();
        }

        BinaryTree<T> hold;

        // case c: deleteNode has exactly two subtrees
        if (deleteNode.right != null && deleteNode.left != null) {
            hold = findPredecessor(deleteNode);
            deleteNode.data = hold.data;
            deleteNode = hold; // fall through to case a or b
        }

        // case a: deleteNode is a leaf
        if (deleteNode.left == null && deleteNode.right == null) {
            deleteHere(deleteNode, null);
            size--;
            return deleteNode.data;
        }       

        // case b: deleteNode has exactly one subtree
        if (deleteNode.right != null) {
            hold = deleteNode.right;
            deleteNode.right = null;
        } else {
            hold = deleteNode.left;
            deleteNode.left = null;
        }

        deleteHere(deleteNode,hold);
        if (tree == deleteNode) { // root deleted
            tree = hold;
        }
        size--;
        return deleteNode.data;
    }


    public T minKey() {
        if (tree.data == null) { // tree empty, can't find min value
            throw new NoSuchElementException();
        }

        BinaryTree<T> root = tree;
        T min=root.data;
        root = root.left;  // turn left once
        while (root != null) {  // keep going left to leftmost node
            min = root.data;
            root = root.left;
        }
        return min;
    }


    public T maxKey() {
        if (tree.getData() == null) { // tree empty, can't find max value
            throw new NoSuchElementException();
        }

        BinaryTree<T> root=tree;
        T max=root.data;
        root = root.right;  // turn right once
        while (root != null) { // keep going to rightmost node
            max = root.data;
            root = root.right;
        }
        return max;
    }


    public int size() {
        return size;
    }


    protected void recursivePreOrder(BinaryTree<T> root, Visitor<T> visitor) {
        if (root != null) {
            visitor.visit(root);
            recursivePreOrder(root.left, visitor);
            recursivePreOrder(root.right, visitor);
        }
    }


    public void preOrder(Visitor<T> visitor) {
        if (tree.isEmpty()) {
            return;
        }
        recursivePreOrder(tree, visitor);
    }


    protected void recursiveInOrder(BinaryTree<T> root, Visitor<T> visitor) {
        if (root != null) {
            recursiveInOrder(root.left, visitor);
            visitor.visit(root);
            recursiveInOrder(root.right, visitor);
        }
    }


    public void inOrder(Visitor<T> visitor) {
        if (tree.isEmpty()) {   
            return;
        }
        recursiveInOrder(tree, visitor);
    }


    protected void recursivePostOrder(BinaryTree<T> root, Visitor<T> visitor) {
        if (root != null) {
            recursivePostOrder(root.left, visitor);
            recursivePostOrder(root.right, visitor);
            visitor.visit(root);
        }
    }

    public void postOrder(Visitor<T> visitor) {
        if (tree.isEmpty()) {
            return;
        }
        recursivePostOrder(tree, visitor);
    }
}

================================================== ==============================

अब मैं किसी अन्य वर्ग के छात्र है .... मैं छात्र वस्तुओं की एक द्विआधारी खोज वृक्ष बनाना चाहते हैं ..

BinarySearchTree<Student> tree = new BinarySearchTree<Student>();

लेकिन जब मुझे लगता है कि मैं निम्न त्रुटि मिलता है:

बाउंड बेमेल: प्रकार छात्र प्रकार BinarySearchTree की घिरे पैरामीटर के लिए एक वैध विकल्प> नहीं है

कोई भी विचार यहाँ क्या हो रहा है ... मैं इसे समझ नहीं कर सकते हैं।

02/05/2009 को 06:31
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


3 जवाब

वोट
0

वर्ग छात्र तुलनीय को लागू करता है?

02/05/2009 को 06:41
का स्रोत उपयोगकर्ता

वोट
0

लेकिन मैं काफी यकीन है कि compareTo विधि लागू करने के लिए कैसे नहीं हूँ।

मूल रूप से यह निम्नलिखित की तरह कुछ है। कैसे काम करता है आदेश आप तय करना पड़ता है।

class Student implements Comparable<Student> {

    //...

    int compareTo(Student other) {
        // return some negative number if this object is less than other
        // return 0 if this object is equal to other
        // return some positive number if this object is greater than other
    }
}
02/05/2009 को 06:56
का स्रोत उपयोगकर्ता

वोट
6

 public class BinarySearchTree<T extends Comparable<T>> 

एक औपचारिक जेनरिक तर्क, अपने मामले टी में सूचीबद्ध एक वर्ग आप मामले में एक वैध टी होने के लिए क्या आवश्यक है, आप कहा है, "एक मान्य टी, एक वर्ग तुलनीय को लागू करना चाहिए होने के लिए" (कीवर्ड "फैली ", लेकिन व्यवहार में इसका मतलब है कि" विस्तृत होती या लागू करता "।)

अपने इन्स्टेन्शियशन में, टी छात्र है। हम टी के लिए छात्र स्थानापन्न हैं:

public class BinarySearchTree<Student extends Comparable<Student>>

एक सच्चे बयान है? छात्र वास्तव में तुलनीय को लागू करता है?

यदि ऐसा है, छात्र एक टी जा रहा है की आवश्यकता को फिट बैठता है, और इसलिए आप औपचारिक पैरामीटर टी के लिए वास्तविक पैरामीटर के रूप में उपयोग कर सकते हैं छात्र

यदि नहीं, तो संकलक की शिकायत आप देखा हो।

वास्तव में, जहां तुलनीय का एक उपवर्ग के कार्यान्वयन के लिए एक सुपर वर्ग द्वारा किया जाता है और अधिक जटिल स्थितियों को कवर करने, अधिक सामान्य रूप होगा:

   public class BinarySearchTree<T extends Comparable<? super T > > 

तो अगर आप छात्र को लागू तुलनीय <छात्र> बनाने की जरूरत है।

ध्यान दें कि मैं नहीं था का कहना है कि संकलक एक की तलाश में Student.compareTo। यह भी है कि अब तक नहीं मिलता है। यह अगर टी (अपने मामले, छात्र में) को लागू करने के रूप में घोषित किया जाता है देखने के लिए की तलाश में तुलनीय <टी> (आपके मामले में, तुलनीय <छात्र>)।

अब जोड़ने implements Comparable< Student >छात्र को होगा भी संकलक सुनिश्चित एक बात यह है कि वहाँ बनाने के public int compareToछात्र पर विधि। लेकिन बिना "तुलनीय लागू करता है", संकलक वहाँ एक तरीका है जानता है, भले ही Student.compareTo, यह पता नहीं है कि कि compareToहै Comparable.compareTo

(दूसरे शब्दों में, हम घोषित कार्यान्वयन के लिए देख रहे हैं, न सिर्फ वहाँ सही नाम और हस्ताक्षर के साथ एक विधि के लिए होता है।)

02/05/2009 को 06:57
का स्रोत उपयोगकर्ता

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