Question

My source code:

import java.util.*;

public class Arvore {

    public Arvore() {
        root = null;
    }

    public void inserir(String x) {
        root = insert(x, root);
    }

    public void remove(String x) {
        root = remove(x, root);
    }

    private No remove(String x, No t) {
        if (t == null) {
            return t;
        }

        int comp = x.compareTo(t.str);

        if (comp < 0) {
            t.left = remove(x, t.left);
        } else if (comp > 0) {
            t.right = remove(x, t.right);
        } else if (t.left != null && t.right != null) {
            t.str = findMin(t.right).str;
            t.right = remove(t.str, t.right);
        } else {
            t = (t.left != null) ? t.left : t.right;
        }
        return balance(t);
    }

    public String findMin() {
        return findMin(root).str;
    }

    public String findMax() {
        return findMax(root).str;
    }

    public boolean contains(String x) {
        return contains(x, root);
    }

    public void makeEmpty() {
        root = null;
    }

    public boolean isEmpty() {
        return root == null;
    }
    private static final int maxBal = 1;

    private No balance(No t) {
        if (t == null) {
            return t;
        }

        if (height(t.left) - height(t.right) > maxBal) {
            if (height(t.left.left) >= height(t.left.right)) {
                t = rotateWithLeftChild(t);
            } else {
                t = doubleWithLeftChild(t);
            }
        } else if (height(t.right) - height(t.left) > maxBal) {
            if (height(t.right.right) >= height(t.right.left)) {
                t = rotateWithRightChild(t);
            } else {
                t = doubleWithRightChild(t);
            }
        }

        t.height = Math.max(height(t.left), height(t.right)) + 1;
        return t;
    }

    public void checkBalance() {
        checkBalance(root);
    }

    private int checkBalance(No t) {
        if (t == null) {
            return -1;
        }

        if (t != null) {
            int hl = checkBalance(t.left);
            int hr = checkBalance(t.right);
            if (Math.abs(height(t.left) - height(t.right)) > 1
                    || height(t.left) != hl || height(t.right) != hr) {
                System.out.println("OOPS!!");
            }
        }

        return height(t);
    }

    private No insert(String x, No t) {
        if (t == null) {
            return new No(x, null, null);
        }

        int comp = x.compareTo(t.str);

        if (comp < 0) {
            t.left = insert(x, t.left);
        } else if (comp > 0) {
            t.right = insert(x, t.right);
        } else
            ;

        t.occ+=1;

        return balance(t);
    }

    private No findMin(No t) {
        if (t == null) {
            return t;
        }

        while (t.left != null) {
            t = t.left;
        }
        return t;
    }

    private No findMax(No t) {
        if (t == null) {
            return t;
        }

        while (t.right != null) {
            t = t.right;
        }
        return t;
    }

    private boolean contains(String x, No t) {
        while (t != null) {
            int comp = x.compareTo(t.str);

            if (comp < 0) {
                t = t.left;
            } else if (comp > 0) {
                t = t.right;
            } else {
                return true;
            }
        }

        return false;
    }

    private void printTree(No t) {
        if (t != null) {
            printTree(t.left);
            System.out.println(t.str + ": " + t.occ);
            printTree(t.right);
        }
    }

    private int height(No t) {
        return t == null ? -1 : t.height;
    }

    private No rotateWithLeftChild(No k2) {
        No k1 = k2.left;
        k2.left = k1.right;
        k1.right = k2;
        k2.height = Math.max(height(k2.left), height(k2.right)) + 1;
        k1.height = Math.max(height(k1.left), k2.height) + 1;
        return k1;
    }

    private No rotateWithRightChild(No k1) {
        No k2 = k1.right;
        k1.right = k2.left;
        k2.left = k1;
        k1.height = Math.max(height(k1.left), height(k1.right)) + 1;
        k2.height = Math.max(height(k2.right), k1.height) + 1;
        return k2;
    }

    private No doubleWithLeftChild(No k3) {
        k3.left = rotateWithRightChild(k3.left);
        return rotateWithLeftChild(k3);
    }

    private No doubleWithRightChild(No k1) {
        k1.right = rotateWithLeftChild(k1.right);
        return rotateWithRightChild(k1);
    }


    private class No {

        No(String tStr) {
            this(tStr, null, null);
        }

        No(String tStr, No lt, No rt) {
            str = tStr;
            left = lt;
            right = rt;
            height = 0;
            occ = 1;
        }
        String str;
        No left;
        No right;
        int height;
        int occ = 0;
    }
    private No root;

    public static void main(String[] args) {
        Arvore t = new Arvore();
        System.out.println("AVL TREE TEST\n");
        String msg;
        String[] inputs;
        Scanner sc = new Scanner(System.in);
        ArrayList palavras = new ArrayList();
        int i = 0;

        while (true) {
            msg = sc.nextLine();
            if (msg.equals("")) {
                break;
            }


            inputs = msg.split(" ");

            i = 0;

            while (i < inputs.length) {
                palavras.add(inputs[i]);
            }

        }

        i = 0;
        while (i < palavras.size()) {
            if (palavras.get(i).equals("REMOVE")) {
                palavras.remove(palavras.get(i));
                palavras.remove(palavras.get(i + 1));
                i += 2;
            } else {
                t.insert(palavras.get(i));
            }
            i++;
        }




    t.printTree();
    }
}

I can't figure out why i have an error when i call insert and printTree, on the main function. And i have a warning by adding a string to my arraylist, when i do palavras.add(inputs[i]).

Was it helpful?

Solution

Your loop:

i = 0;

while (i < inputs.length) {
    palavras.add(inputs[i]);
}

is not incrementing i, so you will eventually run out of space for the array as you try to insert an infinite number of references to inputs[0].

The warning is because you are using a raw ArrayList. Try declaring palavras as:

ArrayList<String> palavras = new ArrayList<>(); // new ArrayList<String>(); if pre Java 7

Your other loop:

i = 0;
while (i < palavras.size()) {
    if (palavras.get(i).equals("REMOVE")) {
        palavras.remove(palavras.get(i));
        palavras.remove(palavras.get(i + 1));
        i += 2;
    } else {
        t.insert(palavras.get(i));
    }
    i++;
}

will generate an IndexOutOfBoundsException if the first branch of the if is taken when i == palavras.size() - 1 when it tries to remove the second element. Even if you are guaranteed that "REMOVE" is always followed by another element, you need to either reverse the order of the removals:

palavras.remove(palavras.get(i + 1));
palavras.remove(palavras.get(i));

or remove the element at i twice:

palavras.remove(palavras.get(i));
palavras.remove(palavras.get(i));

(because when you call remove() it moves all succeeding elements down one position in the array).

You are calling t.printTree() but, there is no such method defined in class Arvore; the method you did define takes a tree as an argument. Same thing with insert(): the actual method that's available takes a String and a No as arguments.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top