(AVL) एक BinarySearchTree संतुलित करने की संशोधित करें: जावा

वोट
2

मैं एक द्विआधारी खोज वृक्ष है कि मैं विश्वास दिलाता हूं कि यह संतुलित है बनाया संशोधित करने की आवश्यकता। मैं सिर्फ अपने निर्देशों के अनुसार जोड़ने, संशोधित करने और तरीकों को दूर करने की जरूरत है। यहाँ मैं वर्तमान में क्या है:

package proj;

public class BinarySearchTree<T extends Comparable<T>>{
    public static void main(String[] args) {
        BinarySearchTree<Integer> tree = new BinarySearchTree<Integer>();
        tree.add(5);
        tree.add(1);
        tree.add(2);
        tree.add(6);
    }

    private Node<T> root;
    private int size;
    String inorder = ;
    String preorder = ;

    public BinarySearchTree(){
        root = null;
        size = 0;
    }

    //adds a new item to the queue
    public void add(T obj) {
        Node<T> n = new Node<T>(obj);
        if( root == null ) {
            root = n;
        } else {
            add( root, n );
        }
        size++;
    }

    private void add(Node<T> subtree, Node<T> n) {
        if( subtree.getValue().compareTo(n.getValue()) > 0 ) {
            if( subtree.getLeftChild() == null ) {
                subtree.setLeftChild(n);
                n.setParent(subtree);
            } else {
                add( subtree.getLeftChild(), n );
            }
        } else {
            if( subtree.getRightChild() == null ) {
                subtree.setRightChild(n);
                n.setParent(subtree);
            } else {
                add( subtree.getRightChild(), n );
            }
        }
    }

    //returns the head of the queue
    public T peek(){
        Node<T> current = root;
        while(current.getLeftChild() != null){
            current = current.getLeftChild();
        }
        return current.getValue();
    }

    //removes the head of the queue and returns it
    public T remove(){
        if(root == null){
            return null;
        }

        Node<T> current = root;
        while(current.getLeftChild() != null){
            current = current.getLeftChild();
        }
        if( current.getParent() == null ) {
            root = current.getRightChild();
            if(root != null){
                root.setParent(null);
            }
        } else {
            current.getParent().setLeftChild(current.getRightChild());
            if(current.getRightChild() != null){
                current.getRightChild().setParent(current.getParent());
            }
        }
        size--;
        return current.getValue();
    }

    //returns the position of an element in the queue, or -1 if it is not found
    public int search(T searchItem){
        String tempOrdered = inorder(root);
        for(int i = 0; i<tempOrdered.length(); i++){
            if(String.valueOf(tempOrdered.charAt(i)).equals(searchItem.toString())){
                return i;
            }
        }
        return -1;
    }

    //returns number of nodes in the tree
    //returns the total number of elements in the queue
    public int getSize(){
        return size;
    }
    public String inorder() {
        inorder = ;
        if( root == null )
            return inorder;
        return inorder(root);
    }

    //returns an in-order, comma-separated string of every element in the queue
    private String inorder(Node<T> n){
        if(n.getLeftChild() != null){
            inorder(n.getLeftChild());
        }
        inorder += n.getValue();
        if(n.getRightChild() != null){
            inorder(n.getRightChild());
        }
        return inorder;
    }

    public String preorder() {
        preorder = ;
        if( root == null )
            return preorder;
        return preorder(root);
    }

    //returns a pre-ordered, comma-separated string of every element in the queue
    private String preorder(Node<T> n){
        preorder+= n.getValue();
        if(n.getLeftChild() != null){
            preorder(n.getLeftChild());
        }
        if(n.getRightChild() != null){
            preorder(n.getRightChild());
        }

        return preorder;
    }

    //returns the height of the tree; returns -1 if the tree is empty
    public int height(Node<T> n){
        if(n == null){
            return -1;
        }
        return Math.max(height(n.getLeftChild()), height(n.getRightChild()))+ 1;
    }

    //returns the root node
    public Node<T> getRoot(){
        return root;
    }
}

मैं किसी को इस काम के माध्यम से मुझे चलने के लिए नहीं देख रहा हूँ - बस कैसे मैं यह कर के बारे में जाना चाहिए, ताकि मैं में कोड आधे रास्ते नहीं टूटते के रूप में कुछ सलाह की तलाश में मेरा अनुमान है कि यह है कि मैं की आवश्यकता होगी। पेड़ हर बार कुछ जोड़ा या हटा दिया जाता है के संतुलन कारक जाँच के प्रभाव के लिए कुछ करने के लिए है, तो पेड़ या 'रोटेट करें' को फिर से संगठित जब यह असंतुलित है।

अग्रिम में किसी भी सलाह के लिए धन्यवाद। :) सभी सुझावों की सराहना।

-क्रिस

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


1 जवाब

वोट
1

AVL पेड़ विकिपीडिया पर लेख सब आप स्वयं संतुलित पेड़ इस तरह लागू करने की आवश्यकता देता है (मैं विशेष रूप से पसंद चित्र पुनर्संतुलन के लिए आवश्यक रोटेशन दिखा रहा है)। मूल रूप से आप छोड़ दिया और सही पेड़ रोटेशन लागू करने और अपने में इसका इस्तेमाल करने की जरूरत है addऔर removeलेख में दिए गए नियमों के अनुसार तरीकों।

आप अधिक साहसिक कर रहे हैं, एक लाल-काले पेड़ को लागू करने का प्रयास करें। छद्म कोड के साथ एक अच्छा वर्णन में पाया जा सकता एल्गोरिथम का परिचय

02/06/2011 को 17:56
का स्रोत उपयोगकर्ता

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