Question

i think that the problem is root being null. WOuld someone one teach me how to do it the proper way ? Because i don't know why it is null ... I think the remove function is well implemented, but due to root being null it doesn't continue executing. Help please. Any recommendation are welcome too :D

public static void main(String[] args) {
        Tree t = new Tree("");
        String msg;
        String[] inputs;
        Scanner sc = new Scanner(System.in);
        ArrayList <String> palavras = new ArrayList <String>();
        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++;
            }
        }



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


            } else {
                t.insert(palavras.get(i).toLowerCase());
                i++;
            }
        }   

        t.postorder();
        t.preorder();
        t.inorder();



    }


**********************************************************

public void remove( String word) 
    {
        Node father=root;
        Node actual=root;
        boolean leftnode=true;


        if(root!=null){

            while(!word.equals(actual.str))
            {
              father=actual;      

              if(word.compareTo(actual.str)<0)
              {
                  leftnode=true;
                  actual=actual.left;
              }
              else{
                leftnode=false;
                actual=actual.right;
              }

              if(actual==null){
               return ;
              }
            }

            actual.occ = 0;   
        }
    }
Was it helpful?

Solution

I was implementing Splay tree which works on the same concept as red black tree and at a point I was stuck like this. I will suggest you to draw the flow diagram of your code and then code it again. I hope your problem will be solved as mine. you can refer this and this articles.

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